Category: Keras Faq’s

  • How can I ensure my training run can recover from program interruptions?

    To ensure the ability to recover from an interrupted training run at any time (fault tolerance), you should use a keras.callbacks.BackupAndRestore callback that regularly saves your training progress, including the epoch number and weights, to disk, and loads it the next time you call Model.fit().

  • Why is my training loss much higher than my testing loss?

    A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time. They are reflected in the training time loss but not in the test time loss. Besides, the training loss that Keras displays is the average of the losses for each batch…

  • Training-related questions

    What do “sample”, “batch”, and “epoch” mean? Below are some common definitions that are necessary to know and understand to correctly utilize Keras fit():

  • How can I install HDF5 or h5py to save my models?

    In order to save your Keras models as HDF5 files, Keras uses the h5py Python package. It is a dependency of Keras and should be installed by default. On Debian-based distributions, you will have to additionally install libhdf5: If you are unsure if h5py is installed you can open a Python shell and load the module…

  • What are my options for saving models?

    Note: it is not recommended to use pickle or cPickle to save a Keras model. 1) Whole-model saving (configuration + weights) Whole-model saving means creating a file that will contain: The default and recommended way to save a whole model is to just do: model.save(your_file_path.keras). After saving a model in either format, you can reinstantiate it…

  • How can I obtain reproducible results using Keras during development?

    There are four sources of randomness to consider: To make both Keras and the current backend framework deterministic, use this: To make Python deterministic, you need to set the PYTHONHASHSEED environment variable to 0 before the program starts (not within the program itself). This is necessary in Python 3.2.3 onwards to have reproducible behavior for certain hash-based operations (e.g.,…

  • Where is the Keras configuration file stored?

    The default directory where all Keras data is stored is: $HOME/.keras/ For instance, for me, on a MacBook Pro, it’s /Users/fchollet/.keras/. Note that Windows users should replace $HOME with %USERPROFILE%. In case Keras cannot create the above directory (e.g. due to permission issues), /tmp/.keras/ is used as a backup. The Keras configuration file is a JSON file stored at $HOME/.keras/keras.json. The default…

  • How can I train a Keras model on TPU?

    TPUs are a fast & efficient hardware accelerator for deep learning that is publicly available on Google Cloud. You can use TPUs via Colab, Kaggle notebooks, and GCP Deep Learning VMs (provided the TPU_NAME environment variable is set on the VM). All Keras backends (JAX, TensorFlow, PyTorch) are supported on TPU, but we recommend JAX or TensorFlow…