GPU
-
现在针对深度学习(或者计算机视觉)这种领域,需要的计算机的配置可以讲是变态的〜深度学习需要非常好的服务器或电脑配置,只有这样才能保证程序尽快的出结果。那么针对于深度学习需要的GPU是什么,其实简单的说就是显卡,为的就是快速的运行,当然,游戏那也是相当流畅啊哈哈~~~
-
最近用电脑跑一个深度学习的代码,结果电脑慢的有点过分啊,,,,,,
第一个Epoch直接运行了8个多小时【绝望】
- 根据GPU,像在学校只要连接到校园网,我的学校是5台128G的GPU服务器随便你用 ~~
但是在家里面,要用上GPU就得花钱,但是在这上面,Google竟然推出了免费的GPU服务,简直不要太爽
Google GPU
-
谷歌向所有拥有Gmail账号的人都提供了一个免费GPU,这个GPU你可以理解成一个在一个Ubuntu系统环境里面安装了一个带有Python3或者Python2的Jupyter笔记本软件,使用过Anaconda的人知道Jupyter notebook是特别好用的Python运行软件,完全是直接按照Python的语法来进行运行,还可以仿照Linux的命令,执行自己的程序。
-
说到如何使用谷歌的GPU,首先我们需要登录我们的Google云盘,当然,你需要先注册一个Google账号,然后在登录这个云盘,界面是这样的:
-
这个我们可以先建立一个文件夹,点击左上方的新建,
-
这里我把这个取名一个“ demo”接下来我们双加进入这个demo文件夹,再点击左上角的新建,选择“更多”下的colaboratory。
-
这个我们就进入编辑界面了〜
-
调整使用GPU运行。在这里我们就是设置程序是GPU运行了。点击“ 代码执行程序 ”,选择“ 更改运行时类型 ”,这里设置成GPU,Python版本我默认是Python3,
也可以根据自己的需要修改为Python2。
- 挂载、授权。接下来进行挂载,当完成基本的文件建立和配置后,就需要先运行以下这些代码,来安装必要的库,执行授权。
!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}
- 点击下面这个链接,根据提示获得密码,黏贴到这个栏后,回车一下就可以了。
- 接下来我们让它连接我的云盘,输入下面的代码,根据提示,这样就能直接运行云盘中的程序。
from google.colab import drive
drive.mount('/content/drive')
测试一下
- 接下来输入一个深度学习的代码来测试一下,这是深度学习中典型的一个mnist手写体识别的代码:
# -*- coding: utf-8 -*-
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])
- 接下我们开看看测试运行的结果~
每次循环只要4秒中真香~~~
注意事项
- 这个网盘最想执行一下上面的挂载,跟自己的网盘连接~
- GPU使用目前规定的是不能持续使用12小时,,不过这种问题影响不大~~~