gpt4 book ai didi

python - tf.while_loop 仅考虑最后一次迭代

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:52 25 4
gpt4 key购买 nike

我正在使用 tf.while_loop 动态连接张量。

代码

embeds_raw = tf.constant(np.array([
[1, 1],
[1, 1],
[2, 2],
[3, 3],
[3, 3],
[3, 3]
], dtype='float32'))
embeds = tf.Variable(initial_value=embeds_raw)
container_variable = tf.zeros([512], dtype=tf.int32, name='container_variable')
sen_len = tf.placeholder('int32', shape=[None], name='sen_len')
# max_l = tf.reduce_max(sen_len)
current_size = tf.shape(sen_len)[0]
padded_sen_len = tf.pad(sen_len, [[0, 512 - current_size]], 'CONSTANT')
added_container_variable = tf.add(container_variable, padded_sen_len)
u1 = tf.TensorArray(dtype=tf.float32, size=512, clear_after_read=False)
u1 = u1.split(embeds, added_container_variable)
res = tf.split(embeds, added_container_variable)

i = tf.constant(0, shape=(), dtype='int32', name='i')
x = tf.Variable(tf.constant(0, shape=[2, 2], dtype=tf.float32), dtype=tf.float32)

def condition(_i, _x):
return tf.less(_i, current_size)

def body(_i, _x):
return _i + 1, tf.concat([x, u1.read(_i)], axis=0)

idx, x = tf.while_loop(
condition,
body,
[i, x],
shape_invariants=[tf.TensorShape([]), tf.TensorShape([None, 2])],
)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sents = sess.run(x, feed_dict={sen_len: [2, 1, 3]})
print(sents)
print(len(res))

发生的情况是,它在每次迭代时连接,但删除修改。换句话说,新的迭代不使用之前的结果。

这是我得到的输出:

[[ 0.  0.]
[ 0. 0.]
[ 3. 3.]
[ 3. 3.]
[ 3. 3.]]

而我想要的输出是:

[[ 0.  0.]
[ 0. 0.]
[ 1. 1.]
[ 1. 1.]
[ 2. 2.]
[ 3. 3.]
[ 3. 3.]
[ 3. 3.]]

最佳答案

这是因为这一行:

return _i + 1, tf.concat([x, u1.read(_i)], axis=0)

您应该将其更改为:

return _i + 1, tf.concat([_x, u1.read(_i)], axis=0)

关于python - tf.while_loop 仅考虑最后一次迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com