【问题标题】:Tensorboard no graphTensorboard 无图
【发布时间】:2018-01-28 21:06:19
【问题描述】:

我正在尝试使用 tensorboard 来可视化网络图。下面是关于 MNIST 分类的简单 CNN 代码。代码来自tensorboard tutorial

代码:

import os
import tensorflow as tf
import urllib

GIST_URL = 'https://gist.githubusercontent.com/dandelionmane/4f02ab8f1451e276fea1f165a20336f1/raw/dfb8ee95b010480d56a73f324aca480b3820c180'
LOGDIR = '/tmp/mnist_tutorial/'

### MNIST EMBEDDINGS ###
mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + 'data', one_hot=True)

# Define a simple convolutional layer
def conv_layer(input, channels_in, channels_out):
    w = tf.Variable(tf.zeros([5, 5, channels_in, channels_out]))
    b = tf.Variable(tf.zeros([channels_out]))
    conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="SAME")
    act = tf.nn.relu(conv + b)
    return act

def fc_layer(input, channels_in, channels_out):
    w = tf.Variable(tf.zeros([channels_in, channels_out]))
    b = tf.Variable(tf.zeros([channels_out]))
    act = tf.nn.relu(tf.matmul(input, w) + b)
    return act

def make_hparam_string(learning_rate, use_two_fc, use_two_conv):
  conv_param = "conv=2" if use_two_conv else "conv=1"
  fc_param = "fc=2" if use_two_fc else "fc=1"
  return "lr_%.0E,%s,%s" % (learning_rate, conv_param, fc_param)

 # Setup placeholders, and reshape the data
x = tf.placeholder(tf.float32, shape=[None, 784])
y = tf.placeholder(tf.float32, shape=[None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])
# Create the network
conv1 = conv_layer(x_image, 1, 32)
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
conv2 = conv_layer(pool1, 32, 64)
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
flattened = tf.reshape(pool2, [-1, 7 * 7 * 64])
fc1 = fc_layer(flattened, 7 * 7 * 64, 1024)
logits = fc_layer(fc1, 1024, 10)



# Compute cross entropy as our loss function
cross_entropy = tf.reduce_mean(
      tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
# Use an AdamOptimizer to train the network
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# compute the accuracy
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

sess = tf.Session()
# Initialize all the variables
sess.run(tf.global_variables_initializer())
hparam = make_hparam_string( .1,True,True)

writer = tf.summary.FileWriter(LOGDIR+hparam)
writer.add_graph(sess.graph)
# Train for 2000 steps
for i in range(20):
    batch = mnist.train.next_batch(100)
    # Occasionally report accuracy
    if i % 5 == 0:
      [train_accuracy] = sess.run([accuracy], feed_dict={x: batch[0], y: batch[1]})
      print("step %d, training accuracy %g" % (i, train_accuracy))
    # Run the training step
    sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})
writer.close()

图表不存在!为什么?我也关闭了作者。 (正如这篇文章中提到的there is no graph with tensorboard)。我不确定我错过了什么。

张量板:

$ tree mnist_tutorial/
mnist_tutorial/
├── data
│   ├── t10k-images-idx3-ubyte.gz
│   ├── t10k-labels-idx1-ubyte.gz
│   ├── train-images-idx3-ubyte.gz
│   └── train-labels-idx1-ubyte.gz
└── lr_1E-01,conv=2,fc=2
    └── events.out.tfevents.1503327291.neon-2.local

2 directories, 5 files

张量板日志目录应该是什么。我假设它是 lr_1E-01,conv=2,fc=2 因为它包含事件文件,并被传递给 FileWriter。

【问题讨论】:

  • 我只是复制粘贴并运行你的代码,结束后我启动了 tensorboard,这只是工作,可能是你启动 tensorboard 时 --logdir 指向了错误的目录?应该指向 /tmp/mnist_tutorial 而不是你在构建时传递给作者的 hparam 子目录。
  • @amo-ej1 我很困惑为什么会这样。我不应该使用writer = tf.summary.FileWriter(LOGDIR+hparam) 吗?那就是我传递给 FileWriter 的内容?
  • 我把它指向 /tmp/mnist_tutorial 然后如果你有多个运行,你可以把它们放在不同的子目录中(例如这里讨论:stackoverflow.com/questions/36182380/…

标签: python tensorflow tensorboard


【解决方案1】:

您使用的是 tensorflow windows 版本? 试试下面的代码:

tf.train.write_graph(sess.graph_def, LOGDIR+hparam, 'graph.pb', False)

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 2021-05-30
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2016-04-20
    • 2019-08-08
    相关资源
    最近更新 更多