pythontensorflowcallbacktflearn

How can I, with TFLearn, interact with the metrics of the training (current validation accuracy, training accuracy etc?


This is an example from the TFLearn documentation. It shows how to combine TFLearn and Tensorflow, using a TFLearn trainer with a regular Tensorflow graph. However, the current training, test and validation accuracy calculations are not accessible.

import tensorflow as tf
import tflearn
    ...   
# User defined placeholders
with tf.Graph().as_default():
    # Placeholders for data and labels
    X = tf.placeholder(shape=(None, 784), dtype=tf.float32)
    Y = tf.placeholder(shape=(None, 10), dtype=tf.float32)

    net = tf.reshape(X, [-1, 28, 28, 1])

    # Using TFLearn wrappers for network building
    net = tflearn.conv_2d(net, 32, 3, activation='relu')
    .
    .
    .
    net = tflearn.fully_connected(net, 10, activation='linear')

    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(
            logits=net, 
            labels=Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)

    # Initializing the variables
    ...
    # Launch the graph
    with tf.Session() as sess:
        sess.run(init)
    ...
        for epoch in range(2):  # 2 epochs
    ...
            for i in range(total_batch):
                batch_xs, batch_ys = mnist_data.train.next_batch(batch_size)
                sess.run(optimizer, feed_dict={X: batch_xs, Y: batch_ys})

How do I access the calculated training and validation accuracy at each step in the nested FOR loop?


UPDATE FOR CLARITY:

A solution might be as follows: Using the fit_batch method of the Trainer class, I believe I am calculating the training and validation accuracy during the nested loop.

Does this code calculate the running accuracies as the model trains? Is there a better way of doing this with TFLearn?

I understand that tensorboard uses these values. Could I retrieve the values from the eventlogs?

def accuracy(predictions, labels):
return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
                / predictions.shape[0])
...    
network = input_data(shape=[None, image_size, image_size, num_channels],
                     data_preprocessing=feature_normalization,
                     data_augmentation=None,
                     name='input_d')
.
.
.
network = regression(network, optimizer='SGD',
                    loss='categorical_crossentropy',
                    learning_rate=0.05, name='targets')

model_dnn_tr = tflearn.DNN(network, tensorboard_verbose=0)
...
with tf.Session(graph=graph) as session:
...
    for step in range(num_steps):
    ...
        batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
        batch_labels = train_labels[offset:(offset + batch_size), :]

        loss = model_dnn_tr.fit_batch({'input_d' : batch_data}, {'targets': 
            batch_labels})

        if (step % 50 == 0):
            trainAccr = accuracy(model_dnn_tr.predict({'input_d' : 
                batch_data}), batch_labels)

            validAccr = accuracy(model_dnn_tr.predict({'input_d' : 
                valid_dataset}), valid_labels)

testAccr = accuracy(model_dnn_tr.predict({'input_d' : test_dataset}), 
    test_labels)

Solution

  • UPDATE with The correct answer

    Could I retrieve the values from the eventlogs?

    Tensorboard does have a means to download the accuracy datasets, but making use of it during training is problematic.

    Does this code calculate the running accuracies as the model trains?

    In a word. Yes.

    The fit_batch method works as one might expect; as does the initial solution I posted below.

    However, neither is the prescribed method.

    Is there a better way of doing this within TFLearn?

    Yes!

    In order to o track and interact with the metrics of the training, a Training Callback function should be implemented.

    from tflearn import callbacks as cb
    
    class BiasVarianceStrategyCallback(cb.Callback):
        def __init__(self, train_acc_thresh,run_id,rel_err=.1):
            """ Note: We are free to define our init function however we please. """
            def errThrshld(Tran_accuracy=train_acc_thresh,relative_err=rel_err):
                Tran_err = round(1-Tran_accuracy,2)
                Test_err = ...
                Vald_err = ...
                Diff_err = ...
                return {'Tr':Tran_err,'Vl':Vald_err,'Ts':Test_err,'Df':Diff_err}
            return
    
        def update_acc_df(self,training_state,state):
            ...
            return
        def on_epoch_begin(self, training_state):
            """ """
            ...
            variance_found = ...
            if trn_acc_stall or vld_acc_stall:
                print("accuracy increase stalled. training epoch:"...
                if trn_lss_mvNup or vld_lss_mvNup:
                    print("loss began increase training:"...
                    raise StopIteration
                    return
                if variance_found or bias_found:
                    print("bias:",bias_found,"variance:",variance_found)
                    raise StopIteration
                    return
            return
        def on_batch_end(self, training_state, snapshot=False):
            self.update_acc_df(training_state,"batch")
            return
        def on_epoch_end(self, training_state):
            self.update_acc_df(training_state,"epoch")
            return
        def on_train_end(self, training_state):
            self.update_acc_df(training_state,"train")
            self.df = self.df.iloc[0:0]
            return
    

    Initial solution

    The most satisfying solution I found thus far: