在协作中保存变量状态 [英] Saving Variable state in Colaboratory

查看:104
本文介绍了在协作中保存变量状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行协作实验室中的Python脚本,它正在运行所有以前的代码单元.

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.

推荐答案

浏览器中显示的Colab单元格的输出存储在笔记本JSON中,并保存到云端硬盘中.这些将持续存在.

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

如果要保存Python变量状态,则需要使用类似 pickle 保存到文件,然后将该文件保存在VM外部.

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.

当然,这有点麻烦.一种使事情变得容易的方法是使用FUSE文件系统挂载一些持久性存储,您可以在其中轻松保存常规文件,但可以使它们在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.

在此示例笔记本中,使用Drive FUSE包装器执行此操作的示例是: https://colab.research.google.com/notebook#fileId=1mhRDqCiFBL_ZyQLAc9a >

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. 安装Google Drive FUSE包装器.
  2. 验证和挂载Google云端硬盘支持的文件系统.
  3. 使用pickle作为Drive上的文件保存本地Python变量.
  4. 加载已保存的变量.
  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.

这篇关于在协作中保存变量状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆