Saving Variable state in Colaboratory

纵然是瞬间 提交于 2019-12-08 00:01:54

问题


When I am running a Python Script in Colaboratory, it's running all previous code cell.

Is there any way by which previous cell state/output can be saved and I can directly run next cell after returning to the notebook.


回答1:


The outputs of Colab cells shown in your browser are stored in notebook JSON saved to Drive. Those will persist.

If you want to save your Python variable state, you'll need to use something like pickle to save to a file and then save that file somewhere outside of the VM.

Of course, that's a bit a trouble. One way to make things easier is to use a FUSE filesystem to mount some persistant storage where you can easily save regular files but have them persist beyond the lifetime of the VM.

An example of using a Drive FUSE wrapper to do this is in this example notebook: https://colab.research.google.com/notebook#fileId=1mhRDqCiFBL_Zy_LAcc9bM0Hqzd8BFQS3

This notebook shows the following:

  1. Installing a Google Drive FUSE wrapper.
  2. Authenticating and mounting a Google Drive backed filesystem.
  3. Saving local Python variables using pickle as a file on Drive.
  4. Loading the saved variables.



回答2:


It'a a nope. As @Bob in this recent thread says: "VMs time out after a period of inactivity, so you'll want to structure your notebooks to install custom dependencies if needed."



来源:https://stackoverflow.com/questions/47448015/saving-variable-state-in-colaboratory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!