PyTorch - multiplying tensor with scalar results in zero vector












2















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])









share|improve this question

























  • Results in tensor([-0.0000, -1.8421, -3.6841, -5.5262, -7.3683]) for me, v0.4.0a0+3749c58

    – blue-phoenox
    Nov 25 '18 at 11:46













  • Python version 3.7.1

    – Kuo
    Nov 25 '18 at 11:54
















2















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])









share|improve this question

























  • Results in tensor([-0.0000, -1.8421, -3.6841, -5.5262, -7.3683]) for me, v0.4.0a0+3749c58

    – blue-phoenox
    Nov 25 '18 at 11:46













  • Python version 3.7.1

    – Kuo
    Nov 25 '18 at 11:54














2












2








2


0






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])









share|improve this question
















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])






python numpy pytorch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 20:43









blue-phoenox

4,291101745




4,291101745










asked Nov 25 '18 at 11:33









KuoKuo

287




287













  • Results in tensor([-0.0000, -1.8421, -3.6841, -5.5262, -7.3683]) for me, v0.4.0a0+3749c58

    – blue-phoenox
    Nov 25 '18 at 11:46













  • Python version 3.7.1

    – Kuo
    Nov 25 '18 at 11:54



















  • Results in tensor([-0.0000, -1.8421, -3.6841, -5.5262, -7.3683]) for me, v0.4.0a0+3749c58

    – blue-phoenox
    Nov 25 '18 at 11:46













  • Python version 3.7.1

    – Kuo
    Nov 25 '18 at 11:54

















Results in tensor([-0.0000, -1.8421, -3.6841, -5.5262, -7.3683]) for me, v0.4.0a0+3749c58

– blue-phoenox
Nov 25 '18 at 11:46







Results in tensor([-0.0000, -1.8421, -3.6841, -5.5262, -7.3683]) for me, v0.4.0a0+3749c58

– blue-phoenox
Nov 25 '18 at 11:46















Python version 3.7.1

– Kuo
Nov 25 '18 at 11:54





Python version 3.7.1

– Kuo
Nov 25 '18 at 11:54












1 Answer
1






active

oldest

votes


















2














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])





share|improve this answer


























  • Yes. Can you elaborate a bit more on why is that? Without .float(), I add/minus 1,2... to (math.log(10000.0) / 10) and will get something like tensor([ 0, -2, -4, -6, -8]), tensor([0, 0, 0, 0, 0]), tensor([0, 2, 4, 6, 8])...

    – Kuo
    Nov 25 '18 at 12:08











  • @Kuo Thanks, I added some more examples to make the behaviour of multiplication long * float more clear!

    – blue-phoenox
    Nov 25 '18 at 12:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467011%2fpytorch-multiplying-tensor-with-scalar-results-in-zero-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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])





share|improve this answer


























  • Yes. Can you elaborate a bit more on why is that? Without .float(), I add/minus 1,2... to (math.log(10000.0) / 10) and will get something like tensor([ 0, -2, -4, -6, -8]), tensor([0, 0, 0, 0, 0]), tensor([0, 2, 4, 6, 8])...

    – Kuo
    Nov 25 '18 at 12:08











  • @Kuo Thanks, I added some more examples to make the behaviour of multiplication long * float more clear!

    – blue-phoenox
    Nov 25 '18 at 12:19
















2














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])





share|improve this answer


























  • Yes. Can you elaborate a bit more on why is that? Without .float(), I add/minus 1,2... to (math.log(10000.0) / 10) and will get something like tensor([ 0, -2, -4, -6, -8]), tensor([0, 0, 0, 0, 0]), tensor([0, 2, 4, 6, 8])...

    – Kuo
    Nov 25 '18 at 12:08











  • @Kuo Thanks, I added some more examples to make the behaviour of multiplication long * float more clear!

    – blue-phoenox
    Nov 25 '18 at 12:19














2












2








2







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])





share|improve this answer















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])






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 16:21

























answered Nov 25 '18 at 11:54









blue-phoenoxblue-phoenox

4,291101745




4,291101745













  • Yes. Can you elaborate a bit more on why is that? Without .float(), I add/minus 1,2... to (math.log(10000.0) / 10) and will get something like tensor([ 0, -2, -4, -6, -8]), tensor([0, 0, 0, 0, 0]), tensor([0, 2, 4, 6, 8])...

    – Kuo
    Nov 25 '18 at 12:08











  • @Kuo Thanks, I added some more examples to make the behaviour of multiplication long * float more clear!

    – blue-phoenox
    Nov 25 '18 at 12:19



















  • Yes. Can you elaborate a bit more on why is that? Without .float(), I add/minus 1,2... to (math.log(10000.0) / 10) and will get something like tensor([ 0, -2, -4, -6, -8]), tensor([0, 0, 0, 0, 0]), tensor([0, 2, 4, 6, 8])...

    – Kuo
    Nov 25 '18 at 12:08











  • @Kuo Thanks, I added some more examples to make the behaviour of multiplication long * float more clear!

    – blue-phoenox
    Nov 25 '18 at 12:19

















Yes. Can you elaborate a bit more on why is that? Without .float(), I add/minus 1,2... to (math.log(10000.0) / 10) and will get something like tensor([ 0, -2, -4, -6, -8]), tensor([0, 0, 0, 0, 0]), tensor([0, 2, 4, 6, 8])...

– Kuo
Nov 25 '18 at 12:08





Yes. Can you elaborate a bit more on why is that? Without .float(), I add/minus 1,2... to (math.log(10000.0) / 10) and will get something like tensor([ 0, -2, -4, -6, -8]), tensor([0, 0, 0, 0, 0]), tensor([0, 2, 4, 6, 8])...

– Kuo
Nov 25 '18 at 12:08













@Kuo Thanks, I added some more examples to make the behaviour of multiplication long * float more clear!

– blue-phoenox
Nov 25 '18 at 12:19





@Kuo Thanks, I added some more examples to make the behaviour of multiplication long * float more clear!

– blue-phoenox
Nov 25 '18 at 12:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467011%2fpytorch-multiplying-tensor-with-scalar-results-in-zero-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'