TensorFlow自动求梯度

例1

import tensorflow as tf

a=tf.Variable(tf.constant(1.0),name='a')
b=tf.Variable(tf.constant(1.0),name='b')
cost=a+b
train_op=tf.train.GradientDescentOptimizer(learning_rate=2).minimize(cost)
print(tf.trainable_variables())

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    writer=tf.summary.FileWriter('./tensorboard_dir',sess.graph)
    print('initial value: ',sess.run([a,b,cost]))
    print('train_op 1 step: ',sess.run([train_op,a,b,cost]))
    print('train_op 2 step: ',sess.run([train_op,a,b,cost]))
    writer.close()

注意: tensorboard --logdir=tensorboard_dir,logdir的值不要用单引号包起来
img

posted on 2018-12-16 09:45  Frank_Allen  阅读(414)  评论(0编辑  收藏  举报

导航