一步步带你在线上使用Tesla K80 GPU!

Colaboratory免费GPU试用指南,现在我们来一起看一下吧。
地址

1. 在Google Drive上创建文件夹
Colab用的数据都存储在Google Drive云端硬盘上,所以,我们需要先指定在Google Drive上要用的文件夹。比如说,可以在Google Drive上新建一个“app”文件夹,或者其他什么名字,也可以选择Colab笔记本默认的文件夹。
在这里插入图片描述
2. 新建Colab笔记本
在刚刚创建的app文件夹里点击右键,选择“More/更多”,然后从菜单里选择“Google Colaboratory”,这样就新建出了一个Colab笔记本。如果没有“Google Colaboratory”则需要点击“关联更多应用”添加该插件。之后方可成功新建。
在这里插入图片描述
点击笔记本的名字,可以重命名。
在这里插入图片描述
3. 设置免费GPU
这一步,要改变笔记本所用的默认硬件。在笔记本里点Edit>Notebook settings(修改>笔记本设置),或者Runtime>Change runtime type(运行时>改变运行时类型),然后在Hardware accelerator(硬件加速器)一栏选择GPU。
在这里插入图片描述
在这里插入图片描述
然后点击保存,Google Colab就可以用了。

4. 用Colab运行基本Python代码
我们来运行一些Python Numpy教程里的基本数据类型代码。
在这里插入图片描述更多代码参考斯坦福大学卷积神经网络与视觉识别课程(CS231n)的Python Numpy教程,地址。运行结果如你所料。

5. 用Colab运行.py文件
先运行下面这些代码,来安装必要的库、执行授权。

!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}

运行的时候应该会看到下图所示的结果:
在这里插入图片描述
看见那个链接之后,点击它,复制验证码并粘贴到文本框里。授权完成后,就可以挂载Google Drive了:

!mkdir -p drive
!google-drive-ocamlfuse drive

安装Keras:

!pip install -q keras

将mnist_cnn.py文件上传到位于Google云端硬盘上的应用文件夹。运行下面的代码,用MNIST数据集训练一个简单的卷积神经网络:

!python3 drive/app/mnist_cnn.py 

从结果中可以看到,每个epoch只需要11秒。

6. 下载泰坦尼克数据集(.csv File),显示前5行
想按照链接下载.csv文件到app文件夹,只需运行:

!wget https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/Titanic.csv -P drive/app

也可以直接将.csv文件上传到app文件夹。然后读取app文件夹中的.csv文件,显示前5行:

import pandas as pd
titanic = pd.read_csv(“drive/app/Titanic.csv”)
titanic.head(5) 

7. Tips

- 如何安装库?
安装Keras:

!pip install -q keras
import keras

安装PyTorch:

!pip install -q http://download.pytorch.org/whl/cu75/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl torchvision
import torch

安装OpenCV:

!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2

安装XGBoost:

!pip install -q xgboost==0.4a30
import xgboost

安装GraphViz:

!apt-get -qq install -y graphviz && pip install -q pydot
import pydot

安装7zip Reader:

!apt-get -qq install -y libarchive-dev && pip install -q -U libarchive
import libarchive

安装其他库:
!pip install或者!apt-get install命令。

- GPU在干活么?
要查看你在Colab里是不是真的在用GPU,可以运行以下代码来交叉检查:

import tensorflow as tf
tf.test.gpu_device_name() 

在这里插入图片描述
如果显示上图的结果,就是在用GPU。

- 我在用哪个GPU?

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

其实现在,Colab只提供Tesla K80,所以你会看到下图这样的结果:
在这里插入图片描述

- RAM有多大?

!cat /proc/meminfo

在这里插入图片描述
- CPU呢?

!cat /proc/cpuinfo 

在这里插入图片描述
8.总结
好好学习,认真薅毛。温馨提示:自备梯子。各位小伙伴快去体验吧!欢迎在评论区留言提问。

猜你喜欢

转载自blog.csdn.net/Zserendipity/article/details/106019653