我有一个 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_prop
至True
hoặcFalse
在这个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
- 总的来说,尤其是这里。
Tôi là một lập trình viên xuất sắc, rất giỏi!