tensorboard与tf.reset_default_graph()

打开地址问题
在命令行中输入: tensorboard –logdir=’path to log-dicrectory’,然后复制地址去浏览器打开。

tf.reset_default_graph()
train_x = np.random.randn(200,6)
train_y = np.zeros((200,2))
train_y[:100,0] = 1.0
train_y[100:,1] = 1.0
with tf.name_scope('input'):
    x = tf.placeholder(tf.float32, shape=[200,6])
    y_true = tf.placeholder(tf.float32, shape=[200,2])
with tf.name_scope('classifier'):
    weights = tf.Variable(tf.random_normal([6,2]))
    bias = tf.Variable(tf.zeros([2]))
    y_pred = tf.nn.softmax(tf.matmul(x, weights) + bias)
tf.summary.histogram('weights', weights)
tf.summary.histogram('bias', bias)
with tf.name_scope('cost'):
    cross_entropy = -tf.reduce_sum(y_true*tf.log(y_pred+(1e-10)), reduction_indices=1)
    cost = tf.reduce_mean(cross_entropy)
tf.summary.scalar('loss',cost)
train_op = tf.train.GradientDescentOptimizer(0.001).minimize(cost)
with tf.name_scope('accuracy'):
    accuracy = tf.cast(tf.equal(tf.argmax(y_pred,1), tf.argmax(y_true,1)), tf.float32)
    acc_op = tf.reduce_mean(accuracy)
tf.summary.scalar('accuracy', acc_op)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter("yaobao/", sess.graph)
    merged = tf.summary.merge_all()
    for step in range(100):
        sess.run(train_op, feed_dict={x:train_x, y_true:train_y})
        if (step+1)%10 == 0:
            summary, accuracy = sess.run([merged, acc_op],feed_dict={x:train_x, y_true:train_y})
            writer.add_summary(summary, step)

因为tf.summary.FileWriter中写的是”yaobao/”,又因为这个notebook我是放在桌面了,因此是tensorboard --logdir==C:\Users\yaobao\Desktop\yaobao
一个ERROR
You must feed a value for placeholder tensor ‘input/Placeholder’ with dtype float and shape [200,6]
在IPYTHON与notebook中运行此段代码必须在开头加上tf.reset_default_graph(), 当然有其他方法。具体原因不明

猜你喜欢

转载自blog.csdn.net/yb564645735/article/details/79494423