论文中的结构如下:
Input (224x224x3)
(1) Conv1 (96x55x55)
(2) MaxPool1 (96x27x27)
(3) Conv2 (256x27x27)
(4) MaxPool2 (256x13x13)
(5) Conv3 (384x13x13)
(6) Conv4 (384x13x13)
(7) Conv5 (256x13x13)
(8) MaxPool3 (256x6x6)
(9) FC (1x4096)
(10) FC (4096)
(11) FC (1000)
Output (1000)
打算用一个其他的数据集,输出只有3类,所以把(11)改成只有3个神经元
包 | 版本 |
---|---|
pytorch | 0.4.1 post2 |
torchvision | 0.1.8 -->0.2.1 |
import torch
USE_GPU = torch.cuda.is_available()
print("USE_GPU = {}".format(USE_GPU))
如果显示为True即可使用GPU进行训练
torch.nn
,AlexNet用到了以下几个类:torch.nn.Module
torch.nn.Sequential
torch.nn.Conv2d
torch.nn.MaxPool2d
torch.nn.Linear
torch.nn.Conv2d()
而不用torch.nn.Conv3d()
?答:
(1)图像确实有rgb三个通道,卷积核filter也确实是三维,但Conv2d和Conv3d并非按照卷积核的维数进行区分;
(2)处理一幅图像时,卷积核只在width和height两个方向上滑动,对应使用Conv2d;
(3)我们可以说,使用Conv2d的时候,pytorch默认被卷图像的层数(3层rgb)和单个卷积核的层数(3层)是一致的;
(4)所以,当使用torch.nn.Conv2d()
的时候,kernel_size这个参数只需要给出卷积核(width,height)这两个维度的大小即可,不需要给出“单个卷积核的层数” 和“所使用卷积核的个数” 。
'''example 输入224*224*3层 的图像 卷积为--> 96层*55*55 的feature map
前两个参数3和96表示的是 输入的层数 、 输出的层数
其中filter的层数默认为3,使用96个这样的filter,即得到想要大小的feature map'''
torch.nn.Conv2d(3, 96, kernel_size=(11,11), stride=3, padding=2, bias=True)
torchvision.transforms
:0.1.8版本的torchvision中没有transforms.Resize(),只有transforms.Scale(),且只能变成正方形;
0.2.1版本的torchvision中可以使用Resize
my_trans = transforms.Compose([transforms.Resize((224,224 ),transforms.ToTensor()])
torch.utils.data.Dataset
和 torch.utils.data.DataLoader
:要实现按照自己的意愿读入数据,可以继承torch.utils.data.Dataset类,并重写如下三个方法:
def __init__():
def __getitem__():
def __len__():
A、关于Pytorch变量类型的转换
Traceback (most recent call last):
File "/home/mortimerli/桌面/测试小代码/1_mortimer_alexnet/mortimer_alexnet_with_pytorch.py", line 159, in
batch_loss = loss_func(y_pred, y_train_batch) # 计算这个batch的loss
File "/home/mortimerli/anaconda3/envs/python36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/home/mortimerli/anaconda3/envs/python36/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 421, in forward
return F.mse_loss(input, target, reduction=self.reduction)
File "/home/mortimerli/anaconda3/envs/python36/lib/python3.6/site-packages/torch/nn/functional.py", line 1716, in mse_loss
return _pointwise_loss(lambda a, b: (a - b) ** 2, torch._C._nn.mse_loss, input, target, reduction)
File "/home/mortimerli/anaconda3/envs/python36/lib/python3.6/site-packages/torch/nn/functional.py", line 1674, in _pointwise_loss
return lambd_optimized(input, target, reduction)
RuntimeError: mse_loss_forward is not implemented for type torch.cuda.LongTensor
https://blog.csdn.net/hustchenze/article/details/79154139
按照上面文章所说将代码从:
batch_loss = loss_func(y_pred, y_train_batch) # 计算这个batch的loss
改为:
aa = y_pred.float()
bb = y_train_batch.float()
batch_loss = loss_func(aa, bb) # 计算这个batch的loss
即可进行loss的计算了,之后出现了下面的问题
B、关于loss function和梯度下降
Traceback (most recent call last):
File "/home/mortimerli/桌面/测试小代码/1_mortimer_alexnet/mortimer_alexnet_with_pytorch.py", line 162, in
batch_loss.backward() # 反向传播一次
File "/home/mortimerli/anaconda3/envs/python36/lib/python3.6/site-packages/torch/tensor.py", line 93, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/home/mortimerli/anaconda3/envs/python36/lib/python3.6/site-packages/torch/autograd/__init__.py", line 90, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
怀疑model中的parameters没有设置为需要梯度,但是如下命令输出全部为True,排除该可能
for para in model.parameters():
print(para.requires_grad) # = True
后来发现是解决上一个问题时,这段代码导致了这个问题
aa = y_pred.float()
bb = y_train_batch.float()
batch_loss = loss_func(aa, bb) # 计算这个batch的loss
aa = y_pred.float()
这个操作使aa具有和y_pred相同的数据,但是requires_grad这个属性由True变为了False;bb和y_train_batch同理。这样我们得到计算所得batch_loss的requires_grad属性也是False,因此不能反向传播。解决方法,添加下面代码中的倒数第二句,将batch_loss.requires_grad
属性设置为True
:
aa = y_pred.float()
bb = y_train_batch.float()
batch_loss = loss_func(aa, bb) # 计算这个batch的loss
batch_loss.requires_grad = True#(requires_grad=True)
batch_loss.backward() # 反向传播一次
参考:https://ptorch.com/news/163.html Pytorch v0.4.0发布,首次支持Windows以及张量/变量合并
C、training_acc一直为0
D、loss降不下去,acc提不起来
E.改用CrossEntrophyLoss
F.leaf Tensor 叶节点
Pickle AttributeError: Can’t get attribute ‘Wishart’ on
保存:
torch.save(model,"model.pth")
读取:
model = torch.load("model.pth")