使用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)