Multiply Two vector in torch

  1. torch.cmulz=torch.cmul(x,y) returns a new tensor.

    torch.cmul(z,x,y) puts the result in z.

    y:cmul(x) multiplies all elements of y with corresponding elements of x.

    z:cmul(x,y) puts the result in z.

  2. nn.CMul : learning  the scale input value
    mlp = nn.Sequential()
    mlp:add(nn.CMul(5, 1))
    
    y = torch.Tensor(5, 4)
    sc = torch.Tensor(5, 4)
    for i = 1, 5 do sc[i] = i; end -- scale input with this
    
    function gradUpdate(mlp, x, y, criterion, learningRate)
       local pred = mlp:forward(x)
       local err = criterion:forward(pred, y)
       local gradCriterion = criterion:backward(pred, y)
       mlp:zeroGradParameters()
       mlp:backward(x, gradCriterion)
       mlp:updateParameters(learningRate)
       return err
    end
    
    for i = 1, 10000 do
       x = torch.rand(5, 4)
       y:copy(x)
       y:cmul(sc)
       err = gradUpdate(mlp, x, y, nn.MSECriterion(), 0.01)
    end
    
    print(mlp:get(1).weight)
  3. nn.CMultable
    ii = {torch.ones(5)*2, torch.ones(5)*3, torch.ones(5)*4}
    m = nn.CMulTable()
    =m:forward(ii)
     24
     24
     24
     24
     24
    [torch.DoubleTensor of dimension 5]

Link: https://github.com/torch/nn/blob/master/doc/simple.md#nn.CMul

https://github.com/torch/nn/blob/master/doc/table.md#nn.CAddTable

http://torch7.readthedocs.io/en/rtd/maths/#torch.elementwise.dok

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s