实例:1 + 1/2 + 1/2^2 + 1/2^3 + … + 1/2^50
tensorflow1
import tensorflow as tf
print(tf.__version__)
x = tf.Variable(0.)
y = tf.Variable(1.)
add_op = x.assign(x + y)
div_op = y.assign(y / 2)
with tf. Session() as sess:
sess.run(tf.global_variables_initializer())
for iteration in range(50):
sess.run(add_op)
sess.run(div_op)
print(x.eval())
tensorflow2
import tensorflow as tf
print(tf.__version__)
x = tf.constant(0.)
y = tf.constant(1.)
for iteration in range(50):
x = x + y
y = y / 2
print(x)
print(x.numpy())