sách gpt4 ăn đã đi

python - 通过 while_loop 进行 Tensorflow 梯度

In lại Tác giả: Walker 123 更新时间:2023-11-30 09:17:50 29 4
mua khóa gpt4 giày nike

我有一个 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_propTruehoặ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 - 总的来说,尤其是这里。

câu trả lời hay nhất

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

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

29 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress