主頁 >  其他 > 基于GPT3.5的代碼編輯器Cursor試用-你的智能代碼編輯助手

基于GPT3.5的代碼編輯器Cursor試用-你的智能代碼編輯助手

2023-04-17 08:06:29 其他

一、安裝和使用

官網下載安裝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
import 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()
View Code

 可以運行

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=https://www.cnblogs.com/justLittleStar/archive/2023/04/16/(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())
View Code

3. 聚類

 k-means: 用Python實作K-means演算法,并在iris資料集上進行測驗,最后對結果進行可視化
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()
View Code

 

GMM: 用numpy實作GMM演算法,給出測驗樣例

# Welcome to Cursor
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal

# Generate some random data
np.random.seed(0)
n_samples = 300
mean1 = [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 convergence
n_clusters = 2
tolerance = 0.01

# Initialize the means, covariances, and mixing coefficients
means = 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 responsibilities
def 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 coefficients
def 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 algorithm
log_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 clusters
plt.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 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 = https://www.cnblogs.com/justLittleStar/archive/2023/04/16/pd.read_csv('world_population.csv', index_col=0)
data.index = pd.to_datetime(data.index)

# Train-test split
train_data = https://www.cnblogs.com/justLittleStar/archive/2023/04/16/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()
View Code

 

不知道對不對,訓練代碼都給出了

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)
View Code

 

不知道對不對,寫到一般次數受限了(直接寫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
View 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++實作通用矩陣乘法,并給出多個測驗樣例,要求如下: - 矩陣乘法需要實作分塊乘法 - 需要根據矩陣的稀疏情況進行優化
#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;
}
View Code

 可運行

 

用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)
View Code

可運行

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()
View Code

 可運行

 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=https://www.cnblogs.com/justLittleStar/archive/2023/04/16/weather_data)

if __name__ == '__main__':
    app.run()
View Code

爬蟲有問題,程式有的包都沒匯入,

10.電機控制

用C語言寫一個控制直流電機的演算法,要求包括啟動電機、多檔加速電機、停止電機、基于PID演算法對電機速度進行控制
View Code

 不知道對不對

11.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
View 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

 不知道對不對

  總結比較強大的全能代碼寫手,但是需要設定合適的問題, 能夠完成初級問題解答,有時可能比搜索快,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/550262.html

標籤:其他

上一篇:界面重建——Marching cubes演算法

下一篇:KMeans演算法與GMM混合高斯聚類

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more