最新要闻
- 世界观察:民营天龙二号液体火箭首飞成功:还隐藏了一个中国第一
- 天天看热讯:不知道这几点!你买电动牙刷就是花冤枉钱
- 世界热资讯!热泵干衣机被严重低估了!浑身都是宝
- 环球快报:2023上海车展丨这些即将首发的热门新车你一定不要错过!
- 云南泼水节白天是热闹夜晚是浪漫:市民游客共狂欢
- 员工回应公司发布高薪招聘老板公告:不是开玩笑
- 贾跃亭憋了九年的车终于量产?结果 又一张大饼!
- 环球快报:四边等宽的鸿蒙手机来了!华为nova 11明天发
- 潍坊风筝节放飞打工人的心声:引发网友热议
- Intel鸡血驱动暴涨63%!Arc A750性价比秒杀RTX 3060 72%!
- 当前最新:天舟六号飞船、长征七号火箭抵达文昌!五次发射 100%成功
- 山东泰山队vs上海申花首发出炉:四外援先发,韩镕泽镇守球门
- 每日热讯!“泼水节被撕扯雨衣”女生发声:很崩溃
- 世界快报:众人狂欢!泼水节连狗路过都得淋两桶水再走
- 贝贝健电蚊香液到手14.9元:驱赶蚊虫神器 夏天必备
- 全球信息:超级SSD 21合一组成168TB!价格直奔20万元
广告
手机
iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?
警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案
- iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?
- 警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案
- 男子被关545天申国赔:获赔18万多 驳回精神抚慰金
- 3天内26名本土感染者,辽宁确诊人数已超安徽
- 广西柳州一男子因纠纷杀害三人后自首
- 洱海坠机4名机组人员被批准为烈士 数千干部群众悼念
家电
基于GPT3的代码编辑器Cursor试用-你的智能代码编辑助手
一、安装和使用
官网下载安装Cursor,然后创建一个代码文件。
Ctrl + K生成, Ctrl + L提问。
默认每小时30词。
【资料图】
- # 1. Try generating with command K on a new line. Ask for a pytorch script of a feedforward neural network
- # 2. Then, select the outputted code and hit chat. Ask if there"s a bug. Ask how to improve.
- # 3. Try selecting some code and hitting edit. Ask the bot to add residual layers.
- # 4. To try out cursor on your own projects, go to the file menu (top left) and open a folder.
有些包需要自行安装,程序不一定一次通过运行,可提问。
二、测试
1. 乒乓球游戏
请用python写一个乒乓球游戏。有如下要求:- 游戏包含两个参与者,一方是电脑,一方是玩家;电脑在界面左侧,玩家在界面右侧。- 游戏界面上包含一个开始按钮,点击开始按钮后,游戏才能开始。- 乒乓球接触球拍和界面上下边界时会反弹。- 玩家通过Up,Down键来控制球拍的上下运动,玩家按住对应的键,球拍会保持一个方向运动。- 电脑通过自动判断乒乓球的位置来进行移动,尽可能的将乒乓球反弹。- 一方的球拍没有接到球的时候,对方得1分,先得够5分的一方获胜,同时游戏结束。- 游戏结束后显示获胜的一方的角色:电脑或者玩家。
代码
# Import necessary modulesimport pygameimport random# Initialize Pygamepygame.init()# Set up the game windowWINDOW_WIDTH = 800WINDOW_HEIGHT = 600WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))pygame.display.set_caption("Ping Pong Game")# Set up the colorsWHITE = (255, 255, 255)BLACK = (0, 0, 0)# Set up the fontsFONT = pygame.font.SysFont(None, 48)# Set up the ballBALL_RADIUS = 10ball_x = WINDOW_WIDTH // 2ball_y = WINDOW_HEIGHT // 2ball_dx = 5 * random.choice([-1, 1])ball_dy = 5 * random.choice([-1, 1])# Set up the paddlesPADDLE_WIDTH = 10PADDLE_HEIGHT = 100player_paddle_x = WINDOW_WIDTH - PADDLE_WIDTH - 10player_paddle_y = WINDOW_HEIGHT // 2 - PADDLE_HEIGHT // 2computer_paddle_x = 10computer_paddle_y = WINDOW_HEIGHT // 2 - PADDLE_HEIGHT // 2paddle_speed = 5# Set up the scoresplayer_score = 0computer_score = 0# Set up the game loopgame_running = Falsegame_ended = Falseclock = pygame.time.Clock()while not game_ended: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: game_ended = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: game_running = True elif event.type == pygame.KEYUP: if event.key == pygame.K_UP or event.key == pygame.K_DOWN: player_paddle_dy = 0 # Handle player input keys = pygame.key.get_pressed() if keys[pygame.K_UP]: player_paddle_dy = -paddle_speed elif keys[pygame.K_DOWN]: player_paddle_dy = paddle_speed else: player_paddle_dy = 0 # Move the paddles player_paddle_y += player_paddle_dy if player_paddle_y < 0: player_paddle_y = 0 elif player_paddle_y > WINDOW_HEIGHT - PADDLE_HEIGHT: player_paddle_y = WINDOW_HEIGHT - PADDLE_HEIGHT computer_paddle_y += ball_dy if computer_paddle_y < 0: computer_paddle_y = 0 elif computer_paddle_y > WINDOW_HEIGHT - PADDLE_HEIGHT: computer_paddle_y = WINDOW_HEIGHT - PADDLE_HEIGHT # Move the ball if game_running: ball_x += ball_dx ball_y += ball_dy # Check for collisions with the walls if ball_y < BALL_RADIUS or ball_y > WINDOW_HEIGHT - BALL_RADIUS: ball_dy *= -1 # Check for collisions with the paddles if ball_x < computer_paddle_x + PADDLE_WIDTH and \ computer_paddle_y < ball_y < computer_paddle_y + PADDLE_HEIGHT: ball_dx *= -1 elif ball_x > player_paddle_x and \ player_paddle_y < ball_y < player_paddle_y + PADDLE_HEIGHT: ball_dx *= -1 elif ball_x < 0: player_score += 1 game_running = False ball_x = WINDOW_WIDTH // 2 ball_y = WINDOW_HEIGHT // 2 ball_dx = 5 * random.choice([-1, 1]) ball_dy = 5 * random.choice([-1, 1]) elif ball_x > WINDOW_WIDTH: computer_score += 1 game_running = False ball_x = WINDOW_WIDTH // 2 ball_y = WINDOW_HEIGHT // 2 ball_dx = 5 * random.choice([-1, 1]) ball_dy = 5 * random.choice([-1, 1]) # Check for game over if player_score == 5 or computer_score == 5: game_running = False game_over_text = FONT.render("Game Over", True, WHITE) if player_score == 5: winner_text = FONT.render("Player Wins!", True, WHITE) else: winner_text = FONT.render("Computer Wins!", True, WHITE) # Draw the game WINDOW.fill(BLACK) if not game_running: start_text = FONT.render("Press Space to Start", True, WHITE) start_rect = start_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)) WINDOW.blit(start_text, start_rect) pygame.draw.circle(WINDOW, WHITE, (ball_x, ball_y), BALL_RADIUS) pygame.draw.rect(WINDOW, WHITE, (player_paddle_x, player_paddle_y, PADDLE_WIDTH, PADDLE_HEIGHT)) pygame.draw.rect(WINDOW, WHITE, (computer_paddle_x, computer_paddle_y, PADDLE_WIDTH, PADDLE_HEIGHT)) player_score_text = FONT.render(str(player_score), True, WHITE) computer_score_text = FONT.render(str(computer_score), True, WHITE) score_rect = player_score_text.get_rect(midtop=(WINDOW_WIDTH // 2, 10)) WINDOW.blit(player_score_text, score_rect.move(-50, 0)) WINDOW.blit(computer_score_text, score_rect.move(50, 0)) if not game_running and (player_score > 0 or computer_score > 0): WINDOW.blit(game_over_text, game_over_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 - 50))) WINDOW.blit(winner_text, winner_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 50))) pygame.display.update() clock.tick(60)# Quit Pygamepygame.quit()View Code
可以运行
2. 手写数字识别
用Python写一个基于MINIST数据集的手写数字识别代码,并给出测试样例
import numpy as npimport tensorflow as tffrom tensorflow.keras.datasets import mnistfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense# Load MNIST dataset(x_train, y_train), (x_test, y_test) = mnist.load_data()# Normalize pixel values to be between 0 and 1x_train = x_train / 255.0x_test = x_test / 255.0# Reshape input data to have a single channelx_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], x_train.shape[2], 1))x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], x_test.shape[2], 1))# Define LeNet modelmodel = Sequential()model.add(Conv2D(filters=6, kernel_size=(5, 5), activation="relu", input_shape=(28, 28, 1)))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Conv2D(filters=16, kernel_size=(5, 5), activation="relu"))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Flatten())model.add(Dense(units=120, activation="relu"))model.add(Dense(units=84, activation="relu"))model.add(Dense(units=10, activation="softmax"))# Compile modelmodel.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])# Train modelmodel.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))# Visualize test example# Visualize test exampleimport matplotlib.pyplot as pltimage_index = 4444plt.imshow(x_test[image_index].reshape(28, 28),cmap="Greys")plt.show()# Predict test examplepred = model.predict(x_test[image_index].reshape(1, 28, 28, 1))print("Predicted value:", pred.argmax())View Code
3. 聚类
k-means: 用Python实现K-means算法,并在iris数据集上进行测试,最后对结果进行可视化
from sklearn.datasets import load_irisfrom sklearn.cluster import KMeansimport matplotlib.pyplot as plt# load iris datasetiris = load_iris()X = iris.data# initialize KMeans model with 3 clusterskmeans = KMeans(n_clusters=3)# fit the model to the datakmeans.fit(X)# predict the clusters for each data pointy_kmeans = kmeans.predict(X)# plot the clustersplt.scatter(X[:, 0], X[:, 1], c=y_kmeans, cmap="viridis")centers = kmeans.cluster_centers_plt.scatter(centers[:, 0], centers[:, 1], c="black", s=200, alpha=0.5);plt.show()View Code
GMM: 用numpy实现GMM算法,给出测试样例
# Welcome to Cursorimport numpy as npimport matplotlib.pyplot as pltfrom scipy.stats import multivariate_normal# Generate some random datanp.random.seed(0)n_samples = 300mean1 = [0, 0]cov1 = [[1, 0], [0, 1]]mean2 = [5, 5]cov2 = [[1, 0], [0, 1]]X = np.concatenate([np.random.multivariate_normal(mean1, cov1, int(n_samples/2)), np.random.multivariate_normal(mean2, cov2, int(n_samples/2))])# Define the number of clusters and the tolerance for convergencen_clusters = 2tolerance = 0.01# Initialize the means, covariances, and mixing coefficientsmeans = np.random.rand(n_clusters, X.shape[1])covs = np.array([np.eye(X.shape[1])] * n_clusters)mixing_coeffs = np.ones(n_clusters) / n_clusters# Define the function to compute the responsibilitiesdef compute_responsibilities(X, means, covs, mixing_coeffs): responsibilities = np.zeros((X.shape[0], len(mixing_coeffs))) for k in range(len(mixing_coeffs)): responsibilities[:, k] = mixing_coeffs[k] * multivariate_normal.pdf(X, means[k], covs[k]) responsibilities /= np.sum(responsibilities, axis=1, keepdims=True) return responsibilities# Define the function to update the means, covariances, and mixing coefficientsdef update_parameters(X, responsibilities): n_clusters = responsibilities.shape[1] n_samples = X.shape[0] means = np.zeros((n_clusters, X.shape[1])) covs = np.zeros((n_clusters, X.shape[1], X.shape[1])) mixing_coeffs = np.zeros(n_clusters) for k in range(n_clusters): nk = np.sum(responsibilities[:, k]) means[k] = np.sum(responsibilities[:, k].reshape(-1, 1) * X, axis=0) / nk covs[k] = np.dot((responsibilities[:, k].reshape(-1, 1) * (X - means[k])).T, (X - means[k])) / nk mixing_coeffs[k] = nk / n_samples return means, covs, mixing_coeffs# Run the EM algorithmlog_likelihoods = []for i in range(100): responsibilities = compute_responsibilities(X, means, covs, mixing_coeffs) means, covs, mixing_coeffs = update_parameters(X, responsibilities) log_likelihood = np.sum(np.log(np.sum(responsibilities, axis=1))) log_likelihoods.append(log_likelihood) if i > 0 and np.abs(log_likelihoods[-1] - log_likelihoods[-2]) < tolerance: break# Plot the data and the clustersplt.scatter(X[:, 0], X[:, 1], c=np.argmax(responsibilities, axis=1))plt.show()View Code
4. RNN
用分别用Pytorh和Numpy写一个RNN网络,然后在一个1960年到2022年世界总人口树的时序数据上对它们进行测试。要求:-对比Arima算法-对数据进行可视化-用1960年到2012年预测2013年到2022年,对预测结果可视化
# PyTorch RNN networkimport torchimport torch.nn as nnclass RNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(RNN, self).__init__() self.hidden_size = hidden_size self.i2h = nn.Linear(input_size + hidden_size, hidden_size) self.i2o = nn.Linear(input_size + hidden_size, output_size) self.softmax = nn.LogSoftmax(dim=1) def forward(self, input, hidden): combined = torch.cat((input, hidden), 1) hidden = self.i2h(combined) output = self.i2o(combined) output = self.softmax(output) return output, hidden def initHidden(self): return torch.zeros(1, self.hidden_size)# Numpy RNN networkimport numpy as npclass NumpyRNN: def __init__(self, input_size, hidden_size, output_size): self.hidden_size = hidden_size self.Wxh = np.random.randn(hidden_size, input_size) * 0.01 self.Whh = np.random.randn(hidden_size, hidden_size) * 0.01 self.Why = np.random.randn(output_size, hidden_size) * 0.01 self.bh = np.zeros((hidden_size, 1)) self.by = np.zeros((output_size, 1)) def forward(self, inputs, hprev): xs, hs, ys, ps = {}, {}, {}, {} hs[-1] = np.copy(hprev) for t in range(len(inputs)): xs[t] = np.zeros((input_size, 1)) xs[t][inputs[t]] = 1 hs[t] = np.tanh(np.dot(self.Wxh, xs[t]) + np.dot(self.Whh, hs[t-1]) + self.bh) ys[t] = np.dot(self.Why, hs[t]) + self.by ps[t] = np.exp(ys[t]) / np.sum(np.exp(ys[t])) return ps, hs def initHidden(self): return np.zeros((self.hidden_size, 1))# Testing on world population dataimport pandas as pdimport matplotlib.pyplot as pltfrom statsmodels.tsa.arima_model import ARIMA# Load datadata = pd.read_csv("world_population.csv", index_col=0)data.index = pd.to_datetime(data.index)# Train-test splittrain_data = data.loc["1960":"2012"]test_data = data.loc["2013":"2022"]# ARIMA modelmodel = ARIMA(train_data, order=(1, 1, 1))model_fit = model.fit(disp=0)arima_pred = model_fit.predict(start="2013", end="2022", dynamic=False)# PyTorch RNN modelinput_size = 1hidden_size = 32output_size = 1pytorch_rnn = RNN(input_size, hidden_size, output_size)criterion = nn.MSELoss()optimizer = torch.optim.Adam(pytorch_rnn.parameters(), lr=0.01)# Train PyTorch RNN modelfor epoch in range(1000): hidden = pytorch_rnn.initHidden() optimizer.zero_grad() loss = 0 for i in range(len(train_data)-1): input_tensor = torch.tensor(train_data.iloc[i]) target_tensor = torch.tensor(train_data.iloc[i+1]) output, hidden = pytorch_rnn(input_tensor, hidden) loss += criterion(output, target_tensor) loss.backward() optimizer.step()# Test PyTorch RNN modelpytorch_rnn.eval()pytorch_pred = []hidden = pytorch_rnn.initHidden()for i in range(len(test_data)): input_tensor = torch.tensor(test_data.iloc[i]) output, hidden = pytorch_rnn(input_tensor, hidden) pytorch_pred.append(output.detach().numpy()[0][0])# Numpy RNN modelinput_size = 1hidden_size = 32output_size = 1numpy_rnn = NumpyRNN(input_size, hidden_size, output_size)# Train Numpy RNN modelloss_fn = nn.MSELoss()learning_rate = 0.01for epoch in range(1000): hprev = numpy_rnn.initHidden() loss = 0 for i in range(len(train_data)-1): x = np.array(train_data.iloc[i]) y = np.array(train_data.iloc[i+1]) ps, hprev = numpy_rnn.forward(x, hprev) loss += loss_fn(torch.tensor(ps[len(ps)-1]), torch.tensor(y)) # Backpropagation dWhy = np.dot((ps[len(ps)-1]-y), hprev.T) dby = ps[len(ps)-1]-y dhnext = np.dot(numpy_rnn.Why.T, ps[len(ps)-1]-y) dWxh, dWhh, dbh = np.zeros_like(numpy_rnn.Wxh), np.zeros_like(numpy_rnn.Whh), np.zeros_like(numpy_rnn.bh) for t in reversed(range(len(ps)-1)): dy = np.copy(ps[t]) dy[train_data.iloc[t+1]] -= 1 dWhy += np.dot(dy, hs[t].T) dby += dy dh = np.dot(numpy_rnn.Why.T, dy) + dhnext dhraw = (1 - hs[t] * hs[t]) * dh dbh += dhraw dWxh += np.dot(dhraw, xs[t].T) dWhh += np.dot(dhraw, hs[t-1].T) dhnext = np.dot(numpy_rnn.Whh.T, dhraw) for dparam in [dWxh, dWhh, dWhy, dbh, dby]: np.clip(dparam, -5, 5, out=dparam) numpy_rnn.Wxh -= learning_rate * dWxh numpy_rnn.Whh -= learning_rate * dWhh numpy_rnn.Why -= learning_rate * dWhy numpy_rnn.bh -= learning_rate * dbh numpy_rnn.by -= learning_rate * dby# Test Numpy RNN modelnumpy_pred = []hprev = numpy_rnn.initHidden()for i in range(len(test_data)): x = np.array(test_data.iloc[i]) ps, hprev = numpy_rnn.forward(x, hprev) numpy_pred.append(ps[len(ps)-1].argmax())# Visualize resultsplt.figure(figsize=(10,5))plt.plot(train_data.index, train_data, label="Training data")plt.plot(test_data.index, test_data, label="True values")plt.plot(test_data.index, arima_pred, label="ARIMA")plt.plot(test_data.index, pytorch_pred, label="PyTorch RNN")plt.plot(test_data.index, numpy_pred, label="Numpy RNN")plt.legend()plt.show()View Code
不知道对不对,训练代码都给出了
5. 自注意力
用Pytorch实现自注意力网络,并进行测试
import torchimport torch.nn as nnimport torch.nn.functional as Fclass SelfAttention(nn.Module): def __init__(self, embed_size, heads): super(SelfAttention, self).__init__() self.embed_size = embed_size self.heads = heads self.head_dim = embed_size // heads assert (self.head_dim * heads == embed_size), "Embed size needs to be divisible by heads" self.values = nn.Linear(self.head_dim, self.head_dim, bias=False) self.keys = nn.Linear(self.head_dim, self.head_dim, bias=False) self.queries = nn.Linear(self.head_dim, self.head_dim, bias=False) self.fc_out = nn.Linear(heads*self.head_dim, embed_size) def forward(self, values, keys, query, mask): N = query.shape[0] value_len, key_len, query_len = values.shape[1], keys.shape[1], query.shape[1] # Split embedding into self.heads pieces values = values.reshape(N, value_len, self.heads, self.head_dim) keys = keys.reshape(N, key_len, self.heads, self.head_dim) query = query.reshape(N, query_len, self.heads, self.head_dim) values = self.values(values) # (N, value_len, heads, head_dim) keys = self.keys(keys) # (N, key_len, heads, head_dim) queries = self.queries(query) # (N, query_len, heads, head_dim) energy = torch.einsum("nqhd,nkhd->nhqk", [queries, keys]) # (N, heads, query_len, key_len) if mask is not None: energy = energy.masked_fill(mask == 0, float("-1e20")) attention = torch.softmax(energy / (self.embed_size ** (1/2)), dim=3) # (N, heads, query_len, key_len) out = torch.einsum("nhql,nlhd->nqhd", [attention, values]).reshape(N, query_len, self.heads*self.head_dim) out = self.fc_out(out) return outclass TransformerBlock(nn.Module): def __init__(self, embed_size, heads, dropout, forward_expansion): super(TransformerBlock, self).__init__() self.attention = SelfAttention(embed_size, heads) self.norm1 = nn.LayerNorm(embed_size) self.norm2 = nn.LayerNorm(embed_size) self.feed_forward = nn.Sequential( nn.Linear(embed_size, forward_expansion*embed_size), nn.ReLU(), nn.Linear(forward_expansion*embed_size, embed_size) ) self.dropout = nn.Dropout(dropout) def forward(self, value, key, query, mask): attention = self.attention(value, key, query, mask) x = self.dropout(self.norm1(attention + query)) forward = self.feed_forward(x) out = self.dropout(self.norm2(forward + x)) return outclass Encoder(nn.Module): def __init__(self, src_vocab_size, embed_size, num_layers, heads, device, forward_expansion, dropout, max_length): super(Encoder, self).__init__() self.embed_size = embed_size self.device = device self.word_embedding = nn.Embedding(src_vocab_size, embed_size) self.position_embedding = nn.Embedding(max_length, embed_size) self.layers = nn.ModuleList([ TransformerBlock(embed_size, heads, dropout=dropout, forward_expansion=forward_expansion) for _ in range(num_layers) ]) self.dropout = nn.Dropout(dropout) def forward(self, x, mask): N, seq_length = x.shape positions = torch.arange(0, seq_length).expand(N, seq_length).to(self.device) out = self.dropout(self.word_embedding(x) + self.position_embedding(positions)) for layer in self.layers: out = layer(out, out, out, mask) return outclass DecoderBlock(nn.Module): def __init__(self, embed_size, heads, forward_expansion, dropout, device): super(DecoderBlock, self).__init__() self.norm = nn.LayerNorm(embed_size) self.attention = SelfAttention(embed_size, heads) self.transformer_block = TransformerBlock(embed_size, heads, dropout, forward_expansion) self.dropout = nn.Dropout(dropout) def forward(self, x, value, key, src_mask, trg_mask): attention = self.attention(x, x, x, trg_mask) query = self.dropout(self.norm(attention + x)) out = self.transformer_block(value, key, query, src_mask) return outclass Decoder(nn.Module): def __init__(self, trg_vocab_size, embed_size, num_layers, heads, forward_expansion, dropout, device, max_length): super(Decoder, self).__init__() self.device = device self.word_embedding = nn.Embedding(trg_vocab_size, embed_size) self.position_embedding = nn.Embedding(max_length, embed_size) self.layers = nn.ModuleList([ DecoderBlock(embed_size, heads, forward_expansion, dropout, device) for _ in range(num_layers) ]) self.fc_out = nn.Linear(embed_size, trg_vocab_size) self.dropout = nn.Dropout(dropout) def forward(self, x, enc_out, src_mask, trg_mask): N, seq_length = x.shape positions = torch.arange(0, seq_length).expand(N, seq_length).to(self.device) x = self.dropout(self.word_embedding(x) + self.position_embedding(positions)) for layer in self.layers: x = layer(x, enc_out, enc_out, src_mask, trg_mask) out = self.fc_out(x) return outclass Transformer(nn.Module): def __init__(self, src_vocab_size, trg_vocab_size, src_pad_idx, trg_pad_idx, embed_size=256, num_layers=6, forward_expansion=4, heads=8, dropout=0, device="cuda", max_length=100): super(Transformer, self).__init__() self.encoder = Encoder(src_vocab_size, embed_size, num_layers, heads, device, forward_expansion, dropout, max_length) self.decoder = Decoder(trg_vocab_size, embed_size, num_layers, heads, forward_expansion, dropout, device, max_length) self.src_pad_idx = src_pad_idx self.trg_pad_idx = trg_pad_idx self.device = device def make_src_mask(self, src): src_mask = (src != self.src_pad_idx).unsqueeze(1).unsqueeze(2) # (N, 1, 1, src_len) return src_mask.to(self.device) def make_trg_mask(self, trg): trg_mask = torch.tril(torch.ones((seq_length, seq_length))).expand(N, 1, seq_length, seq_length) # (N, 1, trg_len, trg_len) return trg_mask.to(self.device)View Code
不知道对不对,写到一般次数受限了(直接写Transformer了)
6. 聊天
实现一个聊天机器人,给出测试样例
# 使用Python调用BERT实现聊天机器人import torchfrom transformers import BertTokenizer, BertForQuestionAnswering# 加载预训练的BERT模型tokenizer = BertTokenizer.from_pretrained("bert-base-chinese")model = BertForQuestionAnswering.from_pretrained("bert-base-chinese")# 回答问题的函数def answer_question(question, answer_text): # 将问题和答案文本编码为输入ID input_ids = tokenizer.encode(question, answer_text) tokens = tokenizer.convert_ids_to_tokens(input_ids) # 确定问题和答案文本的分段ID sep_index = input_ids.index(tokenizer.sep_token_id) num_seg_a = sep_index + 1 num_seg_b = len(input_ids) - num_seg_a segment_ids = [0]*num_seg_a + [1]*num_seg_b assert len(segment_ids) == len(input_ids) # 使用BertForQuestionAnswering模型预测答案的开始和结束位置 outputs = model(torch.tensor([input_ids]), token_type_ids=torch.tensor([segment_ids])) start_scores = outputs.start_logits end_scores = outputs.end_logits # 找到具有最高“start”和“end”分数的标记 answer_start = torch.argmax(start_scores) answer_end = torch.argmax(end_scores) # 将标记转换回字符串 answer = tokens[answer_start] for i in range(answer_start + 1, answer_end + 1): if tokens[i][0:2] == "##": answer += tokens[i][2:] else: answer += " " + tokens[i] return answerView Code
# Here is an example of how to use the answer_question function:question = "什么是BERT?"answer_text = "BERT是一种预训练语言模型,可以用于各种自然语言处理任务。"answer = answer_question(question, answer_text)print(answer)# Output: "一种预训练语言模型"
解释
BertForQuestionAnswering是一个预训练的BERT模型,用于回答问题。它是transformers库中的一个类,可以通过from_pretrained方法加载预训练的模型。在给定问题和答案文本的情况下,answer_question函数使用BertTokenizer将文本编码为输入ID,并使用BertForQuestionAnswering模型预测答案的开始和结束位置。然后,它将这些位置转换回标记并返回答案字符串。
直接调包,好像没理解问题,给出QA了。
7. 矩阵乘法
基于C++实现通用矩阵乘法,并给出多个测试样例。要求如下:- 矩阵乘法需要实现分块乘法- 需要根据矩阵的稀疏情况进行优化
#includeView Code#include #include using namespace std;const int BLOCK_SIZE = 32;void gemm(vector >& A, vector >& B, vector >& C) { int n = A.size(), m = B.size(), p = B[0].size(); for (int i = 0; i < n; i += BLOCK_SIZE) { for (int j = 0; j < p; j += BLOCK_SIZE) { for (int k = 0; k < m; k += BLOCK_SIZE) { for (int ii = i; ii < min(n, i + BLOCK_SIZE); ii++) { for (int jj = j; jj < min(p, j + BLOCK_SIZE); jj++) { for (int kk = k; kk < min(m, k + BLOCK_SIZE); kk++) { C[ii][jj] += A[ii][kk] * B[kk][jj]; } } } } } }}void optimize_gemm(vector >& A, vector >& B, vector >& C) { int n = A.size(), m = B.size(), p = B[0].size(); vector > Bt(p, vector (m)); for (int i = 0; i < p; i++) { for (int j = 0; j < m; j++) { Bt[i][j] = B[j][i]; } } for (int i = 0; i < n; i += BLOCK_SIZE) { for (int j = 0; j < p; j += BLOCK_SIZE) { for (int k = 0; k < m; k += BLOCK_SIZE) { for (int ii = i; ii < min(n, i + BLOCK_SIZE); ii++) { for (int jj = j; jj < min(p, j + BLOCK_SIZE); jj++) { double sum = 0; for (int kk = k; kk < min(m, k + BLOCK_SIZE); kk++) { sum += A[ii][kk] * Bt[jj][kk]; } C[ii][jj] += sum; } } } } }}int main() { vector > A = {{1, 2, 3}, {4, 5, 6}}; vector > B = {{7, 8}, {9, 10}, {11, 12}}; vector > C1(2, vector (2)), C2(2, vector (2)); gemm(A, B, C1); optimize_gemm(A, B, C2); cout << "C1:" << endl; for (auto row : C1) { for (auto x : row) { cout << x << " "; } cout << endl; } cout << "C2:" << endl; for (auto row : C2) { for (auto x : row) { cout << x << " "; } cout << endl; } return 0;}
可运行
用python写一个基于多线程的并行矩阵乘法,并给出测试样例
import numpy as npimport threading# Define a function to perform matrix multiplicationdef matmul(A, B, C, i, j): C[i][j] = np.dot(A[i], B[:,j])# Define a function to perform parallel matrix multiplicationdef parallel_matmul(A, B, num_threads): # Get the shape of the matrices m, n = A.shape p, q = B.shape # Check if the matrices can be multiplied if n != p: print("Matrices cannot be multiplied") return None # Create a matrix to store the result C = np.zeros((m, q)) # Create a list to store the threads threads = [] # Create the threads for i in range(m): for j in range(q): thread = threading.Thread(target=matmul, args=(A, B, C, i, j)) threads.append(thread) # Start the threads for i in range(num_threads): for j in range(i, len(threads), num_threads): threads[j].start() # Wait for the threads to finish for thread in threads: thread.join() # Return the result return C# Test the functionA = np.array([[1, 2], [3, 4], [5, 6]])B = np.array([[7, 8], [9, 10]])C = parallel_matmul(A, B, 2)print(C)View Code
可运行
8. 可视化
获取贵州茅台从2018年元旦至2023年3月20日的行情数据,计算MACD指标
import tushare as tsimport matplotlib.pyplot as pltfrom mpl_finance import candlestick_ochlfrom matplotlib.pylab import date2numimport pandas as pdimport numpy as np# 获取贵州茅台从2018年元旦至2023年3月20日的行情数据df = ts.get_k_data("600519", start="2018-01-01", end="2023-03-20")# 计算MACD指标def MACD(df, fastperiod=12, slowperiod=26, signalperiod=9): ewma12 = pd.Series.ewm(df["close"], span=fastperiod).mean() ewma26 = pd.Series.ewm(df["close"], span=slowperiod).mean() dif = ewma12 - ewma26 dea = pd.Series.ewm(dif, span=signalperiod).mean() macd = (dif - dea) * 2 return dif, dea, macddif, dea, macd = MACD(df)# 画图展示df["time"] = pd.to_datetime(df["date"])df["time"] = df["time"].apply(date2num)df = df[["time", "open", "close", "high", "low"]]fig, ax = plt.subplots(figsize=(20, 10))candlestick_ochl(ax, df.values, width=0.6, colorup="red", colordown="green", alpha=0.8)plt.plot(dif, label="DIF", color="blue")plt.plot(dea, label="DEA", color="orange")plt.bar(macd.index, macd, label="MACD", color="purple")plt.legend()plt.title("Guizhou Maotai Stock Price with MACD")plt.xlabel("Date")plt.ylabel("Price")plt.show()View Code
可运行
9.WEB
用python爬取成都的7天内的天气,转换成WEB程序:- 使用Python Flask编写后端代码- 前端页面静态页面, 展示天气图标
from flask import Flask, render_template_stringimport requestsfrom bs4 import BeautifulSoupapp = Flask(__name__)@app.route("/")def home(): # Scrape weather data from website url = "https://www.weather.com/zh-CN/weather/5day/l/CHXX0006:1:CH" response = requests.get(url) print("Start to crawling") soup = BeautifulSoup(response.content, "html.parser") days = soup.find_all("h3", class_="DailyContent--daypartName--3G5Y8") temps = soup.find_all("div", class_="DailyContent--temp--_8DL5") icons = soup.find_all("img", class_="DailyContent--icon--2s5M5") weather_data = [] for i in range(len(days)): day = days[i].text temp = temps[i].text icon = icons[i]["alt"] weather_data.append({"day": day, "temp": temp, "icon": icon}) print(weather_data) # Render template with weather data template = """Weather App 5-Day Weather Forecast
Day | Temperature | Icon |
---|---|---|
{{ data.day }} | {{ data.temp }} | {{ data.icon }} |
爬虫有问题,程序有的包都没导入。
10.电机控制
用C语言写一个控制直流电机的算法。要求包括启动电机、多档加速电机、停止电机、基于PID算法对电机速度进行控制View Code
不知道对不对
11.Verilog HDL
加法器 乘法器 矩阵乘法
// This is a Verilog code for a simple addermodule adder(input a, input b, output sum); assign sum = a + b;endmodule// This is a Verilog code for a simple multipliermodule multiplier(input a, input b, output mult); assign mult = a * b;endmodule// This is a Verilog code for a matrix multiplication modulemodule matrix_multiplier( input [3:0][3:0] matrix_A, // 4x4 matrix A input [3:0][3:0] matrix_B, // 4x4 matrix B output [3:0][3:0] matrix_C // 4x4 matrix C = A * B);genvar i, j, k;wire [7:0] temp;generate // Multiply each element of matrix A with corresponding element of matrix B // and accumulate the results to get each element of matrix C for (i = 0; i < 4; i = i + 1) begin : ROWS_C for (j = 0; j < 4; j = j + 1) begin : COLS_C assign temp = 8"h0; for (k = 0; k < 4; k = k + 1) begin : ELEMENTS_A_B assign temp = temp + matrix_A[i][k] * matrix_B[k][j]; end assign matrix_C[i][j] = temp; end endendgenerateendmoduleView Code
不知道对不对
12.线程池
用Java写一个线程池,并给出测试样例
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPoolExample { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(5); for (int i = 0; i < 10; i++) { Runnable worker = new WorkerThread("" + i); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println("Finished all threads"); }}class WorkerThread implements Runnable { private String message; public WorkerThread(String s) { this.message = s; } public void run() { System.out.println(Thread.currentThread().getName() + " (Start) message = " + message); processMessage(); System.out.println(Thread.currentThread().getName() + " (End)"); } private void processMessage() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } }}View Code
不知道对不对
总结比较强大的全能代码写手,但是需要设置合适的问题。能够完成初级问题解答,有时可能比搜索快。关键词:
-
每日快播:React onBlur回调中使用document.activeElement返回body解决方案
React在onBlur函数中调用document activeElement总是返回body,得不到新的聚焦元素怎么办?
来源: 如何获取软件包的下载地址 wget curl
基于GPT3的代码编辑器Cursor试用-你的智能代码编辑助手
每日快播:React onBlur回调中使用document.activeElement返回body解决方案
世界观察:民营天龙二号液体火箭首飞成功:还隐藏了一个中国第一
天天看热讯:不知道这几点!你买电动牙刷就是花冤枉钱
世界热资讯!热泵干衣机被严重低估了!浑身都是宝
环球快播:致聂红的一封信
环球快报:2023上海车展丨这些即将首发的热门新车你一定不要错过!
云南泼水节白天是热闹夜晚是浪漫:市民游客共狂欢
员工回应公司发布高薪招聘老板公告:不是开玩笑
热推荐:java -- File类和递归
贾跃亭憋了九年的车终于量产?结果 又一张大饼!
环球快报:四边等宽的鸿蒙手机来了!华为nova 11明天发
潍坊风筝节放飞打工人的心声:引发网友热议
Intel鸡血驱动暴涨63%!Arc A750性价比秒杀RTX 3060 72%!
当前最新:天舟六号飞船、长征七号火箭抵达文昌!五次发射 100%成功
山东泰山队vs上海申花首发出炉:四外援先发,韩镕泽镇守球门
每日热讯!“泼水节被撕扯雨衣”女生发声:很崩溃
世界快报:众人狂欢!泼水节连狗路过都得淋两桶水再走
贝贝健电蚊香液到手14.9元:驱赶蚊虫神器 夏天必备
全球信息:超级SSD 21合一组成168TB!价格直奔20万元
全球视点!扎实打牢数据结构算法根基,从此不怕算法面试系列之001 week01 02-01 什么是算法?
焦点快报!女子花2万为猫移植鱼皮被网暴 本人:救治一半不可能放弃
全球热头条丨女子澄清妈妈做月嫂存款482万为虚构:觉得好玩就发了 现在很后悔
环球热文:养男三不碰,养女三讲究,教出来的孩子才能立足社会!
世界信息:Air724UG开发板串口教程
首发宁德时代麒麟电池 极氪009 ME版正式交付:根治续航焦虑
环球百事通!超智驾轿跑SUV!小鹏G6将亮相上海车展
学系统集成项目管理工程师(中项)系列06b_信息系统安全管理(下)
【打怪升级】【微服务】聊聊微服务拆分设计
微头条丨《世界尽头的咖啡馆》-热衷于有意义的事,追求真我。
31-触发器01
2023年4月自考《人力资源管理(一)》真题答案汇总
卡普空更新出尔反尔:突然移除《生化危机2/3》Steam版光追
世界速看:奥利奥礼包到手39元:夹心饼干、巧脆卷全都有
环球看点!Vulnhub Fall Walkthrough
环球消息!苹果在线服务又出Bug:用户被迫反复输入Apple ID
每日视点!余承东罕见认错:还连甩五项重磅技术更新
《灌篮高手》赤木刚宪预告 大猩猩怒吼霸气登场
每日看点!Visual Studio Code开发常用的工具栏选项,查看源码技巧以及【vscode常用的快捷键】
环球快资讯丨也门和平进程迎来重要机遇
环球时讯:《流浪地球2》网播热度不减:上线后全网热度登顶
今日热文:深入理解 JVM ------ 调优案例分析与实战
观热点:1年内5名机车网红车祸身亡:靠摩托车来吸睛涨粉 已成“流量密码”
世界热点评!当心寄生虫!女生15元买15个螺肉疑为福寿螺 央视科普如何区分
FreeSWITCH添加iLBC编码及转码
世界最资讯丨负数有奇数和偶数吗_奇数和偶数是什么意思
华为鸿蒙HarmonyOS 4.0来了!余承东确定秋天发布
世界热点评!熊猫“蔓越煤”胡萝卜卡喉 饲养员海姆立克法施救:不愧是“生命的拥抱”
最新消息:《3D编程模式》写书-第3次记录
全球焦点!推荐给你让人震惊的网站集合
快看点丨迪士尼《小美人鱼》黑人鱼主演发声反对霸凌:要和我一样可爱
天天实时:国内首颗主动降水测量卫星成功发射!中国又拿下全球唯一
每日短讯:中国人民银行行长易纲会见印度尼西亚财长英卓华
【焦点热闻】使用 APT-mirror 四步配置 Ubuntu 本地软件仓库
环球快播:【见•闻】日本汽车界对合成燃料汽车赛道寄予厚望
最资讯丨超越“乔帮主” 库克成苹果任职时间最长的CEO
环球信息:存失速、起火风险!奔驰中国召回近25万辆汽车
天天滚动:清明过后教你美味简单的几道家常菜,好吃不容错过,上桌抢着吃
今亮点!14nm以上EDA工具国产化!华为:完成了软件/硬件开发78款工具的替代
每日热议!特斯拉降价是为打“价格战”?马斯克否认并透露原因
世界即时:女子鱼刺卡喉花1265元取出直呼贵!提醒:切勿自行处理
环球焦点!day01-Redis入门
销量大跌 资金链断裂!广汽本田一4S店老板“跑路”
坐燃油车从来不晕车 为何坐电动车很容易晕车?
每日精选:欧拉发布1080°女性安全架构:刹车自动增强、一键紧急呼救
环球要闻:华为4月17日首发全液冷超充架构 充电桩功率“遥遥领先”
今日视点:中国人自己的技术!比亚迪云辇-X实测:三个轮子行驶、过弯超平稳
今头条!卢拉访华:在国际外交舞台上演“王者归来”
预训练模型-从BERT原理到BERT调包和微调
【世界热闻】我的第一个项目(十) :处理全局变量(解决模块化后变量无法获取的问题)
【环球新视野】2023年Rust发展如何?
环球速看:可怕一幕!男子骑摩托车遭风筝线勒喉受伤 官方科普风筝线比刀还锋利
焦点信息:男子未悬挂号牌 竟是嫌老婆选的“250”车牌太丢人
【天天播资讯】为鼓励走出家门:韩国为宅男宅女每月发3400元补贴 网友直呼羡慕
世界热议:解决国产手机厂商5G卡脖子:国产射频滤波器搞定了 年产12万片项目落地
世界热头条丨女子吐槽领导隔监控点名员工加班 大家为工作不敢反抗:网友唏嘘
【世界聚看点】广东家常菜名字_广东家常菜
全球播报:socat的下载和基础使用
天天热文:比亚迪上海车展几号展台公布:比亚迪百万豪车在这里
当前焦点!2023LPL春季赛总决赛落幕 JDG 3:1击败BLG问鼎总冠军
全球最资讯丨宣传防盗、防电诈,送反诈螺蛳粉!柳州警方为企业守平安
【Visual Leak Detector】VS 中 VLD 输出解析
当前通讯!upload-labs writeup
每日视点!1克燃料等于8吨石油 日本明确首个核聚变战略:2050年发电
曝淄博酒店网上标价千元前台仅200元引热议:官方回应
全球关注:首发仅1899元!铁威马F4-423(4G)四盘位NAS开售
【Visual Leak Detector】在 VS 2015 中使用 VLD
世界关注:WTT新乡冠军赛:孙颖莎获女单冠军
全球百事通!智己NOA体验:高速上我撒手半个小时 回过头竟还在人寰
环球热推荐:网易云音乐上线“鲸云母带”音质:一首歌170MB SVIP专享
视焦点讯!如何快速而优雅的解决问题(提问的智慧简略版)
天天讯息:cin与CTRL+z的问题
世界快看点丨如何防止设备被重复控制
【环球快播报】破纪录!西班牙女子洞穴生活500多天 回地面称“不想出来”
世界热消息:被海鸥圈粉 沈义人:感觉要购入第一台比亚迪了
每日精选:成龙被观众当场要求退票上热搜 电影《龙马精神》回应:起诉造谣账号
最新资讯:几十秒看完10分钟的视频 就靠这AI输入法:日语也不怕
快讯:唯一全面实现国产化!低端低价的1LCD爆发:超DLP成智能投影仪主流
当前热议!6错误代码C3848.