Cursor使用
安装
- Cursor官网)
- 下载后直接安装
使用
- 使用
Ctrl+K
实现提出需求生成代码
示例
提出问题
- 利用pytorch生成一个在gpu上实现对猫狗图像分类的cnn网络模型并实现训练
生成的代码
import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader from torchvision import datasets, transforms # Check if a GPU is available device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Define transforms for the training and validation sets train_transforms = transforms.Compose([ transforms.RandomRotation(30), transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) val_transforms = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) # Load the datasets with ImageFolder train_dataset = datasets.ImageFolder("train/", transform=train_transforms) val_dataset = datasets.ImageFolder("val/", transform=val_transforms) # Define the dataloaders train_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True) val_dataloader = DataLoader(val_dataset, batch_size=64, shuffle=True) # Define the CNN model class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(3,32,kernel_size=3, padding=1) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.fc1 = nn.Linear(128 * 28 * 28, 512) self.fc2 = nn.Linear(512, 2) self.dropout = nn.Dropout(p=0.5) self.relu = nn.ReLU() self.device = device def forward(self, x): x = self.pool(self.relu(self.conv1(x))) x = self.pool(self.relu(self.conv2(x))) x = self.pool(self.relu(self.conv3(x))) x = x.view(-1, 128 * 28 * 28) x = self.dropout(self.relu(self.fc1(x))) x = self.fc2(x) return x model=CNN() # Define the loss function and optimizer criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.01) # Move the model to the GPU model.to(device) # Train the model num_epochs = 10 for epoch in range(num_epochs): train_loss = 0.0 val_loss = 0.0 for images, labels in train_dataloader: # Move the data to the GPU images, labels = images.to(device), labels.to(device) optimizer.zero_grad() outputs = model(images) loss = criterion(outputs, labels) loss.backward() optimizer.step() train_loss += loss.item() * images.size(0) train_loss = train_loss / len(train_dataloader.dataset) model.eval() for images, labels in val_dataloader: # Move the data to the GPU images, labels = images.to(device), labels.to(device) outputs = model(images) loss = criterion(outputs, labels) val_loss += loss.item() * images.size(0) val_loss = val_loss / len(val_dataloader.dataset) print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format( epoch + 1, train_loss, val_loss )) # Save the model weights torch.save(model.state_dict(), "model_weights.pth")
- 中间遇到了一些问题在生成的时候它到后面会停止需要把没生成完的呢一行代码删掉然后使用
Ctrl+K
输入继续
的指令让他继续生成 - 需要需求很明确,要求写的详细才可以生成较为准确的代码
- 中间遇到了一些问题在生成的时候它到后面会停止需要把没生成完的呢一行代码删掉然后使用
运行
- 运行时遇到了一些问题它少了一个
model=CNN()
- 可以使用
Ctrl+L
把报错信息粘贴上去提问
- 运行时遇到了一些问题它少了一个
运行结果
总结
优点:
- 可以节约时间,有问题直接提问就会得到答案
- 可以快速帮助理解代码,并解决运行时遇到的问题
缺点:
- 如果问题模糊无法得到较理想的代码
- 在生成时会出现生成一半停止的问题需要输入
继续
才可以继续生成
感受
- Chat GPT的发布是颠覆时代的,用好它可以大大提高生产力,不用重复造轮子。熟练的使用AI辅助可以提高竞争优势。