简体   繁体   English

pytorch 关于张量形状的问题。 以及如何重塑张量

[英]pytorch question about tensor shape. And how to reshape a tensor

When I do print("action shape:, ", action.shape) for my tensor action I got (64,).当我为张量动作执行print("action shape:, ", action.shape)时,我得到了 (64,)。 It is the same as (1, 64)?和 (1, 64) 一样吗? And how do I reshape its size to (64,1)?以及如何将其大小重塑为 (64,1)?

Technically it is not the same shape and in pytorch you will get an error if you have things that need a shape of (64,) but you give it (1,64) but it is easy to change it to (64,) by squeezing it.从技术上讲,它不是相同的形状,在 pytorch 中,如果您有需要 (64,) 形状的东西但您给它 (1,64) 但很容易将其更改为 (64,)挤压它。 To reshape it to a size of (64, 1) you can do this要将其重塑为 (64, 1) 的大小,您可以这样做

action = action.unsqueeze(1)
# or
action = action.view(-1, 1)

either will work but I would recommend the first one.两者都可以,但我会推荐第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM