python - tensorflow 中 numpy.newaxis 的替代方案是什么?

标签 python python-3.x numpy tensorflow tensor

您好,我是 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/

相关文章:

javascript - 对于 Django cometd 来说,什么更好呢?

python - 是否可以覆盖 "self"以指向 python 中 self.method 中的另一个对象?

python - Pyinstaller 在创建 exe 文件时无法导入 pylsd

shell - 我在 PyCharm 中得到 'Inappropriate ioctl for device',但不是正常的 shell

Python函数只运行一次

python - 使用日期数据进行 Sklearn 线性回归

python - numpy 直方图数组

python - python-eve 的 redis 设置

python - Selenium Webdriver 与 python 抛出 requests.exceptions.MissingSchema

python - 比较2种颜色直方图,其中一种是从txt文件加载的