import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import torch class Net(nn.Module): # 需要继承这个类
def __init__(self):
super(Net, self).__init__()
# 建立了两个卷积层,self.conv1, self.conv2,注意,这些层都是不包含激活函数的
self.conv1 = nn.Conv2d(1, 6, 5) # 1 input image channel, 6 output channels, 5x5 square convolution kernel
self.conv2 = nn.Conv2d(6, 16, 5)
# 三个全连接层
self.fc1 = nn.Linear(16 * 5 * 5, 120) # an affine operation: y = Wx + b
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10) def forward(self, x): # 注意,2D卷积层的输入data维数是 batchsize*channel*height*width
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) # Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv2(x)), 2) # If the size is a square you can only specify a single number
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) print(x)
print('y=--------')
return x def num_flat_features(self, x):
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()
# create your optimizer
optimizer = optim.SGD(net.parameters(), lr = 0.01)
num_iteations = 20
input = Variable(torch.randn(2, 1, 32, 32))
print('input=',input)
#target = Variable(torch.Tensor([5],dtype=torch.long))
target = Variable(torch.LongTensor([5,7]))
# in your training loop:
for i in range(num_iteations):
optimizer.zero_grad() # zero the gradient buffers,如果不归0的话,gradients会累加 output = net(input) # 这里就体现出来动态建图了,你还可以传入其他的参数来改变网络的结构
criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)
loss.backward() # 得到grad,i.e.给Variable.grad赋值
optimizer.step() # Does the update,i.e. Variable.data -= learning_rate*Variable.grad

这里是给出的一个代码。

init只是规定了conv的输入通道数量、输出通道数量和卷积核尺寸。

然后在神经网络中,充当卷积层的是forward部分。

input = Variable(torch.randn(2, 1, 32, 32))    #batchsize,channel,height,width
target = Variable(torch.LongTensor([5,7]))     #我希望两个神经网络,第一个等于5,第二个等于7.当然随便两个数。(不代表5*7维矩阵呀)

简单了解pytorch的forward的更多相关文章

  1. 超简单!pytorch入门教程(五):训练和测试CNN

    我们按照超简单!pytorch入门教程(四):准备图片数据集准备好了图片数据以后,就来训练一下识别这10类图片的cnn神经网络吧. 按照超简单!pytorch入门教程(三):构造一个小型CNN构建好一 ...

  2. 超简单!pytorch入门教程(四):准备图片数据集

    在训练神经网络之前,我们必须有数据,作为资深伸手党,必须知道以下几个数据提供源: 一.CIFAR-10 CIFAR-10图片样本截图 CIFAR-10是多伦多大学提供的图片数据库,图片分辨率压缩至32 ...

  3. 超简单!pytorch入门教程(三):构造一个小型CNN

    torch.nn只接受mini-batch的输入,也就是说我们输入的时候是必须是好几张图片同时输入. 例如:nn. Conv2d 允许输入4维的Tensor:n个样本 x n个色彩频道 x 高度 x ...

  4. pytorch 调用forward 的具体流程

    forward方法的具体流程: 以一个Module为例:1. 调用module的call方法2. module的call里面调用module的forward方法3. forward里面如果碰到Modu ...

  5. 超简单!pytorch入门教程(一):Tensor

    http://www.jianshu.com/p/5ae644748f21 二.pytorch的基石--Tensor张量 其实标量,向量,矩阵它们三个也是张量,标量是零维的张量,向量是一维的张量,矩阵 ...

  6. 超简单!pytorch入门教程(二):Autograd

    一.autograd自动微分 autograd是专门为了BP算法设计的,所以这autograd只对输出值为标量的有用,因为损失函数的输出是一个标量.如果y是一个向量,那么backward()函数就会失 ...

  7. PyTorch之前向传播函数自动调用forward

    参考:1. pytorch学习笔记(九):PyTorch结构介绍 2.pytorch学习笔记(七):pytorch hook 和 关于pytorch backward过程的理解 3.Pytorch入门 ...

  8. 机器翻译注意力机制及其PyTorch实现

    前面阐述注意力理论知识,后面简单描述PyTorch利用注意力实现机器翻译 Effective Approaches to Attention-based Neural Machine Translat ...

  9. pytorch中检测分割模型中图像预处理探究

    Object Detection and Classification using R-CNNs 目标检测:数据增强(Numpy+Pytorch) - 主要探究检测分割模型数据增强操作有哪些? - 检 ...

随机推荐

  1. 软件工程 week 03

    一.效能分析 1.作业地址:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2139 2.git地址:https://git.coding.ne ...

  2. 通过用户名&密码验证访问远程共享文件夹 C#

    通过代码先在cmd中运行net use进行验证,然后就可访问共享文件了. 验证方法如下: public string connectState(string path/*要访问的文件路径*/, str ...

  3. VS2015 使用 Visual Studio Emulator For Android 调试无法命中断点的解决办法?

    源解决方案是英文版的,地址:https://dzone.com/articles/fix-for-could-not-connect-to-the-debugger-while-de 问题现象: 1. ...

  4. oracle删除当前用户以及当前用户所有表、索引等操作

    ORACLE删除当前用户下所有的表的方法 如果有删除用户的权限,则可以: drop user user_name cascade; 加了cascade就可以把用户连带的数据全部删掉.删除后再创建该用户 ...

  5. 1.1.17 Word在表格中插入竖排文字,显示一半

    隐藏效果如下所示: 这是因为文字的[段落行距]设置为[固定值],将文字选中,设置为[单倍行距]即可.

  6. 阿里四不像Fourinone

    阿里四不像——分布式核心技术框架 Fourinone https://blog.csdn.net/shero_zsmj/article/details/52277194

  7. EasyMock 模拟对象测试

    一.EasyMock 使用动态代理实现模拟对象创建,一般可以满足以下测试需求 1.要测试的模块依赖于其它自己控制不了的模块,如第三方服务,其它组员在开发的服务等,它们都没办法配合你来测试: 2.涉及到 ...

  8. 学习笔记之Problem Solving with Algorithms and Data Structures using Python

    Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...

  9. 团队第三次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-7日晚上9点 召开时长15分钟 任务表格 袁勤 继续学习SpringBoot https://github.com/buaa-2016/phyweb ...

  10. JS stringObject.Match()

    JavaScript match() 方法 JavaScript String 对象 定义和用法 match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 inde ...