您好,我是 tensorflow 的新手。我想在 tensorflow 中实现以下 python 代码。
import numpy as np
a = np.array([1,2,3,4,5,6,7,9,0])
print(a) ## [1 2 3 4 5 6 7 9 0]
print(a.shape) ## (9,)
b = a[:, np.newaxis] ### want to write this in tensorflow.
print(b.shape) ## (9,1)
最佳答案
对应的命令是tf.newaxis
(或者None
,在numpy中)。它在 tensorflow 的文档中没有自己的条目,但在 tf.stride_slice
的文档页面上有简要提及。 .
x = tf.ones((10,10,10))
y = x[:, tf.newaxis] # or y = x [:, None]
print(y.shape)
# prints (10, 1, 10, 10)
使用 tf.expand_dims
也可以,但是,如上面的链接所述,
Those interfaces are much more friendly, and highly recommended.
关于python - tensorflow 中 numpy.newaxis 的替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42344090/