pytorch学习之六 保存神经网络

我们训练好的神经网络,需要保存下来下次使用,有几种方法来做。

torch.save( net1,'net.pkl' );
torch.save( net1.state_dict(),'net_params.pkl' );
	

第一种方式,是保存整个神经网络信息,包括一些层数,每层神经元数,激励函数,以及所有的参数信息

第二种方式,是只保存参数信息。

对应的提取网络方法也不同,
完全保存的方法,可以直接load就好了
net2 = torch.load( ‘net.pkl’ )

只保存参数的方法还得先搭整个神经网络,再参数提取

#方法1
def restore_net():
	net2  =torch.load( 'net.pkl' )
	prediction = net2(x);
	
def restore_params();
    net3 = torch.nn.Sequential(
        torch.nn.Linear(1, 10),
        torch.nn.ReLU(),
        torch.nn.Linear(10, 1)
    )

net3.load_state_dict(torch.load('net_params.pkl'))

猜你喜欢

转载自blog.csdn.net/ronaldo_hu/article/details/91871232