python - 如何迭代 Tensorflow 中的张量?

标签 python tensorflow neural-network

假设我们有一个这样的张量x = [[[1,2],[3,4]],[[5,6],[7,8]]]。我想要一个 tensorflow 操作,以便它返回将 1 添加到第一个嵌套张量的每个元素。即操作将返回的结果 [[[2,3],[3,4]],[[6,7],[7,8]]]。 我知道 tf.map_fn 操作,但我找不到使用此操作来实现的方法。如何解决这个问题?

最佳答案

您可以在第一个轴上拆分张量,将一个添加到第一个轴上的第一个张量,然后使用 tf.stack 堆叠新旧张量:

>>> x = tf.constant([[[1, 2], [3, 4]],[[5, 6], [7, 8]]])
>>> with tf.Session() as sess:
...    sess.run(tf.stack((x[:,0] + 1, x[:, 1]), axis=1))
... 
array([[[2, 3],
        [3, 4]],

       [[6, 7],
        [7, 8]]], dtype=int32)

关于python - 如何迭代 Tensorflow 中的张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026877/

相关文章:

machine-learning - 使用 keras 示例 pretrained_word_embeddings 时出错

android - 适用于 Android 的 TensorFlow C++ 示例

python - reshape LSTM 自动编码器的输入数据

matlab - MATLAB 中的神经网络

machine-learning - Keras RNN 损失不会随 epoch 减少而减少

python - 替换 pandas DataFrame 值模式

python - 将 matplotlib 图像插入 Pandas 数据框中

python - 被 tensorflow 输出混淆

javascript - 如何访问 NTFS 主文件表 (MFT)?

python - 为什么 Tkinter 在绘图时会删除之前的矩形?