I have no idea why the result is all 0 with tensor. Anything wrong here?
>>> import torch
>>> import numpy as np
>>> import math
>>> torch.__version__
'0.4.1'
>>> np.__version__
'1.15.4'
>>> torch.arange(0, 10, 2) *-(math.log(10000.0) / 10)
tensor([0, 0, 0, 0, 0])
>>> np.arange(0, 10, 2) *-(math.log(10000.0) / 10)
array([-0. , -1.84206807, -3.68413615, -5.52620422, -7.3682723 ])
>>> torch.arange(0, 10, 2)
tensor([0, 2, 4, 6, 8])
>>> np.arange(0, 10, 2)
array([0, 2, 4, 6, 8])
As written in the comment when using 0.4.0 get the same results as with numpy:
tensor([-0.0000, -1.8421, -3.6841, -5.5262, -7.3683])
However with 0.4.1
I'm getting a zero vector too.
The reason for this is that torch.arange(0, 10, 2)
returns a tensor of type float
for 0.4.0 while it returns a tensor of type long
for 0.4.1.
So casting your tensor to float
should work for you:
torch.arange(0, 10, 2).float() *-(math.log(10000.0) / 10)
Multiplying long
and float
works by heavy rounding, as the result is still a tensor of type long
. So when converting a FloatTensor
to a LongTensor
values between -1 and 1 will be rounded to 0.
Since -(math.log(10000.0) / 10)
results in -0.9210340371976183
your result is 0
. So effectively -0.9210340371976183
is converted to type long
before multiplying. But when converting it will be round down to 0
, see this example:
t = torch.tensor((-(math.log(10000.0) / 10)))
print('FloatTensor:', t)
print('Converted to Long:', t.long())
Outout:
FloatTensor: tensor(-0.9210)
Converted to Long: tensor(0)
Thus:
torch.arange(0, 10, 2).float() *-(math.log(10000.0) / 10)
becomes:
torch.arange(0, 10, 2).float() * 0
Therefore you get a tensor of zeros as result.
Some more examples:
If you multiply it with a value between 1 and 2, lets say 1.7, it will always been rounded down to 1:
t = torch.tensor(range(5), dtype=torch.long)
print(t)
print(t * 1.7)
Output:
tensor([ 0, 1, 2, 3, 4])
tensor([ 0, 1, 2, 3, 4])
And similarly when multiplying with 2.7
results in an effective multiplication of 2
:
t = torch.tensor(range(5), dtype=torch.long)
print(t)
print(t * 2.7)
Output:
tensor([ 0, 1, 2, 3, 4])
tensor([ 0, 2, 4, 6, 8])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With