Search code examples
pythonkerasloss-functionregularized

Output the loss/cost function in keras


I am trying to find the cost function in Keras. I am running an LSTM with the loss function categorical_crossentropy and I added a Regularizer. How do I output what the cost function looks like after my Regularizer this for my own analysis?

model = Sequential()
model.add(LSTM(
    NUM_HIDDEN_UNITS,
    return_sequences=True,
    input_shape=(PHRASE_LEN, SYMBOL_DIM),
    kernel_regularizer=regularizers.l2(0.01)
    ))
model.add(Dropout(0.3))
model.add(LSTM(NUM_HIDDEN_UNITS, return_sequences=False))
model.add(Dropout(0.3))
model.add(Dense(SYMBOL_DIM))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
    optimizer=RMSprop(lr=1e-03, rho=0.9, epsilon=1e-08))

Solution

  • How do i output what the cost function looks like after my regularizer this for my own analysis?

    Surely you can achieve this by obtaining the output (yourlayer.output) of the layer you want to see and print it (see here). However there are better ways to visualize these things.

    Meet Tensorboard.

    This is a powerful visualization tool that enables you to track and visualize your metrics, outputs, architecture, kernel_initializations, etc. The good news is that there is already a Tensorboard Keras Callback that you can use for this purpose; you just have to import it. To use it just pass an instance of the Callback to your fit method, something like this:

    from keras.callbacks import TensorBoard
    #indicate folder to save, plus other options
    tensorboard = TensorBoard(log_dir='./logs/run1', histogram_freq=1,
        write_graph=True, write_images=False)  
    
    #save it in your callback list
    callbacks_list = [tensorboard]
    #then pass to fit as callback, remember to use validation_data also
    model.fit(X, Y, callbacks=callbacks_list, epochs=64, 
        validation_data=(X_test, Y_test), shuffle=True)
    

    After that, start your Tensorboard sever (it runs locally on your pc) by executing:

    tensorboard --logdir=logs/run1
    

    For example, this is what my Kernels look like on two different models I tested (to compare them you have to save separate runs and then start Tensorboard on the parent directory instead). This is on the Histograms tab, on my second layer:

    enter image description here

    The model on the left I initialized with kernel_initializer='random_uniform', thus its shape is the one of a Uniform Distribution. The model on the right I initialized with kernel_initializer='normal', thus why it appears as a Gaussian distribution throughout my epochs (about 30).

    This way you could visualize how your kernels and layers "look like", in a more interactive and understandable way than printing outputs. This is just one of the great features Tensorboard has, and it can help you develop your Deep Learning models faster and better.

    Of course there are more options to the Tensorboard Callback and for Tensorboard in general, so I do suggest you thoroughly read the links provided if you decide to attempt this. For more information you can check this and also this questions.

    Edit: So, you comment you want to know how your regularized loss "looks" analytically. Let's remember that by adding a Regularizer to a loss function we are basically extending the loss function to include some "penalty" or preference in it. So, if you are using cross_entropy as your loss function and adding an l2 regularizer (that is Euclidean Norm) with a weight of 0.01 your whole loss function would look something like:

    enter image description here