美文网首页
2019-08-29pytorch学习(二)网络

2019-08-29pytorch学习(二)网络

作者: 强扭的解渴瓜 | 来源:发表于2019-08-29 17:38 被阅读0次

小白的自我救赎

Neural networks can be constructed using the torch.nn package
An nn.Module contains layers, and a method forward(input)that returns the output即两部分组成:构建网络层,已经输入接口forward函数,用于前向传播
Define a network:

import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):#pythorch固有形式
      def __init__(self):#__init__方法的第一个参数永远是self,表示创建的实例本身。因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。
      super(Net, self).__init__()
  # 1 input image channel, 6 output channels, 3x3 square convolution
        # kernel
      self.conv1 = nn.Conv2d(1, 6, 3)
      self.conv2 = nn.Conv2d(6, 16, 3)
 # an affine operation: y = Wx + b
      self.fc1 = nn.Linear(16 * 6 * 6, 120)  # 6*6 from image dimension
      self.fc2 = nn.Linear(120, 84)
      self.fc3 = nn.Linear(84, 10)#以上代码即为构建网络层部分
#**注意:参数只涉及到网络中卷积核数目、大小等参数,不涉及输入图像大小
#以下代码为forward函数、前向传播部分
      def forward(self, x):
    # Max pooling over a (2, 2) window
      x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
      x = F.max_pool2d(F.relu(self.conv2(x)), 2)
      x = x.view(-1, self.num_flat_features(x))#拉成向量
      x = F.relu(self.fc1(x))
      x = F.relu(self.fc2(x))
      x = self.fc3(x)
      return x
  def num_flat_features(self, x):#定义子函数计算batch维度之外的向量参数个数
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features
net = Net()
print(net)

Out:
Net(
(conv1): Conv2d(1, 6, kernel_size=(3, 3), stride=(1, 1))
(conv2): Conv2d(6, 16, kernel_size=(3, 3), stride=(1, 1))
(fc1): Linear(in_features=576, out_features=120, bias=True)
(fc2): Linear(in_features=120, out_features=84, bias=True)
(fc3): Linear(in_features=84, out_features=10, bias=True)
)

params = list(net.parameters())
print(len(params))
print(params[0].size())  # conv1's .weight

Out:
10
torch.Size([6, 1, 3, 3])
下一步为了防止每次反向传播,梯度累加,需要梯度初始化为0

net.zero_grad()#所有参数梯度清零
out.backward(torch.randn(1, 10))#反向传播的过程只需要调用loss.backgrad()函数即可

注意:
torch.nn only supports mini-batches. The entire torch.nn package only supports inputs that are a mini-batch of samples, and not a single sample.即不支持单张图像输入训练
例如,nn.Conv2d只接受4维的张量:
[nSamples ,nChannels, Height ,Width]
如果只有单个样本,那么使用input.unsqueeze(0)来增加假的batch维度.

相关文章

  • 2019-08-29pytorch学习(二)网络

    小白的自我救赎 Neural networks can be constructed using the torc...

  • 网络学习二

    一个服务器发送给客户端的时间的客户端代码以及详解 代码展示 代码详解 代码展示 代码详解 (sockfd = so...

  • 网络课程学习《二》

    九月七号早上,我和我班的孩子们参加了第二次彩虹花晨读《打开书》,孩子们依然很兴奋。 这次孩子们比第...

  • 网络编程学习二

    一、请求的数据格式 在网络数据传输过程中,服务器是不支持ios对象的,ios端是不能直接提交字符串、字典等,需要在...

  • 网络编程学习二

    一、请求的数据格式 在网络数据传输过程中,服务器是不支持ios对象的,ios端是不能直接提交字符串、字典等,需要在...

  • TensorFlow 深度学习中文第二版(初稿)

    TensorFlow 深度学习中文第二版 第 1 章深度学习入门人工神经网络ANN 如何学习?神经网络架构深度学习...

  • 神经网络:学习(二)

    参数展开 在Octave中,若我们需要使用fminuc函数来计算使得代价函数最小化的权重矩阵,我们需要将参数矩阵展...

  • 网络货运平台学习(二)

    一、定义: “无车承运人”指的是不拥有车辆而从事货物运输的个人或单位。“无车承运人”具有双重身份,对于真正的托运人...

  • 深度学习第二天--卷积神经网络

    昨天是学习深度学习的第二天,学习到了一个很重要的现代神经网络--卷积神经网络,就是它开启了对于现代神经网络的第三...

  • 为什么学生网络教育效果不好

    目录: 一、学习网络教育的感受; 二、、网络教育的优点! 三、网络教育效果不好的原因? 四、怎么改进网络教学效果 ...

网友评论

      本文标题:2019-08-29pytorch学习(二)网络

      本文链接:https://www.haomeiwen.com/subject/eitwectx.html