在 Google Colab 上获取笔记本的路径

Get the path of the notebook on Google Colab

我正在尝试使用 hyperas (hyperparameter optimization for keras) in a Google Colab notebook,我已经成功安装了 hyperas:

!pip install hyperas

但是 minimize 函数参数 notebook_name 有一个问题,当您使用笔记本

这个参数必须填写笔记本的路径,但在 Colab 中我不知道如何获取它

您可以从 Google 驱动器复制 notebook.ipybn。然后hyperas可以从中提取信息。

# Install the PyDrive wrapper & import libraries.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Copy/download the file
fid = drive.ListFile({'q':"title='notebook.ipynb'"}).GetList()[0]['id']
f = drive.CreateFile({'id': fid})
f.GetContentFile('notebook.ipynb')

接受的答案对我不起作用。这是我的解决方案。 colab分配的目录是/content/。您需要从 google 驱动器下载当前笔记本并将其上传到 /content/。