官方说明
Colaboratory 是一个研究项目,可免费使用。(暂不确定这个项目是不是长久有效)
Colaboratory 支持 Python 2.7 和 Python 3.6。
谷歌中文说明:https://colab.research.google.com/notebook
以下教程基于Google浏览器(桌面版 Chrome)操作,并且使用代理访问Google。
申请Colaboratory
1、进入谷歌云盘( https://drive.google.com/drive/my-drive ),新建一个文件夹(本教程以app命名)
2、创建Colaboratory,进入创建好的文件夹,点开新建 -> 更多 ->关联更多应用 。搜索并找到【Colaboratory】,然后关联
3、创建新的Colab笔记本
通过右键单击>更多> Colaboratory创建一个新的jupyter笔记本
通过点击文件名来重命名。
使用Colaboratory
1、设置GPU运行,选择 修改 -> 设置
2、设置后端Python版本和免费的GPU使用
现在已经可以运行一些ubuntu 命令,每条命令以英文的【!】开始
3、新建代码与运行
①新建代码
②输入代码内容
③点击运行代码
根据自己的需求安装一些软件
4、使用Google Colab运行py文件,读取和存储到Google云盘中
首先运行这些代码,以便安装必要的库并执行授权。
1 2 3 4 5 6 7 8 9 10 11 12 |
!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} |
注:!pip install或者 !apt-get install命令可以安装其他能使用到的库。
运行可以看到如下结果:此时,点击链接地址,获取验证码。
点击链接地址,获取授权码,把获取到的授权码输入之后回车出现如下图所示,再点击这个链接又会获取Google Cloud SDK,操作和之前一样。
出现下面提示说明已经授权成功
Please enter the verification code: Access token retrieved correctly.
补充:如果出现如下提示
Warning: apt-key output should not be parsed (stdout is not a terminal)
这说明认证已经完成,直接执行一步就可以了。
5、挂载Google Drive
1 2 |
!mkdir -p drive !google-drive-ocamlfuse drive |
挂载完成后就可以使用Google云盘中的文件了,(因为这个jupyter笔记本设置的是python3所以下面事例用python3)
1 |
!python3 drive/app/xxx.py |
6、更改工作目录
查看工作目录
你能看到datalab和驱动器文件夹。因此,您必须在定义每个文件名之前添加drive/app。为了避免这个麻烦,我们可以更改工作目录
更改工作目录为app(你自己使用哪个文件,改下面命令中的名字就可以,禁止使用中文。)
1 2 |
import os os.chdir("drive/app") |
相关命令
1、查看是否使用GPU
1 2 |
import tensorflow as tf tf.test.gpu_device_name() |
2、在使用哪个GPU
1 2 |
from tensorflow.python.client import device_lib device_lib.list_local_devices() |
3、RAM信息
1 |
!cat /proc/meminfo |
4、CPU信息
1 |
!cat /proc/cpuinfo |
91福利python3脚本
把代码保存为.py文件格式
1、推荐使用(服务器&Colaboratory都可以使用)