谷歌爸爸的colab让我们有了免费使用gpu的机会,在此感谢磕头哐哐哐!(不过每次只能连续使用12小时的GPU,搜了一下也有人说会随机断开连接,避免你长时间使用,但我至今还未遇到这个问题)
这篇文章主要介绍如何更改工作路径和挂载google drive。
本文参照robinfly所写博客:【colab入坑系列 · 一】Google drive授权及工作路径,如果有侵权问题请联系删除。
Google drive授权
新建一个notebook,直接运行一下代码:
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
!mkdir -p drive
!google-drive-ocamlfuse drive
在执行时会提示E: Package 'python-software-properties' has no installation candidate
,只要等或者按回车(一开始我确实等待就好了……)就会让你登录Google账号,进行授权,把授权码复制粘贴即可。有两次授权的操作。
切换工作路径
在colab环境中,我们挂载Google drive的位置是 /content/drive/ 。colab中的notebook和py文件默认都是以 /content/ 作为工作目录,需要执行一下命令手动切换工作目录,例如:
import os
path = "/content/drive/OpenNMT/...(dirs...)"
os.chdir(path)
os.listdir(path)
配置完后也可以执行!ls
来查看目录下的文件。