this is my code:
import tensorflow as tf
import tensorboardcolab as tb
tbc = tb.TensorBoardColab()
writer= tbc.get_writer()
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 10
batch_size = 100
tf.reset_default_graph()
x = tf.placeholder('float', [None, 784], name='input_data')
y = tf.placeholder('float', [None, 10], name='labels')
def neural_network_model(data):
with tf.name_scope('feedforward'):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes])),}
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
tf.summary.histogram("l1", l1)
tf.summary.histogram("l2", l2)
tf.summary.histogram("l3", l3)
output = tf.add(tf.matmul(l3,output_layer['weights']),output_layer['biases'])
return output
def train_neural_network(x):
prediction = neural_network_model(x)
with tf.name_scope('cost'):
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
writer.add_graph(sess.graph)
for epoch in range(hm_epochs):
epoch_loss = 0
for t in range(int(mnist.train.num_examples/batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
if t%50 == 0:
val_acc = accuracy.eval({x:mnist.test.images, y:mnist.test.labels})
print('Accuracy:',val_acc)
print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)
# acc_summ = tf.summary.scalar('val_acc', val_acc)
# cost_summ = tf.summary.scalar('epoch_loss',epoch_loss)
# merged = tf.summary.merge([cost_summ, acc_summ])
# writer.add_summary(merged)
train_neural_network(x)
It runs fine when the commented parts are commented out. However, I don't get why whenwriter.add_summary() is uncommented out I get the following error:
AttributeError Traceback (most recent call last)
<ipython-input-204-e776a1e8e4f1> in <module>()
----> 1 train_neural_network(x)
1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/summary/writer/writer.py in add_summary(self, summary, global_step)
125 # to save space - we just store the metadata on the first value with a
126 # specific tag.
--> 127 for value in summary.value:
128 if not value.metadata:
129 continue
AttributeError: 'Tensor' object has no attribute 'value'
It doesn't make sense to me. The docs for the FileWriter class clearly states that it takes in a list of String tensors, and the docs for the tf.summary.scalar says that it returns a tensor of type string with summary protocol, so it seems like it should work.
Here's what I've tried, by people who seemed to have the same issue as me, but nothing worked:
Tensorboard expects a string for summary, adding eval() didn't work.
The value error is raised because you have to evaluate the summary node within a session. I am already running it in session.
Merges all summaries in the default graph. Initially, I was using merging all summaries, but now I switched to tf.summary.merge.
Coming from improperly connected tensors. I am using tf.matmul and tf.add here instead of regular operands.
Another thing I tried was putting tensor objects into the second argument of tf.summary.scalar, something like replacing the commented part of the code above like this:
cost_summ = tf.summary.scalar('epoch_loss',c)
merged = tf.summary.merge([cost_summ])
writer.add_summary(merged)
But event that didn't seem to work.
Why doesn't my above code work, while this code here works? I don't see a difference.
import tensorflow as tf
a = tf.summary.scalar('a', tf.constant(0))
b = tf.summary.scalar('b', tf.constant(1))
c = tf.summary.scalar('c', tf.constant(2))
d = tf.summary.scalar('d', tf.constant(3))
ab = tf.summary.merge([a, b])
cd = tf.summary.merge([c, d])
abcd = tf.summary.merge([ab, cd])
with tf.Session() as sess:
writer = tf.summary.FileWriter('.', sess.graph)
summary = sess.run(abcd)
writer.add_summary(summary)
Also, I am using Google Colab, which is why I'm importing tensorboardcolab.
add_summary()on the result ofsess.run, not thesummary.mergeresult itself.