module
= Identity()
Creates a module that returns whatever is input to it as output.
This is useful when combined with the module
ParallelTable
in case you do not wish to do anything to one of the input Tensors.
Example:
require "lab" mlp=nn.Identity() print(mlp:forward(lab.ones(5,2)))gives the output:
1 1 1 1 1 1 1 1 1 1 [torch.Tensor of dimension 5x2]
Here is a more useful example, where one can implement a network which also computes a Criterion using this module:
pred_mlp=nn.Sequential(); -- A network that makes predictions given x. pred_mlp:add(nn.Linear(5,4)) pred_mlp:add(nn.Linear(4,3)) xy_mlp=nn.ParallelTable();-- A network for predictions and for keeping the xy_mlp:add(pred_mlp) -- true label for comparison with a criterion xy_mlp:add(nn.Identity()) -- by forwarding both x and y through the network. mlp=nn.Sequential(); -- The main network that takes both x and y. mlp:add(xy_mlp) -- It feeds x and y to parallel networks; cr=nn.MSECriterion(); cr_wrap=nn.CriterionTable(cr) mlp:add(cr_wrap) -- and then applies the criterion. for i=1,100 do -- Do a few training iterations x=lab.ones(5); -- Make input features. y=torch.Tensor(3); y:copy(x:narrow(1,1,3)) -- Make output label. err=mlp:forward{x,y} -- Forward both input and output. print(err) -- Print error from criterion. mlp:zeroGradParameters(); -- Do backprop... mlp:backward({x, y} ); mlp:updateParameters(0.05); end