1、 view()的作用相当于numpy中的reshape,重新定义矩阵的形状
x=x.view(-1,32*5*5)#view的功能跟reahape相同,在这里的作用是展平,与torch.flatten功能相同
2、 降维torch.squeeze(input, dim=None, out=None)
3、 增维torch.unsqueeze(input, dim, out=None)
4、安装 pycocotools
Linux: pip install pycocotools
Windows:pip install pycocotools-windows
5、totensor的作用:将PIL图像或者一个numpy的数据转换为tensor格式,将[高度,宽度,通道]转换为[通道,高度,宽度],并将[0,255]之间的像素值映射到[0,1]
import torchvision.transforms as transforms
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
])
6、5中Normalize的作用:使用均值和标准差对tensor进行标准化
7、找到输出中最大的那个值的索引torch.max()
predict_y=torch.max(outputs,dim=1)[1]
8,pytorch中得到的是tensor,如果想得到数值,通过.item()方法进行获取
running_loss+=loss.item()
accuracy=(predict_y==val_label).sum().item()/val_label.size(0)
9,模型保存和加载
#模型保存
save_path = './lenet.pth'
torch.save(net.state_dict(),save_path)
#模型加载
net=LeNet()
net.load_state_dict(torch.load('lenet.pth'))
10、torch中padding的使用:当padding=(1,2)时,代表上下各补一行0,左右各补两行0
11,如何保证3×3的卷积后特征层的尺寸不变?
让其padding和stride=1即可
如何保证5×5的卷积后特征层的尺寸不变?
让其padding=2、stride=1即可
总结:如何设置padding尺寸使得卷积或者池化后尺寸不变?
根据公式:
输出尺寸=(输入尺寸-核尺寸+2p)/s +1
让padding尺寸=(卷积核或者池化核-1)除以2
即可
12,csdn写博客修改jpg格式图像尺寸:
总结:
1把图片链接中xxx.jpg以后的全删了
2在xxx.jpg后面加上要修改的尺寸,注意 = 前有个空格
3若要居中在xxx.jpg后面加上 #pic_center
pytorch
dir()help()
##