python - 通过 while_loop 进行 Tensorflow 梯度

标签 python tensorflow machine-learning

我有一个 tensorflow 模型,其中层的输出是二维张量,例如 t = [[1,2], [3,4]] .

下一层需要一个由该张量的每一行组合组成的输入。也就是说,我需要把它变成t_new = [[1,2,1,2], [1,2,3,4], [3,4,1,2], [3,4,3,4]] .

到目前为止我已经尝试过:

1) tf.unstack(t, axis=0)循环遍历它的行并将每个组合附加到缓冲区,然后 t_new = tf.stack(buffer, axis=0) 。这有效除非当形状未指定时,即。没有这样...

2)我使用了 tf.while_loop 来生成索引 idx=[[0,0], [0,1], [1,0], [1,1]] ,然后t_new = tf.gather(t, idx) 。 我的问题是:我应该设置 back_propTrueFalse在这个tf.while_loop ?我只是在循环内生成索引。不知道什么back_prop甚至意味着。

另外,您知道有更好的方法来实现我的需求吗?

这是 while_loop:

i = tf.constant(0)
j = tf.constant(0)
idx = tf.Variable([], dtype=tf.int32)
def body(i, j, idx):
    c = tf.concat([idx, [i, j]], axis=0)
    i, j = tf.cond(tf.equal(j, sentence_len - 1),
                   lambda: (i + 1, 0),
                   lambda: (i, j + 1))
    return i, j, c
_, _, indices = tf.while_loop(lambda i, j, _: tf.less(i, sentence_len),
                             body,
                             [i, j, idx],
                             shape_invariants=[i.get_shape(),
                                               j.get_shape(),
                                               tf.TensorShape([None])])

现在我可以做t_new = tf.gather(t, indices) .

但是我对 tf.while_loop 的含义很困惑的back_prop - 总的来说,尤其是这里。

最佳答案

在这种情况下,您可以将 back_prop 设置为 false。它不需要通过索引的计算进行反向传播,因为该计算不依赖于任何学习的变量。

关于python - 通过 while_loop 进行 Tensorflow 梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50518309/

相关文章:

python - 广度优先搜索算法

python - 用于从 Google 云存储呈现静态图像的 Django 设置

python - 尽管我提供的形状看起来正确,但尝试进行预测时,TensorFlow/Keras模型错误,原因是形状不匹配

python - 这个CNN的降维似乎违背了我对理论的理解

python - tf.keras 预测很差,而评估很好

java - 文本分类分类器

Python错误: list index out of range

python - 在另一个训练操作中运行训练操作

python - TensorFlow 中损失函数 (MLP) 的奇怪 NaN 值

machine-learning - 使用 3 channel 图像向后传递卷积层