Torch Getting Started note 10: How to build torch neural network model

Source: Internet
Author: User

This chapter does not involve too many neural network principles, but focuses on how to use the Torch7 neural network

First require (equivalent to the C language include) NN packet, the packet is a dependency of the neural network, remember to add ";" at the end of the statement, this is the same as MATLAB will print out the data. Torch allows us to design our own network of layers, like a container, to add layers of your own layer (the layers of neural networks) to the inside layer.

First, to construct a neural network container, that is, to declare a neural network container in the form above, the network name is NET, Bo Master currently know that Torch provides three forms of construction of the neural network container, as shown below

Here is the first kind, see the name can also guess is the queue form, layer by layer to load, the second, the third, the image should be more image than the description of my words. (There is time to find a different case to add the two tests later).

The line of code here is not a screenshot, the 3rd line is not, the 2nd line of code is directly followed by the 4th line of code. Here is the net Neural network container called add () adding method, add layer to inside, nn. Spatialconvolution () is the creation of a volume base, the meaning of the parameter values are: 1 for the input image of the channel is 1, which is rewritten to 3 is the input 3-channel picture; 6 indicates the number of channels through which the convolution operation of the layer is output; 5 These two parameters represent a convolution operation with a 5x5 kernel. In summary, the layer is a 1-channel image input and then use the 5x5 convolution kernel for convolution operations, and finally output 6 channels of the picture.
module = NN. Spatialconvolution (Ninputplane, Noutputplane, KW, KH, [DW], [DH], [PADW], [PadH])

This is the convolution layer function description, we have only used the first 4 parameters, [] for the parameters can not be entered, the following DW,DH that is, the horizontal and vertical movement of the step size default to 1,PADW for the width dimension of the parameter appended 0, because some of the image with you set the convolution kernel and step operation, There may be the wrong dimension (analogy 2x3 matrix can not be multiplied by the 2x4 matrix, you need to replace the 2x4 matrix into a 3x4 matrix, here is the matrix of the 2x4 to add a row of 0 elements, so that it becomes the matrix of 3x4), the default is 0, preferably set to (kW-1)/ 2, which is the width of the convolution core 1 and then divided by 2. The padh default is PADW, preferably set to (kH-1)/2, which is the high-1 convolution core and then divided by 2.

If the convolutional neural network is not very understanding, may look still not too understand I express meaning, can first put these put, continue to look down, recommend a website http://cs231n.github.io/convolutional-networks/ Do not like to read English can first look at this http://dataunion.org/11692.html website.


The 4th line is to add the volume base operation to the container, now adding the excitation function Relu function in the convolution layer, you can also add sigmoid function and so on.

Here is the Add pool layer, which is the largest number in a 2x2 area, with a horizontal step of 2 and a longitudinal step of 2.
module = NN. Spatialmaxpooling (KW, KH, [DW], [DH], [PADW], [PadH])

Padw and Padh, like the convolution layer, do not repeat them.

Add volume base torch will automatically connect the previous 6 pictures as input, this time is output 16 pictures, convolution kernel for 5x5, excitation function, pooling layer.

Line 7th is followed by line 9th, line 8th has no code.
This line converts the 16x5x5 3-D tensor into a 16x5x5 1-d tensor. What is popularly understood as what was originally described in the 16x5x5 cube is now transformed into a 16x5x5 line to describe what was before (there is a picture, I'll try to explain it again, hopefully more clearly).

Linear is an all-connected layer, we convert the 3-dimensional tensor into (imagined as a cube) into a 1-dimensional tensor, which outputs 16x5x5=400 nodes in the previous step, where we make these 400 nodes with 120 nodes to establish a full connection.

Add an excitation layer

The above 120 nodes are fully connected with 84 nodes, adding an excitation layer.

The first step of line 13, 84 nodes are fully connected with 10 nodes. Finally, the output of 10 nodes is converted to logarithmic probability, which is used for classification problems.

Finally, print out your own built-in neural network. Note that net:__ is a two underline, do not hit the wrong ...

This is the establishment of the structure of the neural network, I will explain the view () function here, the S4 layer to the C5 layer here is the use of the view () function, and then imagine my analogy of the cube-to-line conversion, the S4 layer can be seen as long 16 width 5 High 5 cube, The C5 layer becomes a 120-length segment.

The neural network is established, and the next chapter is how to train it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.