Colab入门

Colab优点

  1. 免费
  2. 使用方便,无需在本地安装TensorFlow,keras等深度学习框架

配置

install Keras

!pip install -q keras

关联你自己的Google Drive账号,把Google Drive当成你存放数据和代码的仓库

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

点击下图所示链接,赋值授权码,然后就能访问你Google Drive中的文件
Colab入门

!ls "/content/drive/My Drive/"

会返回你Google Drive主目录下的所有文件夹

配置工作目录

假设你在HW2文件夹内,创建一个新的deep_learning_HM.ipynb文件
Colab入门
Colab不会把你的路径设为你创建文件时所在的路径,而是使用默认路径"/content’,当你想加载dataset.csv.npy文件时,会报错
Colab入门
因此你需要主动设置工作路径

import os
os.chdir("/content/drive/My Drive/HW2")

总结

在使用Colab时,每一个ipynb文件都先执行以下代码

from google.colab import drive
drive.mount('/content/drive/')
import os
os.chdir("/content/drive/My Drive/YourWorkPath")