Search code examples
kerasjupyter-notebookgoogle-colaboratory

How to edit Google Colaboratory libraries?


I edited Keras .optimizer and .layers modules locally, but Colab uses its own Keras & TensorFlow libraries. Uploading then using the edited libs would be rather involved per pathing and package interactions, and an overkill for a few small edits.

The closest I've got to accessing a module is keras.optimizers.__file__, which gives a relative path I don't know what to do with: '/usr/local/lib/python3.6/dist-packages/keras/optimizers.py'

Can Colab libraries be edited? Permanently (not per-runtime)?


Solution

  • Per-runtime solution

    import keras.optimizers
    
    with open('optimizers.txt','r') as writer_file:
        contents_to_write = writer_file.read()
    with open(keras.optimizers.__file__,'w') as file_to_overwrite:
        file_to_overwrite.write(contents_to_write)
    

    >>Restart runtime (do not 'Reset all runtimes')


    To clarify, (1) save edited module of interest as a .txt, (2) overwrite Colab module with the saved module via .__file__, (3) 'Reset all runtimes' restores Colab modules - use if module breaks

    Considering its simplicity, it's as good as a permanent fix. For possibly better scalability, see fizzybear's solution.