Google免费GPU使用教程(亲测可用)

今天突然看到一篇推文,里面讲解了如何薅资本主义羊毛,即如何免费使用Google免费提供的GPU使用权。

可以免费使用的方式就是通过Google Colab,全名Colaboratory。我们可以用它来提高Python技能,也可以用Keras、TensorFlow、PyTorch、OpenCV等等流行的深度学习库来练习开发深度学习的应用。

现在我们介绍如何免费的使用这个非常非常给力的应用!!!

一  项目建立与配置

(1)在Google Drive上创建文件夹:这项功能的使用主要是通过Google Drive,首先需要在Google Drive里面创建新的文件夹,因为我们所有的操作都是通过Google Drive文件的方式进行的,这里我们创建了一个名为gpu的文件夹,然后打开文件夹;

(2)创建新的Colaboratory:右键更多选择Colaboratory, 如果更多没有的话,可以点击关联更多应用搜索添加即可!

并且这里可以随意修改文件名

(3)设置后端Python版本和免费的GPU使用:然后就可以进行代码编写了~~~

二   授权与挂载

(4)当完成基本的文件建立和配置后,就需要先运行下面这些代码,来安装必要的库、执行授权:

 1 !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
 2 !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
 3 !apt-get update -qq 2>&1 > /dev/null
 4 !apt-get -y install -qq google-drive-ocamlfuse fuse
 5 from google.colab import auth
 6 auth.authenticate_user()
 7 from oauth2client.client import GoogleCredentials
 8 creds = GoogleCredentials.get_application_default()
 9 import getpass
10 !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
11 vcode = getpass.getpass()
12 !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

点击运行可以看到如下结果:此时,点击链接地址,获取验证码。

点击链接地址,获取验证码。

提示成功!

(5)授权完成后,就可以挂载Google Drive了

1 !mkdir -p drive
2 !google-drive-ocamlfuse drive

三   测试阶段

我们使用安装Keras中的测试样例代码进行效果的测试:

 1 # -*- coding: utf-8 -*-
 2
 3
 4 ‘‘‘Trains a simple convnet on the MNIST dataset.
 5 Gets to 99.25% test accuracy after 12 epochs
 6 (there is still a lot of margin for parameter tuning).
 7 16 seconds per epoch on a GRID K520 GPU.
 8 ‘‘‘
 9
10 from __future__ import print_function
11 import keras
12 from keras.datasets import mnist
13 from keras.models import Sequential
14 from keras.layers import Dense, Dropout, Flatten
15 from keras.layers import Conv2D, MaxPooling2D
16 from keras import backend as K
17
18 batch_size = 128
19 num_classes = 10
20 epochs = 12
21
22 # input image dimensions
23 img_rows, img_cols = 28, 28
24
25 # the data, shuffled and split between train and test sets
26 (x_train, y_train), (x_test, y_test) = mnist.load_data()
27
28 if K.image_data_format() == ‘channels_first‘:
29     x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
30     x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
31     input_shape = (1, img_rows, img_cols)
32 else:
33     x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
34     x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
35     input_shape = (img_rows, img_cols, 1)
36
37 x_train = x_train.astype(‘float32‘)
38 x_test = x_test.astype(‘float32‘)
39 x_train /= 255
40 x_test /= 255
41 print(‘x_train shape:‘, x_train.shape)
42 print(x_train.shape[0], ‘train samples‘)
43 print(x_test.shape[0], ‘test samples‘)
44
45 # convert class vectors to binary class matrices
46 y_train = keras.utils.to_categorical(y_train, num_classes)
47 y_test = keras.utils.to_categorical(y_test, num_classes)
48
49 model = Sequential()
50 model.add(Conv2D(32, kernel_size=(3, 3),
51                  activation=‘relu‘,
52                  input_shape=input_shape))
53 model.add(Conv2D(64, (3, 3), activation=‘relu‘))
54 model.add(MaxPooling2D(pool_size=(2, 2)))
55 model.add(Dropout(0.25))
56 model.add(Flatten())
57 model.add(Dense(128, activation=‘relu‘))
58 model.add(Dropout(0.5))
59 model.add(Dense(num_classes, activation=‘softmax‘))
60
61 model.compile(loss=keras.losses.categorical_crossentropy,
62               optimizer=keras.optimizers.Adadelta(),
63               metrics=[‘accuracy‘])
64
65 model.fit(x_train, y_train,
66           batch_size=batch_size,
67           epochs=epochs,
68           verbose=1,
69           validation_data=(x_test, y_test))
70 score = model.evaluate(x_test, y_test, verbose=0)
71 print(‘Test loss:‘, score[0])
72 print(‘Test accuracy:‘, score[1])

这里使用Google GPU的效率每个Epoch大概需要11s左右即可完成

而我们使用实验室的工作站

每个率每个Epoch大概需要130s+完成

四  相关命令

(1)查看是否使用GPU:

1 import tensorflow as tf
2 tf.test.gpu_device_name()

(2)在使用哪个GPU:

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

(3)RAM大小:

1 !cat /proc/meminfo

当然Google的使用需要自备FQ工具!

原文链接:https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d

原文地址:https://www.cnblogs.com/infaraway/p/8372881.html

时间: 2024-08-28 10:29:28

Google免费GPU使用教程(亲测可用)的相关文章

Win7(32/64)VS2010配置编译GDAL环境(图文教程+亲测可用!)

最近的一个VS2010的项目中用到了GDAL,关于GDAL这个库的说明与赞美,这里就不赘述了,下面是在VS2010中配置GDAL的详细过程. 系统说明 Win7(32位/64位),VS2010,GDAL 1.10.1(截止本文发布的最新版),进行下面的操作之前,请确保您已经正确安装了VS2010,步骤如下: 1.下载GDAL 点击进入GDAL的官方下载页面,根据需要选择不同的版本,根据测试之前的GDAL1.9.1 0往后的版本均可以按照本文的方法编译成功.下载完成后解压到一个根目录下,因为这样后

Apache+Php配置(亲测可用)

引言: 在软件项目中,Maven提供了一体化的类库管理系统,非常实用.但是,如果新增的类库jar在网络上无法获取到,如何在本地按照Maven的规则添加进来呢?本文将通过一个小例子展示新增过程. 背景介绍: 一个Maven管理的Java项目,提供一个系统级别的POM.xml,其中定义了整个项目使用的类库. 需求: 需要添加一个自定义的类库到当前项目中.假定当前的类库文件名为:abc.jar.. 如何将类库添加进来? 1.  找到当前Maven的Repository类库位置 一般默认情况下,在win

C#读取Excel设置(亲测可用)

OpenFileDialog openFD = new OpenFileDialog(); openFD.FileName = ""; openFD.Filter = "xls|*.xls|xlsx|*.xlsx"; openFD.Title = "选择更新文件"; openFD.Multiselect = false; if (openFD.ShowDialog() == DialogResult.OK) { this.tbSelectFile

sqlite 删除表中重复数据(亲测可用)

例子:表名  Paper .通过字段PaperID查找重复数据. 1 --查询某表中重复的数据       select * from Paper group by PaperID having count(*)>1; 2--删除重复行数,只剩不重复的记录(rowid为sqlite自带字段)      delete from Paper where Paper.rowid not in (select MAX(Paper.rowid) from Paper group by PaperID);

VCL下最好的皮肤控件AlphaControls 9.05 完美支持XE6 亲测可用

在传统的vcl编程中,因自带的控件效果太过普通,为了更好的用户体验和视觉效果往往会使用第三方的一些皮肤控件,这样的皮肤控件百度上一搜一大把,但往往不了解自己不使用过直接下载下来也是没有用的,因为这要涉及到控件版本 ID工具版本 操作系统 等多方因素配合使用,只有实际测试使用过才可定性. 本文提供的一款控件是经过实际测试过的 测试环境:Windows7 32位 ,Delphi xe6, AlphaControls 9.05 下载链接:http://binstyle.7958.com/down_18

MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions -(亲测可用)

MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions - See more at: http://www.codegood.com/archives/129#sthash.dc3d3aib.dpuf http://www.codegood.com/archives/129 http://www.codegood.com/archives/4 MySQL-python 1.2.3 for Windows an

eclipse设置快捷键sysout+Alt+/后出System.out.println!亲测可用!

以前一直用myeclipse,没有这方面的顾虑,现在换到了eclipse,很多要自己设置了,比如很多快捷键. 最常用的,运行到sysout ,eclipse总是不自动补全,很是恼火!!! 现在终于找到了可用的办法,解决方案如下: 需要重新设置快捷键. 按快捷键ctrl+shirt+L,然后在按一下L.设置快捷键的对话框就出来了,然你将Word Completion移除,在将Content Assist 这个设置为alt+/.就可以了. 希望对你有帮助!!! 本人个人网站:灵异事件 www.lie

Sublime Text3注册码,亲测可用

将以下复制在输入框即可 ,亲测可用 . 不过还是希望大家多多支持正版 . -– BEGIN LICENSE -– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA12C0 A37081C5 D0316412 4584D136 94D7F7D4 95BC8C1C 527DA828 560BB037 D1EDDD8C AE7B379F 50C9D69D B35179EF 2FE898C4

CSS响应式:根据分辨路加载不同CSS的几个方法,亲测可用

有时候你需要把同一个页面在手机和pc同时打开,其中有一个办法就是判断不同分辨路加载不同的css 小编总结了几种分别加载css的方法: 1.比较复杂的使用js判断加载不同css (亲测可用) 但是这种方法只有最开始的时候会判断,如果你拖动浏览器大小是不会发生改变的(当然如果喜欢拖着玩的话) 1 <link rel="stylesheet" type="text/css" id="links" href="../css/m_wuqin