前面文章已经说过variable对象的本质,在C里面,是支持浮点数和整数类型数据相互赋值的,只不过某些情况存在精度损失,例如:
int a=0;
a=55.5555555;
此时a的值为55
再看:
float b=0.0;
b=55;
此时b的值为55.0
再看python代码:
import tensorflow as tf
a=tf.Variable(1.22)
op3=tf.global_variables_initializer()
with tf.Session() as sess:
print(sess.run(op3))
print(sess.run(a))
print(sess.run(tf.assign(a,15)))
输出为:
1.22
15.0
这里跟C一样,给浮点数赋整形变量是可以的
再看代码:
import tensorflow as tf
a=tf.Variable(1)
op3=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(op3)
print(sess.run(a))
print(sess.run(tf.assign(a,15.5556)))
这里就要报错:Expected int32 passed to parameter ‘value’ of op ‘Assign’, got 15.5556 of type ‘float’ instead.
给整形变量赋浮点数就不行了,但C里面是可以的