本文翻译自:点击打开链接
简单的操作不多赘述,这里主要针对想在GPU里面使用自己的数据文件(如上传的txt文件)做实验。
使用Google Colab运行或导入.py文件
首先运行这些代码以安装必要的库并执行授权。
!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}
运行以上代码会出现这样的结果:
点击链接,复制验证码并粘贴到文本框中。(有两次这样的操作)
在完成授权过程之后,
安装Google云端硬盘:
!mkdir -p drive
!google-drive-ocamlfuse drive
更改工作目录
想要用自己的txt文件就必须更改工作目录。
通常运行这段代码时:
!ls
会看到datalab 和drive文件夹,因此必须在使用的文件名之前加 drive/app (app是自己创建的文件夹)路径。
为了解决这个问题,可以改变工作目录。(在本教程中,我改到了app文件夹)用这段简单的代码:
import os
os.chdir("drive/app")
此时再运行:
!ls
将会看到app文件夹里的内容(不再需要在文件名前面添加‘drive/app’)。
另一更详细的文章见这里:Google免费GPU使用教程(Google Colab Colaboratory)