zoukankan      html  css  js  c++  java
  • pytorch1.0神经网络保存、提取、加载

    pytorch1.0网络保存、提取、加载

    import torch
    import torch.nn.functional as F  # 包含激励函数
    import matplotlib.pyplot as plt
    
    # 假数据
    x = torch.unsqueeze(torch.linspace(-1,1,100),dim=1) # x data (tensor), shape=(100, 1)
    y = x.pow(2) + 0.2*torch.rand(x.size())  # noisy y data (tensor), shape=(100, 1)
    
    # The code below is deprecated in Pytorch 0.4. Now, autograd directly supports tensors
    # x, y = Variable(x, requires_grad=False), Variable(y, requires_grad=False)
    
    def save():
        # save net1
        # 建网络
        net1 = torch.nn.Sequential(
            torch.nn.Linear(1, 10),
            torch.nn.ReLU(),
            torch.nn.Linear(10, 1)
        )
        optimizer = torch.optim.SGD(net1.parameters(), lr=0.5)
        loss_func = torch.nn.MSELoss()
        # 训练
        for t in range(100):
            prediction = net1(x)
            loss = loss_func(prediction, y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
    
        # plot result
        plt.figure(1, figsize=(10, 3))
        plt.subplot(131)
        plt.title('Net1')
        plt.scatter(x.data.numpy(), y.data.numpy())
        plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
    
        # 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 # 只保存网络中的参数 (速度快, 占内存少)
    
    
    # 提取网络
    def restore_net():
        # restore entire net1 to net2
        net2 = torch.load('net.pkl')
        prediction = net2(x)
    
        # plot result
        plt.subplot(132)
        plt.title('Net2')
        plt.scatter(x.data.numpy(), y.data.numpy())
        plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
    
    # 只提取网络参数
    def restore_params():
        # 新建 net3
        # restore only the parameters in net1 to net3
        net3 = torch.nn.Sequential(
            torch.nn.Linear(1, 10),
            torch.nn.ReLU(),
            torch.nn.Linear(10, 1)
        )
        # 将保存的参数复制到 net3
        # copy net1's parameters into net3
        net3.load_state_dict(torch.load('net_params.pkl'))
        prediction = net3(x)
    
        # plot result
        plt.subplot(133)
        plt.title('Net3')
        plt.scatter(x.data.numpy(), y.data.numpy())
        plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
        plt.show()
    
    # 保存 net1 (1. 整个网络, 2. 只有参数)
    # save net1
    save()
    # 提取整个网络
    # restore entire net (may slow)
    restore_net()
    # 提取网络参数, 复制到新网络
    # restore only the net parameters
    restore_params()
  • 相关阅读:
    JetBrains注册码计算(IntelliJ IDEA 15.0注册码激活)
    java分页数据导出excel
    linux系统关机与重新启动命令
    无向图的连通性分析
    流域水文模拟
    深信服笔试题(网络project师售后)
    CSS这些代码你都不会,你还有什么好说的!!!
    springMVC3学习(四)--訪问静态文件如js,jpg,css
    POJ 3311 Hie with the Pie(状压DP + Floyd)
    NSDictionary所有API的学习。
  • 原文地址:https://www.cnblogs.com/jeshy/p/11199820.html
Copyright © 2011-2022 走看看