一、安装和使用
官网下载安装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 modules
import pygame,random
# Initialize Pygame
pygame.init()
# Set up the game window
WINDOW\_WIDTH = 800
WINDOW\_HEIGHT = 600
WINDOW = pygame.display.set\_mode((WINDOW\_WIDTH, WINDOW\_HEIGHT))
pygame.display.set\_caption("Ping Pong Game")
# Set up the colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up the fonts
FONT = pygame.font.SysFont(None, 48)
# Set up the ball
BALL\_RADIUS = 10
ball\_x = WINDOW\_WIDTH // 2
ball\_y = WINDOW\_HEIGHT // 2
ball\_dx = 5 \* random.choice(\[-1, 1\])
ball\_dy = 5 \* random.choice(\[-1, 1\])
# Set up the paddles
PADDLE\_WIDTH = 10
PADDLE\_HEIGHT = 100
player\_paddle\_x = WINDOW\_WIDTH - PADDLE\_WIDTH - 10
player\_paddle\_y = WINDOW\_HEIGHT // 2 - PADDLE\_HEIGHT // 2
computer\_paddle\_x = 10
computer\_paddle\_y = WINDOW\_HEIGHT // 2 - PADDLE\_HEIGHT // 2
paddle\_speed = 5
# Set up the scores
player\_score = 0
computer\_score = 0
# Set up the game loop
game\_running = False
game\_ended = False
clock = 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 Pygame
pygame.quit()
可以运行
2. 手写数字识别
用Python写一个基于MINIST数据集的手写数字识别代码,并给出测试样例
import numpy as np import tensorflow as tf from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential from 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 1 x\_train = x\_train / 255.0 x\_test = x\_test / 255.0 # Reshape input data to have a single channel x\_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 model model = 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 model model.compile(optimizer='adam', loss='sparse\_categorical\_crossentropy', metrics=\['accuracy'\]) # Train model model.fit(x\_train, y\_train, epochs=5, validation\_data=(x\_test, y\_test)) # Visualize test example # Visualize test example import matplotlib.pyplot as plt image\_index = 4444 plt.imshow(x\_test\[image\_index\].reshape(28, 28),cmap='Greys') plt.show() # Predict test example pred = model.predict(x\_test\[image\_index\].reshape(1, 28, 28, 1)) print('Predicted value:', pred.argmax())
3. 聚类
k-means: 用Python实现K-means算法,并在iris数据集上进行测试,最后对结果进行可视化
GMM: 用numpy实现GMM算法,给出测试样例
from sklearn.datasets import load\_iris
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# load iris dataset
iris = load\_iris()
X = iris.data
# initialize KMeans model with 3 clusters
kmeans = KMeans(n\_clusters=3)
# fit the model to the data
kmeans.fit(X)
# predict the clusters for each data point
y\_kmeans = kmeans.predict(X)
# plot the clusters
plt.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()
4. RNN
用分别用Pytorh和Numpy写一个RNN网络,然后在一个1960年到2022年世界总人口树的时序数据上对它们进行测试。要求:
-对比Arima算法
-对数据进行可视化
-用1960年到2012年预测2013年到2022年,对预测结果可视化
\# PyTorch RNN network
import torch
import torch.nn as nn
class 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 network
import numpy as np
class 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 data
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima\_model import ARIMA
# Load data
data = pd.read\_csv('world\_population.csv', index\_col=0)
data.index = pd.to\_datetime(data.index)
# Train-test split
train\_data = data.loc\['1960':'2012'\]
test\_data = data.loc\['2013':'2022'\]
# ARIMA model
model = 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 model
input\_size = 1
hidden\_size = 32
output\_size = 1
pytorch\_rnn = RNN(input\_size, hidden\_size, output\_size)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(pytorch\_rnn.parameters(), lr=0.01)
# Train PyTorch RNN model
for 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 model
pytorch\_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 model
input\_size = 1
hidden\_size = 32
output\_size = 1
numpy\_rnn = NumpyRNN(input\_size, hidden\_size, output\_size)
# Train Numpy RNN model
loss\_fn = nn.MSELoss()
learning\_rate = 0.01
for 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 model
numpy\_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 results
plt.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()
不知道对不对,训练代码都给出了
5. 自注意力
用Pytorch实现自注意力网络,并进行测试
import torch
import torch.nn as nn
import torch.nn.functional as F
class 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 out
class 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 out
class 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 out
class 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 out
class 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 out
class 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)
不知道对不对,写到一般次数受限了(直接写Transformer了)
6. 聊天
实现一个聊天机器人,给出测试样例
\# 使用Python调用BERT实现聊天机器人
import torch
from 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 answer
\# 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++实现通用矩阵乘法,并给出多个测试样例。要求如下:
- 矩阵乘法需要实现分块乘法
- 需要根据矩阵的稀疏情况进行优化
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int BLOCK\_SIZE = 32; void gemm(vector<vector<double>>& A, vector<vector<double>>& B, vector<vector<double>>& 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<vector<double>>& A, vector<vector<double>>& B, vector<vector<double>>& C) { int n = A.size(), m = B.size(), p = B\[0\].size(); vector<vector<double>> Bt(p, vector<double>(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<vector<double>> A = {{1, 2, 3}, {4, 5, 6}}; vector<vector<double>> B = {{7, 8}, {9, 10}, {11, 12}}; vector<vector<double>> C1(2, vector<double>(2)), C2(2, vector<double>(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 np
import threading
# Define a function to perform matrix multiplication
def matmul(A, B, C, i, j):
C\[i\]\[j\] \= np.dot(A\[i\], B\[:,j\])
# Define a function to perform parallel matrix multiplication
def 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 function
A = np.array(\[\[1, 2\], \[3, 4\], \[5, 6\]\])
B \= np.array(\[\[7, 8\], \[9, 10\]\])
C \= parallel\_matmul(A, B, 2)
print(C)
8. 可视化
获取贵州茅台从2018年元旦至2023年3月20日的行情数据,计算MACD指标
import tushare as ts
import matplotlib.pyplot as plt
from mpl\_finance import candlestick\_ochl
from matplotlib.pylab import date2num
import pandas as pd
import 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, macd
dif, 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()
可运行
9.WEB
用python爬取成都的7天内的天气,转换成WEB程序:
- 使用Python Flask编写后端代码
- 前端页面静态页面, 展示天气图标
from flask import Flask, render\_template\_string
import requests
from bs4 import BeautifulSoup
app = 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 = """
<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
</head>
<body>
<h1>5-Day Weather Forecast</h1>
<table>
<thead>
<tr>
<th>Day</th>
<th>Temperature</th>
<th>Icon</th>
</tr>
</thead>
<tbody>
{% for data in weather\_data %}
<tr>
<td>{{ data.day }}</td>
<td>{{ data.temp }}</td>
<td>{{ data.icon }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
"""
return render\_template\_string(template, weather\_data=weather\_data)
if \_\_name\_\_ == '\_\_main\_\_':
app.run()
爬虫有问题,程序有的包都没导入。
10.Verilog HDL
加法器 乘法器 矩阵乘法
// This is a Verilog code for a simple adder
module adder(input a, input b, output sum);
assign sum = a + b;
endmodule
// This is a Verilog code for a simple multiplier
module multiplier(input a, input b, output mult);
assign mult = a \* b;
endmodule
// This is a Verilog code for a matrix multiplication module
module 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
end
endgenerate
endmodule
不知道对不对
11.线程池
用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();
}
}
}
不知道对不对
总结比较强大的全能代码写手,但是需要设置合适的问题。能够完成初级问题解答,有时可能比搜索快。
最后的最后
感谢你们的阅读和喜欢,我收藏了很多技术干货,可以共享给喜欢我文章的朋友们,如果你肯花时间沉下心去学习,它们一定能帮到你。
因为这个行业不同于其他行业,知识体系实在是过于庞大,知识更新也非常快。作为一个普通人,无法全部学完,所以我们在提升技术的时候,首先需要明确一个目标,然后制定好完整的计划,同时找到好的学习方法,这样才能更快的提升自己。
这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费
】
一、全套AGI大模型学习路线
AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!
二、640套AI大模型报告合集
这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。
三、AI大模型经典PDF籍
随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。
四、AI大模型商业化落地方案
五、面试资料
我们学习AI大模型必然是想找到高薪的工作,下面这些面试题都是总结当前最新、最热、最高频的面试题,并且每道题都有详细的答案,面试前刷完这套面试题资料,小小offer,不在话下。
这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费
】