Google colab 挂载gdrive(详细Colab工作流代码)

2021.12.20更新

function ClickConnect(){
    console.log("Clicked on connect button"); 
    document.querySelector("paper-button").click()
}
setInterval(ClickConnect,60000)

停止脚本:
delete ClickConnect(id_of_ClickConnect)
# id_of_ClickConnect是执行 setInterval() 时返回的 ID 值

==========================================================
挂载云盘

import os
print(os.getcwd())

from google.colab import drive
drive.mount('/content/gdrive')

修改工作路径

path = "/content/gdrive/MyDrive/nlp"
os.chdir(path)
print(os.getcwd())

Colab 上的工作流:

来自
1、将本地数据集压缩成单个压缩文件,上传到 Google Drive 中。
2、打开一个 Notebook,mount Google Drive。

from google.colab import drive
drive.mount('/content/drive')
  1. 在 Notebook 中添加一个 Section 命名为「安装环境」。然后在这个 Section 中做一些数据复制、解压,以及安装第三方包的工作。实例代码如下:
!mkdir ./data
!cp /content/drive/MyDrive/xxx.zip ./data/
!cd ./data && unzip xxx.zip

因为直接从 Drive 中读取数据是通过网络传输的,并不是从本地文件磁盘读取。如果数据文件多,那么就会发很多次网络请求,导致加载数据非常慢,进而严重影响训练速度;这也是很多小伙伴抱怨 Colab 慢的原因之一。所以我们提前压缩好数据集文件,每次训练前复制压缩文件到实例的磁盘,再进行解压,最大程度保证网络传输次数少,传输数据量小。

# 安装需要的库
!pip install -r requirements.txt
  1. 环境准备好后,写好训练代码就可以开始训练了。
%cd /content/drive/MyDrive/
!python train.py
posted @ 2021-11-22 16:13  zzflybird  阅读(977)  评论(0)    收藏  举报