可以先对每个卷积层的weight和bias进行初始化,再使用add_modules对Sequential进行添加,如果要查看的话则使用f
for module in model.modules():
if isinstance(module,nn.Conv3d):
print(module.weight)
然后保存模型可以用load_state_dict()对model和optimizer进行加载
保存的时候可以用torch.save(state_dict(字典),path)
也可以直接保存model
import torch
import torch.nn as nn
w = torch.empty(2, 3)
# 1. 均匀分布 - u(a,b)
# torch.nn.init.uniform_(tensor, a=0, b=1)
nn.init.uniform_(w)
# tensor([[ 0.0578, 0.3402, 0.5034],
# [ 0.7865, 0.7280, 0.6269]])
# 2. 正态分布 - N(mean, std)
# torch.nn.init.normal_(tensor, mean=0, std=1)
nn.init.normal_(w)
# tensor([[ 0.3326, 0.0171, -0.6745],
# [ 0.1669, 0.1747, 0.0472]])
# 3. 常数 - 固定值 val
# torch.nn.init.constant_(tensor, val)
nn.init.constant_(w, 0.3)
# tensor([[ 0.3000, 0.3000, 0.3000],
# [ 0.3000,