一、前言
二、Google Colab特征
三、開始使用
3.1在谷歌雲盤上創建文件夾
3.2創建Colaboratory
3.3創建完成
四、設置GPU運行
五、運行.py文件
5.1安裝必要庫
5.2 掛載雲端硬盤
5.3 安裝Keras
5.4 Hello Mnist!
一、前言
不知道大家是否為了尋找免費GPU服務器而焦頭爛額。
近些天,谷歌推出了Google Colab(Colaboratory)
官方對其的說明是:
Colaboratory 是一個研究項目,可免費使用。
划重點,最重要的特點是 免費GPU!免費GPU!免費GPU!
雖然不確定這個項目是不是永久的
但這無疑給糾結在是否花大量錢租用GPU服務器進行研究的個人研究者帶去了重磅福利!
經過查閱資料與親自實踐,特把相關教程寫成博文分享給大家。
由於博主水平能力有限,難免有錯誤,歡迎指正哈!
2018.3.22更新
emmm,大概是用的人多了…
在colab上跑一個DCGAN竟然比自己筆記本上用CPU跑的還要慢5倍…
天下沒有免費的午餐…
二、Google Colab特征
Colaboratory 是一個 Google 研究項目,旨在幫助傳播機器學習培訓和研究成果。它是一個 Jupyter 筆記本環境,不需要進行任何設置就可以使用,並且完全在雲端運行。
Colaboratory 筆記本存儲在 Google 雲端硬盤中,並且可以共享,就如同您使用 Google 文檔或表格一樣。Colaboratory 可免費使用。
利用Colaboratory ,可以方便的使用Keras,TensorFlow,PyTorch等框架進行深度學習應用的開發。
三、開始使用
注意:使用google服務可能需要梯子
3.1在谷歌雲盤上創建文件夾
當登錄賬號進入谷歌雲盤時,系統會給予15G免費空間大小。由於Colab需要依靠谷歌雲盤,故需要在雲盤上新建一個文件夾。
選擇新建文件夾,文件夾名稱可自定義。
3.2創建Colaboratory
進入創建好的文件夾,點開新建-更多。
如果在更多欄里沒有發現Colaboratory,選擇關聯更多應用,搜索Colaboratory,選擇關聯。
3.3創建完成
創建完成后,會自動生成一個jupyter筆記本,是不是很熟悉~
四、設置GPU運行
選擇 修改-筆記本設置
將硬件加速器設置為GPU即可
五、運行.py文件
5.1安裝必要庫
輸入相應代碼,並執行(crtl+F9)
!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}
1
2
3
4
5
6
7
8
9
10
11
12
運行后,會出現以下提示
先點開相應的鏈接,選擇自己的谷歌賬號,並允許,最后會得到相應的代碼,輸入相應的框中即可
5.2 掛載雲端硬盤
同上,輸入下面命令,執行即可
!mkdir -p drive
!google-drive-ocamlfuse drive -o nonempty
1
2
5.3 安裝Keras
同理,輸入命令
!pip install -q keras
1
5.4 Hello Mnist!
將代碼粘入jupyter筆記本中,運行,即可開始奇妙的Google Colab之旅
代碼摘自:https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
每一個epoch都只用了十多秒!
是不是很有意思呢!
References
https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d
---------------------
作者:cocoaqin
來源:CSDN
原文:https://blog.csdn.net/cocoaqin/article/details/79184540
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!