TensorFlow Variables and Constants

后端 未结 2 732
粉色の甜心
粉色の甜心 2020-12-24 02:34

I am new to tensorflow , I am not able to understand the difference of variable and constant, I get the idea that we use variables for equations and constants for direct val

2条回答
  •  佛祖请我去吃肉
    2020-12-24 03:01

    In TensorFlow the differences between constants and variables are that when you declare some constant, its value can't be changed in the future (also the initialization should be with a value, not with operation).

    Nevertheless, when you declare a Variable, you can change its value in the future with tf.assign() method (and the initialization can be achieved with a value or operation).

    The function tf.global_variables_initializer() initialises all variables in your code with the value passed as parameter, but it works in async mode, so doesn't work properly when dependencies exists between variables.

    Your first code (#1) works properly because there is no dependencies on variable initialization and the constant is constructed with a value.

    The second code (#2) doesn't work because of the async behavior of tf.global_variables_initializer(). You can fix it using tf.variables_initializer() as follows:

    x = tf.Variable(35, name='x')
    model_x = tf.variables_initializer([x])
    
    y = tf.Variable(x + 5, name='y')
    model_y = tf.variables_initializer([y])
    
    
    with tf.Session() as session:
       session.run(model_x)
       session.run(model_y)
       print(session.run(y))
    

    The third code (#3) doesn't work properly because you are trying to initialize a constant with an operation, that isn't possible. To solve it, an appropriate strategy is (#1).

    Regarding to your last question. You need to run (a) session.run(model) when there are variables in your calculation graph (b) print(session.run(y)).

提交回复
热议问题