Create a multitask network structure in torch

Supplement to Torch7 Starter Sequel --- Use of nngraph Package

http://blog.csdn.net/hungryof/article/details/72902169

faster-rcnn.torch

https://github.com/andreaskoepf/faster-rcnn.torch/blob/master/models/model_utilities.lua

https://github.com/torch/nngraph/blob/master/README.md


Go directly to the source code:

Originally, we used sequential to create a branchless network structure like this.

require 'loadcaffe'

local cnn = loadcaffe.load('VGG_ILSVRC_19_layers_deploy_5.prototxt','VGG_ILSVRC_19_layers_5.caffemodel','nn'):float()

net = nn.Sequential()
for i = 1,3 do
net:add(cnn:get(i))
end

print(net:get(1).weight)

Now we are going to use nngraph to create a two-branch network structure like this.

require 'nn'
require 'nngraph'
require 'loadcaffe'

local cnn = loadcaffe.load('VGG_ILSVRC_19_layers_deploy_5.prototxt','VGG_ILSVRC_19_layers_5.caffemodel','nn'):float()

local h1 = nn.Identity()()

net_same = h1 - cnn:get(1) - cnn:get(2)

net_1 = net_same - cnn:get(3) - cnn:get(4)
net_2 = net_same - cnn:get(3) - cnn:get(4)

gmod = nn.gModule({h1},{net_1,net_2})

print(gmod:get(2))

The out obtained by forward can be divided into two: out[1] and out[2]

If you want to merge them together: out = {out[1], our[2]}

Others are the same. Backward, updateGradInput, etc., just use it casually.


If there are two inputs, that's it.

h1 = - nn.Linear(20,20)
h2 = - nn.Linear(10,10)
hh1 = h1 - nn.Tanh() - nn.Linear(20,1)
hh2 = h2 - nn.Tanh() - nn.Linear(10,1)
madd = {hh1,hh2} - nn.CAddTable()
oA = madd - nn.Sigmoid()
oB = madd - nn.Tanh()
gmod = nn.gModule( {h1,h2}, {oA,oB} )

Isn't it so simple, ahaha


Guess you like

Origin blog.csdn.net/Sun7_She/article/details/76348961