Skip to content

Working with Keras#

Open in Colab

Custom dashboard displaying metadata logged with Keras

Keras is an API built on top of TensorFlow.

The Neptune–Keras integration logs the following metadata automatically:

  • Model summary
  • Parameters of the optimizer used for training the model
  • Parameters passed to during the training
  • Current learning rate at every epoch
  • Hardware consumption and stdout/stderr output during training
  • Training code and Git information

You can also use set the log_model_diagram option to True to save the model visualization produced by the Keras functions model_to_dot() and plot_model().

See example in Neptune  Code examples 


Before you start#


If you'd rather follow the guide without any setup, you can run the example in Colab .

Installing the Neptune–Keras integration#

On the command line or in a terminal app, such as Command Prompt, enter the following:

pip install neptune-tensorflow-keras
conda install -c conda-forge neptune-tensorflow-keras

Keras logging example#

This example shows how to use NeptuneCallback to log metadata as you train your model with Keras.

  1. Create a run:

    import as neptune
    run = neptune.init_run()  # (1)
    1. If you haven't set up your credentials, you can log anonymously: neptune.init_run(api_token=neptune.ANONYMOUS_API_TOKEN, project="common/tf-keras-integration")
  2. Initialize the Neptune callback and pass it to

    from import NeptuneCallback
    neptune_callback = NeptuneCallback(run=run)  # (1)
    1. You can customize the base namespace (folder) of your logged metadata here, by supplying the base_namespace argument and setting it to a name of your choice. The default is "training".
  3. Run your script as you normally would.

    To open the run, click the Neptune link that appears in the console output.

    Example link:

  4. Monitor your Keras training in Neptune.

    In the left pane of the run view, select Charts to watch the training metrics live.


    You can monitor the hardware consumption in the Monitoring section.

Stop the run when done

Once you are done logging, you should stop the Neptune run. You need to do this manually when logging from a Jupyter notebook or other interactive environment:


If you're running a script, the connection is stopped automatically when the script finishes executing. In notebooks, however, the connection to Neptune is not stopped when the cell has finished executing, but rather when the entire notebook stops.

More options#

Enabling logging on batch#

You can set the callback to log metrics for each batch, in addition to each epoch, by tweaking the callback:

neptune_callback = NeptuneCallback(run=run, log_on_batch=True)

Saving the model visualization#

You can save the model visualization diagram by setting the log_model_diagram parameter to True.


This option requires pydot to be installed; otherwise it will silently skip saving the diagram.

import as neptune
from import NeptuneCallback

run = neptune.init_run()

neptune_callback = NeptuneCallback(run=run, log_model_diagram=True)

Logging model weights#

You can log model weights to Neptune both during and after training.

  • To have all the metadata in a single place, you can log model metadata to the same run you created earlier.
  • To manage your model metadata separately, you can use the Neptune model registry.
import glob"my_model")


for name in glob.glob("my_model/variables/*"):

Initialize a ModelVersion object.

You first need to create a Model object that functions as an umbrella for all the versions. You can create and manage each model version separately.

# Create a new model and give it a key
neptune_model = neptune.init_model(key="PRETRAINED")
# If you like, log some generic metadata that should be common to
# all the model versions

# Create a specific version of that model by passing the model ID
model_id = neptune_model["sys/id"].fetch()
model_version = neptune.init_model_version(model=model_id)

# Log metadata to the model version, just like you would for runs 
import glob"my_model")


for name in glob.glob("variables/*"):

The model metadata will now be displayed in the Models tab of the app.

Logging test sample images#

You can log sample images to Neptune with the log() method, which will create a series of images.

from import File

for image in x_test[:100]:

See in Neptune 

Manually logging metadata#

If you have other types of metadata that are not covered in this guide, you can still log them using the Neptune client library (neptune-client).

When you initialize the run, you get a run object, to which you can assign different types of metadata in a structure of your own choosing.

from import neptune
from import NeptuneCallback

# Create a new Neptune run
run = neptune.init_run()

# Log metrics using NeptuneCallback
neptune_callback = NeptuneCallback(run=run), y_train, batch_size=32, callbacks=[neptune_callback])

# Use the same run to upload files

# Use the same run track and version artifacts

# Use the same run to log numbers or text
run["tokenizer"] = "regexp_tokenize"