python - 张量 t[..., 1, tf.newaxis] ... 代表什么?

标签 python tensorflow

我开始使用 Python 和 TensorFlow 机器学习。

我正在研究一个示例,在该示例中,我创建了一个简单的张量,表示具有两行三列 float 的矩阵:

t = tf.constant([[1., 2., 3.], [4., 5., 6.]])

教程然后提供以下代码:

t[..., 1, tf.newaxis]

我想知道省略号 ... 代表什么?

我在 Tensorflow API 中找不到任何对此语法的引用。

最佳答案

“...”表示指定维度“之前的所有维度”。

所以,在这个例子中:

t = tf.constant([[1., 2., 3.], [4., 5., 6.]])
t.shape
# OP: TensorShape([2, 3])

现在添加newaxis

t[..., 1, tf.newaxis]
# OP: <tf.Tensor: shape=(2, 1), dtype=float32, numpy=
# array([[2.],
#       [5.]], dtype=float32)>
t[..., 1, tf.newaxis].shape
# OP: TensorShape([2, 1])

如果没有 newaxis,它基本上会选择所有行 (...) 和第一列。形状将是 (2,)。现在,使用 newaxis,形状为 (2,1)。

关于python - 张量 t[..., 1, tf.newaxis] ... 代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69331522/

相关文章:

python - TensorFlow 无法将字符串转换为数字

tensorflow - Tensorflow 服务的批量预测问题

Python:是否可以事先知道迭代器对象中有多少次迭代?

python - 如何根据 Pandas 中另一列的条件比较同一列中的日期?

python - 查找我的套接字的公共(public) IP 地址?

python - 为什么即使我设置了随机种子也无法在 Keras 中获得可重现的结果?

python - 如何从字典列表中的字典中获取值

python - 如何分析 Python 中的声音输出?

python - 如何在Keras中获取预测错误数据的索引?

python-3.x - 如何将标记化应用于 TensorFlow 数据集?