nn.Linear
'''
class torch.nn.Linear(in_features,out_features,bias=True)
weight=Parameter(torch.Tensor(out_features,in_features))
bias=Parameter(torch.Tensor(out_features))
'''
import torch
my_input=torch.randn(20,30)
my_layer=torch.nn.Linear(30,50)
my_output=my_layer(my_input)
#my_layer.weight.shape:torch.Size([50,30])
#my_layer.bias.shape:toorch.Size([50])
#my_output.shape:torch.Size([20,50])
做线性变换:y=Ax+b
weight和bias服从均匀分布u(-sqrt(1/my_input),sqrt(1/my_input))
bias是一维向量,采用广播机制扩展到输出向量的每行进行计算
nn.Parameter
和Module一起使用时,当parameter赋值给module的属性时,会自动将parameter加入到模型的参数列表中,而tensor不会。
Module是所有神经网络模块的基类。
class yourNet(nn.Module):
def __init__(self):
super(yourNet,self).__init__()
self.linear=nn.Linear(512,3)
self.param=nn.Parameter(torch.tensor([1,2,3],dtype=torch.float),requires_grad=True)
self.tensr=torch.tensor([3,2,1],dtype=torch.float)
model=yourNet()
for i in model.buffers():
print(i)
for i in model.parameters():
print(i)
#读取参数1:
fc._parameters
#读取参数2:
for n,p in model.named_parameters():
print(n,p)
#读取参数3:如上
model.buffers:返回模型所有缓存参数的迭代器,缓存参数是不需要参与反向传播的参数
model.parameters:返回模型所有参数的迭代器
.
pytorch模型文件pth
- 保存模型
#只保存模型参数
state_dict={'net':model.state_dict(),
'optimizer':optimizer.state_dict(),
'epoch':epoch}
torch.save(state_dict,"yourModel.pth")
#保存整个模型
torch.save(model,"yourModel.pth")
print(type(model.state_dict()))
for param in model.state_dict():
print(param,model.state_dict()[param].size())
model.state_dict是一个python的有序字典对象OrderedDict,将每一层与对应参数建立映射关系。只有可以训练的网络层才会被保存到state_dict中,如卷积层、线性层,而池化层、归一化层本身没有参数不会保存在state_dict中。model.parameters()返回一个迭代器,每一个元素都是参数;state_dict是一个字典,包含键值及参数。
optimizer.state_dict包含了优化器的状态及被使用的超参数如lr, momentum, weight_decay。
- 加载模型
checkpoint=torch.load("yourModel.pth")
#checkpoint=torch.load("yourModel.pth",map_location=torch.device('cpu'))
model.load_state_dict(checkpoint['net'])
optimizer.load_state_dict(checkpoint['optimizer'])
start_epoch=checkpoint['epoch']+1
for key,value in checkpoint['optimizer'].items():
print(key,type(value))
- pth文件
import torch
state_dict=torch.load("yourModel.pth")
print(type(state_dict)) #<class 'collections.OrderedDict'>
for i in state_dict:
print(type(state_dict[i])) #<class 'torch.nn.parameter.Parameter'>
print(state_dict[i].data.size()) #torch.Size(...)
print(state_dict[i].requires_grad) #True/Flase
pth文件通过有序字典保存模型参数,每个元素都是Parameter参数,包含data和requires_grad两个方法,data保存模型参数,requires_grad保存当前参数是否需要进行反向传播。
.pkl文件以二进制形式存储,直接读文件需要’rb’而不是‘r’
.pth文件,python在遍历已知库文件目录过程中,如果遇到.pth文件,就会将文件中所记录的路径加入sys.path设置中。