pytorch数据集的一些问题

按照提示把train_data和test_data改成data,把train_labels和test_labels改成targets即可解决。

这里有点我没想清楚的地方是,data和targets里是怎么区分train和test的?想清楚了再来补充

想清楚了,来更新一下:

我这里的代码是

X_train = train_loader.dataset.data.numpy()
y_train = train_loader.dataset.targets.numpy()
X_test = test_loader.dataset.data[:1000].numpy()
y_test = test_loader.dataset.targets[:1000].numpy()

test_loader和train_loader又是哪里来的呢?

MNIST dataset
train_dataset = dsets.MNIST(root = '/ml/pymnist', #选择数据的根目录
                           train = True, # 选择训练集
                           transform = None, #不考虑使用任何数据预处理
                           download = True) # 从网络上download图片
test_dataset = dsets.MNIST(root = '/ml/pymnist', #选择数据的根目录
                           train = False, # 选择测试集
                           transform = None, #不考虑使用任何数据预处理
                           download = True) # 从网络上download图片
#加载数据
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=True)

train_loader里用的dataset是train_dataset,而test_loader里用的dataset是test_dataset,两个dataset在定义时使用的是训练集和测试集,所以要区分data和targets是属于train还是test只要看前面的loader是train还是test就知道了!

原因:

两个矩阵不能相乘

解决方法:

我们都知道两个矩阵相乘的条件是矩阵1的列数等于矩阵2的行数,即[x,y]*[y,z]=[x,z],我这里出错的原因是在设计模型的时候,两个原本应该相乘的矩阵,选错了其中一个,导致行列数对不上,就报了这个错,最后是在运行时输出矩阵的格式之后才找到问题,记录一下引以为戒。

原因:

用DataParallel训练的模型数据并行方式的,key中会包含”module”关键字

解决方法:

model.load_state_dict({k.replace(‘module.’,”):v for k,v in checkpoint[‘state_dict’].items()})

在加载模型中代码里把module.关键字替换为空,此时就能正常加载模型参数了

2023.4.2

Ubuntu无法安装opencv-python库,报错

ERROR: Command errored out with exit status 1: /home/jack/anaconda3/envs/py27/bin/python /home/jack/anaconda3/envs/py27/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /tmp/tmpVBTizQ Check the logs for full command output.
原因:

opencv-python-4.3.0.38开始不再支持 Python 2.7

解决方法:

pip install opencv-python==4.2.0.32 -i https://pypi.tuna.tsinghua.edu.cn/simple

指定可用版本安装即可

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting opencv-python==4.2.0.32
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f6/ca/33ef6ffe0f6f7a825febdbf655c192f30d0f59d40f23b647d08a1c2a26c5/opencv_python-4.2.0.32-cp27-cp27mu-manylinux1_x86_64.whl (28.2MB)
|████████████████████████████████| 28.2MB 14.4MB/s
Requirement already satisfied: numpy>=1.11.1 in ./anaconda3/envs/py27/lib/python2.7/site-packages (from opencv-python==4.2.0.32) (1.16.6)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.2.0.32

TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

原因:

需要先将 tensor 转换到 CPU ,因为 Numpy 是 CPU-only

解决方法:

使用 .cpu() 先进行转换

修改前:

predict.numpy()

修改后:

predict.cpu().numpy()

参考文章:

AttributeError: module ‘numpy’ has no attribute ‘unit8’

错误原因:

低级错误,是uint8不是unit8!!!!

Original: https://blog.csdn.net/SuperBeauty/article/details/124456562
Author: SuperBeauty
Title: pytorch数据集的一些问题

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/708715/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球