####1、保存模型参数和加载模型参数
(A)保存参数
# 2 ways to save the net
torch.save(net1, 'net.pkl') # save entire net
torch.save(net1.state_dict(), 'net_params.pkl') # save only the parameters
(B)加载参数
# copy net1's parameters into net3
net3.load_state_dict(torch.load('net_params.pkl'))
prediction = net3(x)
上面出现的net1和net3都是nn.Module的实例。
####2、模型参数的钳位
# Clip weights of discriminator
for p in discriminator.parameters():
p.data.clamp_(-opt.clip_value, opt.clip_value)
p是Module(nn.Module)—— discriminator的参数。这段代码是实现WGAN时用到的。钳位不仅可以实现WGAN,而且它可以消除在训练中出现的nan情况,但钳位的大小很关键。
####3、模型的CUDA化
在配有CUDA的训练过程中,模型和数据都需要加载到CUDA中,pytorch的张量有两种类型,以Float为例:用于CPU——torch.FloatTensor、用于CUDA——torch.cuda.FloatTensor,以下是完整列表:
n CPU CUDA Desc. 1 torch.FloatTensor torch.cuda.FloatTensor 32-bit floating point 2 torch.DoubleTensor torch.cuda.DoubleTensor 64-bit floating point 3 N/A torch.cuda.HalfTensor 16-bit floating point 4 torch.ByteTensor torch.cuda.ByteTensor 8-bit integer (unsigned) 5 torch.CharTensor torch.cuda.CharTensor 8-bit integer (signed) 6 torch.ShortTensor torch.cuda.ShortTensor 16-bit integer (signed) 7 torch.IntTensor torch.cuda.IntTensor 32-bit integer (signed) 8 torch.LongTensor torch.cuda.LongTensor 64-bit integer (signed) \begin{array}{c|lc|r} n & \text{CPU} & \text{CUDA} & \text{Desc.}\\ \hline 1 & \text{torch.FloatTensor} & \text{torch.cuda.FloatTensor} & \text{32-bit floating point} \\ 2 & \text{torch.DoubleTensor} & \text{torch.cuda.DoubleTensor} & \text{64-bit floating point} \\ 3 & \text{N/A} & \text{torch.cuda.HalfTensor} & \text{16-bit floating point} \\ 4 & \text{torch.ByteTensor} & \text{torch.cuda.ByteTensor} & \text{8-bit integer (unsigned)} \\ 5 & \text{torch.CharTensor} & \text{torch.cuda.CharTensor} & \text{8-bit integer (s