Examples

Use Callbacks

cb1 = tf.keras.callbacks.TensorBoard()
net.fit(X,y,epochs=200,batch_size=200,validation_split=0.2,callbacks=cb1,verbose=1)
%tensorboard --logdir logs
 
from tensorboard.plugins.hparams import api as hp
 
for u in [50,5000]: 
    for d in [0.0,0.5]: 
        for o in ['adam','sgd']:
            logdir = 'logs/hp_{}_{}_{}'.format(u,d,o)
            with tf.summary.create_file_writer(logdir).as_default():
                net = tf.keras.Sequential()
                net.add(tf.keras.layers.Flatten())
                net.add(tf.keras.layers.Dense(u,activation='relu'))
                net.add(tf.keras.layers.Dropout(d))
                net.add(tf.keras.layers.Dense(10,activation='softmax'))
                net.compile(optimizer=o,loss=tf.losses.categorical_crossentropy,metrics=['accuracy','Recall'])
                cb3 = hp.KerasCallback(logdir, {'num of units':u, 'dropout ratio':d, 'optimizer':o})
                net.fit(X,y,epochs=3,callbacks=cb3)
                _rslt=net.evaluate(XX,yy)
                _mymetric=_rslt[1]*0.8 + _rslt[2]*0.2  
                tf.summary.scalar('F-beta score(Test)', _mymetric, step=1)

Append matplot fig to Tensorboard

import io
logdir = "logs" 
 
def plot_to_image(fig):
    """Converts the matplotlib plot specified by 'figure' to a PNG image and
    returns it. The supplied figure is closed and inaccessible after this call."""
    # Save the plot to a PNG in memory.
    buf = io.BytesIO()
    fig.savefig(buf, format='png')
    # Closing the figure prevents it from being displayed directly inside
    # the notebook.
    plt.close(fig)
    buf.seek(0)
    # Convert PNG buffer to TF image
    image = tf.image.decode_png(buf.getvalue(), channels=4)
    # Add the batch dimension
    image = tf.expand_dims(image, 0)
    return image
 
class PlotYhat(tf.keras.callbacks.Callback):
    def on_epoch_begin(self,epoch,logs):
        if epoch % 100 ==0: 
            fig, ax = plt.subplots() 
            ax.plot(y,'.',alpha=0.2)
            ax.plot(net(X),'--')
            with tf.summary.create_file_writer(logdir).as_default():
                tf.summary.image("적합결과시각화"+str(epoch), plot_to_image(fig), step=0)
 
 
cb= PlotYhat() 
net.fit(X,y,epochs=2000, batch_size=100, validation_split=0.45,callbacks=[cb])

Monitor Change of epoch by Epoch

net = tf.keras.Sequential()
net.add(tf.keras.layers.Dense(1))
net.compile(loss='mse',optimizer='adam')
cb1= tf.keras.callbacks.TensorBoard(update_freq='epoch',histogram_freq=100)
net.fit(X,y,epochs=2000, batch_size=100, validation_split=0.45,callbacks=cb1)