Search code examples
pythontensorflowmachine-learningtensorboard

Trouble with tensorboard two 1D graphs instead of one 2D graph


I'm curently working on a machine-learning project. I use Python3 with tensorflow to train a CNN neural network and I would like to measure its performance by using tensorboard.

I would like to measure the loss value per epoch. But instead of having only 1 graph with in X the value of the epoch and in Y the value of the loss, I have 2 graphs, one with epochs value and the other the loss value.

I put a screen-shot here:

enter image description here

There is the training part of my code:

with tf.Session(config = config) as sess:

    sess.run(tf.global_variables_initializer())

    writer = tf.summary.FileWriter(graphDirectory, sess.graph)

    # Generate shepp logan for validation
    x_arr_validate, y_arr_validate, x_true_arr_validate,  y_true_arr_validate = generateData(datasize,nbiter,reco_space,operator,pseudoinverse,validation=True                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        )
    for step in tqdm(range(epoch)):

        #Generate trading data
        x_arr, y_arr, x_true_arr, y_true_arr = generateData(datasize,nbiter,reco_space,operator,pseudoinverse)

        #Training
        feed_dict = {x0: x_arr,
                x_true: x_true_arr,
                y: y_arr}

        _,loss_training = sess.run([optimizer, loss], feed_dict)


        #Validation
        feed_dictValidate = {x0 : x_arr_validate,
                x_true : x_true_arr_validate,
                y : y_arr_validate}

        x_values_result, loss_result = sess.run([x_values, loss], feed_dictValidate)

        lossSummary = tf.Summary(value=[tf.Summary.Value(tag="loss", simple_value=loss_result)])
        epochSummary = tf.Summary(value=[tf.Summary.Value(tag="epoch", simple_value=step)])
        writer.add_summary(lossSummary)
        writer.add_summary(epochSummary)


    saver.save(sess, sessFileName,write_meta_graph=True)
    writer.close()

I try to change:

writer.add_summary(lossSummary)
writer.add_summary(epochSummary)

By:

writer.add_summary(lossSummary,epochSummary)

But that doesn't work.

I aslo try to create an array:

step_per_epoch = []

...

x_values_result, loss_result = sess.run([x_values, loss], feed_dictValidate)
 step_per_epoch.append(loss_result)

lossSummary = tf.Summary(value=[tf.Summary.Value(tag="loss",   simple_value=step_per_epoch)])

writer.add_summary(lossSummary)

But got the following error:

lossSummary = tf.Summary(value=[tf.Summary.Value(tag="loss", simple_value=loss_per_epoch)])
TypeError: [] has type list, but expected one of: int, long, float

I have no idea. Any hints or tips ? Thanks you


Solution

  • If you want to see the epochs in the horizontal axis, then you have to pass a global_step parameter along with the summary (see the documentation for tf.summary.FileWriter.add_summary). In your case, that would be:

    writer.add_summary(lossSummary, step)
    writer.add_summary(epochSummary, step)
    

    Alternatively, if you change the "Horizontal Axis" selection in this panel:

    Horizontal axis panel

    From "Step" to "Relative" or "Wall" you will have relative or absolute time stamps in the X axis, which will allow you to see the progress.