复现报错小结

1、os.environ[‘CUDA_VISIBLE_DEVICES’] = arg.cuda_visible_device报错

os.environ['CUDA_VISIBLE_DEVICES'] = arg.cuda_visible_device这句报错的时候,挪到下面这两句前面。

import torch
import torch.nn as nn

2、查看显卡使用情况

nvidia-smi // 查看显卡使用情况

3、zipfile.BadZipFile: File is not a zip file

正常情况下是文件路径错误/文件格式错误/文件损坏,这里有可能是数据集处理时候出现错误,重新生成.npz文件即可。

4、AttributeError: module ‘numpy’ has no attribute ‘int’.

在这里插入图片描述
将np.int替换成int,将np.float32替换成float。
或将np.int替换成np.int32,np.float32不变。

5、ModuleNotFoundError:No module named xxx

在这里插入图片描述
2)
请添加图片描述
请添加图片描述

pip install scikit-learn

3)

ModuleNotFoundError: No module named 'yaml'
pip install pyyaml

6、循环导包的问题

解决办法:将导包在步骤里面执行,将init的导包冲突的挪到其他执行到的步骤中去

7、将conda环境中的Python降版本

查看已安装的python版本

conda list python

创建新的python环境

conda create --name new_env python=3.8

激活新的python环境

conda active new_env

安装依赖包

conda install --file requirements.txt

查看python版本

python -- verson

8、pytorch

查看pytorch版本

pip3 show torch

降低pytorch版本

pip install torch==1.11.0+cu113 -f https://download.pytorch.org/whl/torch_stable.html

9、tensorboardX

查看TensorBoardX版本

pip show tensorboardX

10、raise ParserError

可能是config文件里的缩进问题。

11、apex

ImportError: cannot import name ‘UnencryptedCookieSessionFactoryConfig’ from ‘pyramid.session’ (unknown location)

解决方法:
版本不对,或者说pip之后的包就不是一个东西,先用pip 卸载掉apex然后从nVidia官网上下载:

pip uninstall apex
git clone https://github.com/NVIDIA/apex
cd apex
python setup.py install

参考博客

12、GPU compute capability(算力)和CUDA版本冲突问题

RuntimeError: CUDA error: no kernel image is available for execution on the device CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

GeForce RTX 3090 with CUDA capability sm_86 is not compatible with the current PyTorch installation. The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_61 sm_70 sm_75 compute_37.

大概意思是说当前GPU的算力与当前版本的Pytorch依赖的CUDA算力不匹配(3090算力为8.6,而当前版本的pytorch依赖的CUDA算力仅支持3.7,5.0,6.0,6.1,7.0,7.5)

· 更换更高版本的CUDA或pytorch版本。注意pytorch版本和CUDA版本也要相匹配。

13、VSCode导入正确的包却无法解析

检查右下角的环境是否选对
在这里插入图片描述

14、RuntimeError: Unable to find a valid CUDNN algorithm to run convolution

减小batchsize

15、RuntimeError: Too many open files. Communication with the workers is no longer possible. Please increase the limit using ulimit -n in the shell or change the sharing strategy by calling torch.multiprocessing.se t_sharing_strategy(‘file_system’) at the beginning of your code

在前面加入以下代码:

import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小样本学习是指在样本量较少的情况下进行模型训练,常用于人脸识别、语音识别等领域。其中一种经典的方法是Siamese Network,这种网络结构可以通过比较两个输入之间的相似度来进行分类。 以下是使用PyTorch实现Siamese Network的代码示例: ```python import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import Dataset, DataLoader class SiameseNetwork(nn.Module): def __init__(self): super(SiameseNetwork, self).__init__() self.conv = nn.Sequential( nn.Conv2d(1, 64, 10), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 7), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(128, 128, 4), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(128, 256, 4), nn.ReLU() ) self.fc = nn.Sequential( nn.Linear(256 * 6 * 6, 4096), nn.Sigmoid(), nn.Linear(4096, 1) ) def forward_once(self, x): output = self.conv(x) output = output.view(output.size()[0], -1) output = self.fc(output) return output def forward(self, input1, input2): output1 = self.forward_once(input1) output2 = self.forward_once(input2) return output1, output2 class SiameseDataset(Dataset): def __init__(self, data, labels): self.data = data self.labels = labels def __getitem__(self, index): return self.data[index][0], self.data[index][1], self.labels[index] def __len__(self): return len(self.data) def train(model, train_loader, criterion, optimizer, device): model.train() running_loss = 0.0 for i, data in enumerate(train_loader, 0): input1, input2, labels = data input1, input2, labels = input1.to(device), input2.to(device), labels.to(device) optimizer.zero_grad() output1, output2 = model(input1, input2) loss = criterion(output1, output2, labels) loss.backward() optimizer.step() running_loss += loss.item() return running_loss / len(train_loader) def test(model, test_loader, criterion, device): model.eval() with torch.no_grad(): running_loss = 0.0 for i, data in enumerate(test_loader, 0): input1, input2, labels = data input1, input2, labels = input1.to(device), input2.to(device), labels.to(device) output1, output2 = model(input1, input2) loss = criterion(output1, output2, labels) running_loss += loss.item() return running_loss / len(test_loader) if __name__ == "__main__": # 数据集中每个样本由两个图像和一个标签组成,标签表示两个图像是否属于同一类别 data = [] labels = [] # TODO: 加载数据集并将数据和标签分别存储到data和labels中 dataset = SiameseDataset(data, labels) train_loader = DataLoader(dataset, batch_size=16, shuffle=True, num_workers=4) model = SiameseNetwork() criterion = nn.BCEWithLogitsLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) num_epochs = 10 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") for epoch in range(num_epochs): train_loss = train(model, train_loader, criterion, optimizer, device) test_loss = test(model, test_loader, criterion, device) print("Epoch {}/{}: Train Loss: {:.4f}, Test Loss: {:.4f}".format(epoch+1, num_epochs, train_loss, test_loss)) ``` 在上述代码中,我们首先定义了一个Siamese Network模型,它由卷积层和全连接层组成。然后定义了一个SiameseDataset类来加载数据集,并使用DataLoader来对数据进行批量处理。接下来定义了train和test函数来训练和测试模型。最后,在主函数中加载数据集并进行模型训练和测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值