TensorFlow 编程训练3

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_36669549/article/details/79766332

变量问题

1.定义变量一定要初始化,新版本的初始化用“tf.global_variables_initializer”

2.定义Session后一定要“run”

3.print 的时候一定print".run"

import tensorflow as tf

state = tf.Variable(0, name='counter')
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

展开阅读全文

没有更多推荐了,返回首页