torch / torch7

http://torch.ch
Other
8.97k stars 2.38k forks source link

y:add(a,b) is y+=a*b instead of y=a+b #396

Closed abcnet closed 8 years ago

abcnet commented 8 years ago

y:add(a,b) is not putting a+b into y. Instead, it is y+=a*b.

hughperkins commented 8 years ago

y:add(a) syntax is for in-place add of a to y. if you want to add a to b, and put the result into y, you can do either of:

y = a + b 

or:

y = torch.add(a, b)
abcnet commented 8 years ago

I know y:add(a) is a completely different statement than y:add(a,b). I am saying the documentation is wrong in describing y:add(a,b). It is not y=a+b as described in the doc. Instead, it is accumulatively adding the product of a times b into y, like y=y+a*b in matlab.

hughperkins commented 8 years ago

Hmmm... right :-)

fmassa commented 8 years ago

@abcnet can't reproduce.

y = torch.ones(3)
a = torch.ones(3)*5
b = torch.ones(3)*2
y:add(a,b)
print(y)

this gives as output

 7
 7
 7
[torch.DoubleTensor of size 3]

as expected. I think the problem you are mentioning is because your a is a number, and not a tensor. and thus you are using the following overloaded version of add https://github.com/torch/torch7/blob/master/doc/maths.md#res-torchaddres-tensor1-value-tensor2

fmassa commented 8 years ago

@abcnet any news on that ? Can this issue be closed ?

abcnet commented 8 years ago

Correct, that is the overloaded version of add. y=torch.ones(3) y:add(10,torch.ones(3)) 11 11 11 Thanks