Understanding Tensorflow Repeatedly

Asked 2 years ago, Updated 2 years ago, 86 views

Currently, we are trying to implement sampling using tensorflow.
I'd like to save the results by sampling several times, but I'm having trouble with the following feeling.

Here's a quick example.

x=tf.Variable(0)
step=tf.assign_add(x,1)

Prepare the above (absolutely use the above) and try to get y=[123] by repeating step 3 times.

y=[step for_in range(3)]

As , y returns as [111].
Is it possible to run the steps several times in order (not at the same time)?

python tensorflow

2022-09-30 21:28

1 Answers

x=tf.Variable(0)
y = tf.Variable([0]*3)
def fun(i):
    step=tf.assign_add(x,1)
    assign=tf.assign(y[i], step)
    with tf.control_dependencies ([assign]):
        return i+1

with tf.Session() asess:
    tf.global_variables_initializer().run()
    result=tf.while_loop(lambdai:i<3,fun,[0])
    sess.run(result)
    print(y.eval())

Now I can do what I thought.


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.