简体   繁体   English

几个后续的“reshape”操作是否只等同于一个?

[英]Are several subsequent `reshape` operations equivalent to only one?

In Pytorch, I recently stumbled onto code that looks like this.在 Pytorch 中,我最近偶然发现了如下代码。

# initially, my_tensor is one_dimensional, of length b*x*y 
my_tensor = my_tensor.reshape(b, x, y)
my_tensor = my_tensor.reshape(b, x*y)

Is it equivalent to only writing the second line?是不是相当于只写了第二行?

my_tensor = my_tensor.reshape(b, x*y)

And in general, is doing several reshape operations always equivalent to only doing the last one?一般来说,做几次重塑操作总是等同于只做最后一次吗?

Intuitively, I think so, but the documentation for reshape doesn't really mention any invariant, and I couldn't find information for the inner representation of tensors and how reshape changed that直觉上,我是这么认为的,但是reshape 的文档并没有真正提到任何不变量,而且我找不到关于张量的内部表示以及 reshape 如何改变它的信息

The reshape operation does not (need to) touch the underlying data.重塑操作不(需要)触及底层数据。 It simply adjusts the "meta-data" about the dimensions.它只是调整有关维度的“元数据”。 So a series of reshape operations (without any operations in between.!) is equal to a single reshape operation.因此,一系列重塑操作(中间没有任何操作!)等于单个重塑操作。

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

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