Google Colaboratory 的使用记录

Colaboratory 是谷歌云盘的一个工具,谷歌官方给出的解释是 “Colaboratory 是一种数据分析工具,可将文字、代码和代码输出内容合并到一个协作文档中”。会免费提供Tesla K80 GPU使用,如果没有系统的硬件,Colaboratory 真的是一个不错的选择。我使用的感觉,Colaboratory 就是一个Jupyter Notebook 和 liunx 系统下终端的功能结合,有Jupyter Notebook的功能,也具有liunx终端的功能,比如说pip一些包都是可以的。

Colaboratory 使用方法

想在国内使用 Colaboratory 前提是必须要翻墙,登陆自己的谷歌账号。
1.开启google drive的硬盘;
2.直接在空白地方右击,新建 Colaboratory,如果没有找到选择关联更多应用,搜一下Colaboratory 就行。新建完成后直接点击打开就行。
在这里插入图片描述
3.新建一个.ipynb 文件后,点击EDIT 、NOTEBOOK SETTINGS 配置笔记本GPU
在这里插入图片描述
Colaboratory的官网也有一些详细的说明,可以看一下(here

绑定云端硬盘

上面简述了一下怎么新建 Colaboratory,新建一个 .ipynb 文件后我们进行以下操作:

# 授权绑定
!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}

输出如下,中间会弹出两次链接和框框,点击链接获取验证码复制到框框里就可以了

E: Package 'python-software-properties' has no installation candidate
Selecting previously unselected package google-drive-ocamlfuse.
(Reading database ... 130812 files and directories currently installed.)
Preparing to unpack .../google-drive-ocamlfuse_0.7.3-0ubuntu3~ubuntu18.04.1_amd64.deb ...
Unpacking google-drive-ocamlfuse (0.7.3-0ubuntu3~ubuntu18.04.1) ...
Setting up google-drive-ocamlfuse (0.7.3-0ubuntu3~ubuntu18.04.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force
··········
Please, open the following URL in a web browser: https://accounts.google.com/o/oauth2/auth?client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&response_type=code&access_type=offline&approval_prompt=force
Please enter the verification code: Access token retrieved correctly.

然后,挂载

# 挂载
!mkdir -p drive
!google-drive-ocamlfuse -o nonempty drive

设置路径,这里的路径是云端的路径,和你笔记本的本地存储没有任何关系。CODE只是我在云盘里面新建的一个文件夹。前面的/content/drive/是默认的硬盘路径

#设置当前路径   /content/drive/Code
import os 
path = '/content/drive/CODE'
os.chdir(path)
os.listdir(path)

确认路径

# 确认当前路径
! pwd

接下来,我们就可运行一些 python 代码和 liunx 指令感受一下。例如查看你的GPU信息。注意:在输入liunx指令时,指令之前会加“ !” ,可以这样理解加!就是liunx下的指令。 在这里插入图片描述
查看torch版本,运行就是点击那个播放视频键那样的按钮。在这里插入图片描述

主要参考:
https://www.jianshu.com/p/e6f1058614c0?from=timeline&isappinstalled=0
https://blog.csdn.net/ssssdbucdbod/article/details/80397808

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值