如何使用Google Colab免費算力訓練自己的模型

  1. 登錄GoogleDriverhttps://drive.google.com/drive/my-drive
  2. 安裝Colaboratory插件,並新建Colaborator,如下圖
    右鍵我的雲端硬盤—>選擇更多—>選擇關聯更多應用—>搜索Colaborator插件並安裝。
    Google Colaborator插件安裝
    新建Colaborator
    在這裏插入圖片描述
    在這裏插入圖片描述
  3. 連接Google drive
    首先在單元格中輸入並執行以下命令
!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}

會出現2次校驗,詳細信息

E: Package 'python-software-properties' has no installation candidate
Selecting previously unselected package google-drive-ocamlfuse.
(Reading database ... 144328 files and directories currently installed.)
Preparing to unpack .../google-drive-ocamlfuse_0.7.22-0ubuntu1~ubuntu18.04.1_amd64.deb ...
Unpacking google-drive-ocamlfuse (0.7.22-0ubuntu1~ubuntu18.04.1) ...
Setting up google-drive-ocamlfuse (0.7.22-0ubuntu1~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 drive

輸入ls命令,若drive文件夾已列出則說明連接成功
在這裏插入圖片描述
4. 查看GPU、CPU情況
查看GPU是否在colab中

import tensorflow as tf
tf.test.gpu_device_name()

如果結果爲空,則不能使用GPU,如果結果爲/device:GPU:0
使用!/opt/bin/nvidia-smi查看顯存情況
在這裏插入圖片描述
查看顯卡內存使用上限

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

在這裏插入圖片描述
5. clolab筆記本的使用

  • install
!pip install -q keras
import keras
  • uninstall
!pip uninstall -y keras 
# 注意這兒用-y是因爲,google筆記本只會顯示結果不能輸入,因此避免安裝過程中需要你輸入yes什麼的
  • 改變目錄
# 注意不能用!cd ../
!pwd
import os
os.chdir('../')
!pwd

在這裏插入圖片描述

  • git clone
    !git clone https://github.com/wxs/keras-mnist-tutorial.git
  • 下載數據
    !wget https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/Titanic.csv -P drive/app
  • 查看內存信息
    !cat /proc/meminfo
  • 查看CPU信息
    !cat /proc/cpuinfo
  1. 訓練自己的模型
    – 未完待續
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章