TensorFlow基本概念

1 基本概念

TensorFlow基本概念

TensorFlow基本概念

2 变量

常量:tf.constant()

变量:tf.Variable()

mport tensorflow as tf
import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"  # 忽略tensorflow警告信息

x = tf.Variable(0)
new_x = tf.add(x,1)
update = tf.assign(x,new_x) #x必须是通过tf.Variable()创建的tensor,不能是常量

with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    for _ in range(5):
        print(sess.run(x))
        sess.run(update)
变量可以被赋值,tf.assign操作。常量不行