使用torch进行深度学习

本文介绍如何使用Torch框架及其nn包构建深度学习模型,包括构建LeNet5网络的具体步骤,并展示了如何通过前向传播和反向传播进行训练。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用torch进行深度学习。
参考 https://github.com/soumith/cvpr2015/blob/master/Deep%20Learning%20with%20Torch.ipynb

nn包

神经网络中torch中可以用nn包来构建,使用containers可以创建复杂神经网络。

require 'nn';

例如以下网络用于对数字图像进行分类:

{% image center http://fastml.com/images/cifar/lenet5.png %}

这是一个前向反馈网络,使用nn.Sequential网络容器可以实现:

net = nn.Sequential()
net:add(nn.SpatialConvolution(1, 6, 5, 5)) -- 1 input image channel, 6 output channels, 5x5 convolution kernel
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.SpatialMaxPooling(2,2,2,2))     -- A max-pooling operation that looks at 2x2 windows and finds the max.
net:add(nn.SpatialConvolution(6, 16, 5, 5))
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.SpatialMaxPooling(2,2,2,2))
net:add(nn.View(16*5*5))                    -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5
net:add(nn.Linear(16*5*5, 120))             -- fully connected layer (matrix multiplication between input and weights)
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.Linear(120, 84))
net:add(nn.ReLU())                       -- non-linearity 
net:add(nn.Linear(84, 10))                   -- 10 is the number of outputs of the network (in this case, 10 digits)
net:add(nn.LogSoftMax())                     -- converts the output to a log-probability. Useful for classification problems

print('Lenet5\n' .. net:__tostring());

另一个例子:

{% image center https://raw.githubusercontent.com/soumith/ex/gh-pages/assets/nn_containers.png %}

在torch中每个神经网络模块都具有自动分化功能。对于给定的输入,它有一个:forward(input)函数用于计算其经过网络后的输出。此外也有一个:backward(input, gradient)函数,用于分化在网络中的每个神经元。这是通过链式法则完成的。

input = torch.rand(1,32,32) -- pass a random tensor as input to the network
output = net:forward(input)
print(output)
net:zeroGradParameters() -- zero the internal gradient buffers of the network (will come to this later)
gradInput = net:backward(input, torch.rand(10))
print(#gradInput)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值