import tensorflow as tf
x = tf.Variable(0.0)
a = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(a)
y = tf.add(x, 10.0) #tensorflow中定义的变量必须用tensorflow中提供的函数进行运算
#得到的结果才能依然是变量
sess.run(tf.assign(x,y)) #tensorflow中对变量x进行重新赋值并更新
print(x) # x type is <tf.Variable 'Variable:0' shape=() dtype=float32_ref>
print(x.eval()) # x value is 10.0
x+=5 #tensorflow中定义的变量与一个数值进行运算,结果变量类型变成张量
print(x) # x type is Tensor("add:0", shape=(), dtype=float32)
tensorflow中定义的变量与数值相加,类型将发生改变。
最新推荐文章于 2024-08-07 20:27:31 发布