Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 600
OverLordGoldDragon Avatar asked Jul 06 '19 20:07

OverLordGoldDragon


3 Answers

Colab now allows direct access to system files from the GUI itself. There one can view and edit all the installed libraries like one would have done on their pc itself.

Google colab screen shot

Go to the Files icon in the left Sidebar. Go to the Up Folder. From there go to the path

usr/local/lib/python3.6/dist-packages

Here, find the package and make your edit. Then restart the runtime, from Runtime/Restart Runtime option in the menu.

like image 170
Pranjal Agarwal Avatar answered Sep 28 '22 05:09

Pranjal Agarwal


You could fork the libraries on GitHub, push your changes to a new branch and then do.

!pip install git+https://github.com/your-username/keras.git@new-branch

Or even a specific commit

!pip install git+https://github.com/your-username/keras.git@632560d91286

You will need to restart your runtime for the changes to work.

More details here.

like image 33
fizzybear Avatar answered Sep 28 '22 05:09

fizzybear


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.

like image 24
OverLordGoldDragon Avatar answered Sep 28 '22 07:09

OverLordGoldDragon