4

I have tried something like this:

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())

    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter('logs', sess.graph)
    for iteration in range(int(n_epochs*train_set_size/batch_size)):
        x_batch, y_batch = get_next_batch(batch_size) # fetch the next training batch 

        sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) 

        if iteration % int(1*train_set_size/batch_size) == 0:
            mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) 
            mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) 
            mse_test = loss.eval(feed_dict={X: x_test, y: y_test})
            y_train_pred,summary1,outimage = sess.run([outputs,merged,out_img_sum], feed_dict={X: x_train,y:y_train})
            y_valid_pred,summary2 = sess.run([outputs,merged], feed_dict={X: x_valid,y:y_valid})
            y_test_pred,summary3 = sess.run([outputs,merged], feed_dict={X: x_test,y:y_test})
            writer.add_summary(summary1, iteration*batch_size/train_set_size)

I am willing to show the y_train and y_train_pred values on the tensorboard. How I can do that? These are like the arrays and I am not getting a way out to show these values comparison on Tensorboard. Please help me.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139

1 Answers1

1

Update:

Yes, you can plot along x-axis. The reason why you get the wrong images on tensorboard is because int(iteration*float(batch_size)/train_set_size) always return the same value(0.0001804630682330861 according to you). I created a similar code below like your situation(since I don't have your data). And it works very well.

import tensorflow as tf
import numpy as np

summary_writer = tf.summary.FileWriter('/tmp/test')

for iteration in range(5):
    y_train_preds = np.random.rand(10)
    summary = tf.Summary()
    for idx, value in enumerate(y_train_preds):
        summary.value.add(tag='y_train', simple_value=value)
        summary_writer.add_summary(summary, iteration*len(y_train_preds)+idx)

summary_writer.close()

Output on tensorboard

enter image description here

The only point need to notice is that make sure global step in add_summary() should increase every time.

May be you can try the following: I've updated your code for you to try as well

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())

    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter('logs', sess.graph)
    for iteration in range(int(n_epochs*train_set_size/batch_size)):
        x_batch, y_batch = get_next_batch(batch_size) # fetch the next training batch 

        sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) 

        if iteration % int(1*train_set_size/batch_size) == 0:
            summary = tf.Summary()
            mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) 
            mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) 
            mse_test = loss.eval(feed_dict={X: x_test, y: y_test})
            y_train_pred,summary1,outimage = sess.run([outputs,merged,out_img_sum], feed_dict={X: x_train,y:y_train})
            y_valid_pred,summary2 = sess.run([outputs,merged], feed_dict={X: x_valid,y:y_valid})
            y_test_pred,summary3 = sess.run([outputs,merged], feed_dict={X: x_test,y:y_test})
            for value in y_train:
                summary.value.add(tag='y_train', simple_value=value)
            for idx, value in enumerate(y_train_pred):
                summary.value.add(tag='y_train_pred', simple_value=value)
                writer.add_summary(summary, iteration*len(y_train_pred)+idx)
            writer.add_summary(summary1, int(iteration*float(batch_size)/train_set_size))

Reference post: tensorboard with numpy array

R.yan
  • 2,214
  • 1
  • 16
  • 33
  • I liked the suggestion. I will try it and if it worked for me I will accept your answer for sure, amigo. – Jaffer Wilson Sep 06 '18 at 10:44
  • Sorry but I am not getting that properly plotted on the graphs. see the image I get only a single line instead of a curve or something that is the graph. I tried it matplotlib and it was something different: https://ibb.co/cgvTaK and the image with matplotlib is something like this: https://ibb.co/eLsc2z I want to see something similar – Jaffer Wilson Sep 06 '18 at 11:18
  • this is probably (if you are using python2) because (batch_size/train_set_size) always returns 0 because they are 2 ints and the division between 2 ints alwayrs returns an int. To solve it just cast one of the two values to float (e.g. float(batch_size)/train_set_size) and then cast the whole expression to int (int(iteration*float(batch_size)/train_set_size)). I edited the answer to reflect these changes – Tommaso Pasini Sep 06 '18 at 11:32
  • @Jaffer Wilson Can you print out the value of batch_size/train_set_size. – R.yan Sep 06 '18 at 11:35
  • @R.yan Sure I will show you what the values are .. Uno moment please. – Jaffer Wilson Sep 06 '18 at 11:53
  • @TommasoPasini I am using Python 3.5 and I do not using Python 2 anyways. It gives me a lot a complications which I can avoid in Python 3 quite often. Well, gracias for helping. – Jaffer Wilson Sep 06 '18 at 11:55
  • @R.yan The values are something like this: `0.0001804630682330861`,`0.0001804630682330861`,`0.0001804630682330861`, and so on. It this correct what you wanted, amigo? – Jaffer Wilson Sep 06 '18 at 11:57
  • Just remove *batch_size/train_set_size and try again? – R.yan Sep 06 '18 at 12:07
  • Then just create a new variable that starts from 0 and you update everytime you print the summary: ``writer.add_summary(summary, counter) counter +=1`` – Tommaso Pasini Sep 06 '18 at 13:20
  • Ok I will try it out. Just let me check it. – Jaffer Wilson Sep 06 '18 at 13:46
  • Is there a way to change the values of the lower or x-axis in the figure? Can we give our own range for that? – Jaffer Wilson Sep 06 '18 at 13:51
  • if you need more control on the chart just save the values and plot it via pyplot lib at the end of the training – Tommaso Pasini Sep 06 '18 at 14:12
  • Is there no way to use the Tensorboard for the plotting purpose? – Jaffer Wilson Sep 06 '18 at 14:14
  • To my knowledge you can't modify the range but it should adjust automatically in order to fit the number of ticks you printed via add_summary – Tommaso Pasini Sep 06 '18 at 14:52
  • @JafferWilson I've updated the answer above, please have a look. – R.yan Sep 07 '18 at 02:49
  • @R.yan Thank you for your updation I will try it out and let you know. – Jaffer Wilson Sep 10 '18 at 05:09