tf.transpose 是否也会改变内存(如 np.ascontiguousarray)? [英] Does tf.transpose also change the memory (like np.ascontiguousarray)?

查看:58
本文介绍了tf.transpose 是否也会改变内存(如 np.ascontiguousarray)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 tf.transpose 是否也会改变内存布局?

If I use tf.transpose does it also change the memory layout?

在 numpy 中,它使用函数 np.ascontiguousarray.

In numpy it is used the function np.ascontiguousarray.

我的意思是,如果我使用 cuda,这将很重要.因为如果内存布局是 [N C H W] 或 [N H W C],它会有所不同.(N ... Nr 个样本,H ... 阵列高度,W ... 阵列宽度,C ... 阵列深度,例如 RGB)

I mean this would be important if I use cuda. Because it makes a difference if the memory layout is [N C H W] or [N H W C]. (N ... Nr of samples, H ... array height, W ... array width, C ... array depth, e.g. RGB)

如何检查?

推荐答案

如果你仔细阅读 文档你可以找到答案:

If you read carefully the documentation you can find the answer:

Numpy 兼容性

在 numpy 中,转置是内存高效的恒定时间操作,因为它们只是返回具有调整步幅的相同数据的新视图.

In numpy transposes are memory-efficient constant time operations as they simply return a new view of the same data with adjusted strides.

TensorFlow 不支持 strides,因此 transpose 返回一个新的张量,其中的项已排列.

TensorFlow does not support strides, so transpose returns a new tensor with the items permuted.

因此 tf.transpose 返回一个具有所需形状的新张量(因此效率低下),所以是的,它改变了内存布局.

Hence tf.transpose returns a new tensor with the desired shape (and therefore is inefficient), so yes, it changes the memory layout.

但是,您可以使用 tf.trasnpose>tf.reshape 用于在不创建新张量形状的情况下更改张量形状

However, instead of using tf.trasnpose you could use tf.reshape for changing the tensor shape without creating a new one

这篇关于tf.transpose 是否也会改变内存(如 np.ascontiguousarray)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆