Google Colab 免费GPU服务器使用教程

本文详细介绍了如何使用Google Colab进行免费的GPU编程,包括创建和配置Colab笔记本,安装必要库,使用Google Drive存储和读取文件,以及如何更改工作目录和检查GPU、RAM和CPU信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方说明

Colaboratory 是一个研究项目,可免费使用。(暂不确定这个项目是不是长久有效)

Colaboratory 支持 Python 2.7 和 Python 3.6。

谷歌中文说明:https://colab.research.google.com/notebook

以下教程基于Google浏览器(桌面版 Chrome)操作,并且使用代理访问Google。

申请Colaboratory

1、进入谷歌云盘( https://drive.google.com/drive/my-drive ),新建一个文件夹(本教程以app命名)

《Google免费GPU使用教程(Google Colab Colaboratory)》

2、创建Colaboratory,进入创建好的文件夹,点开新建 -> 更多 ->关联更多应用 。搜索并找到【Colaboratory】,然后关联

《Google免费GPU使用教程(Google Colab Colaboratory)》

3、创建新的Colab笔记本

通过右键单击>更多> Colaboratory创建一个新的jupyter笔记本

《Google免费GPU使用教程(Google Colab Colaboratory)》

通过点击文件名来重命名。

《Google免费GPU使用教程(Google Colab Colaboratory)》

使用Colaboratory

1、设置GPU运行,选择 修改 -> 设置

《Google免费GPU使用教程(Google Colab Colaboratory)》

2、设置后端Python版本和免费的GPU使用

《Google免费GPU使用教程(Google Colab Colaboratory)》《Google免费GPU使用教程(Google Colab Colaboratory)》

现在已经可以运行一些ubuntu 命令,每条命令以英文的【!】开始《Google免费GPU使用教程(Google Colab Colaboratory)》

3、新建代码与运行

①新建代码

②输入代码内容

③点击运行代码

《Google免费GPU使用教程(Google Colab Colaboratory)》

根据自己的需求安装一些软件

4、使用Google Colab运行py文件,读取和存储到Google云盘中

首先运行这些代码,以便安装必要的库并执行授权。

 

1

2

3

4

5

6

7

8

9

10

11

12

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

注:!pip install或者 !apt-get install命令可以安装其他能使用到的库。

《Google免费GPU使用教程(Google Colab Colaboratory)》

运行可以看到如下结果:此时,点击链接地址,获取验证码。《Google免费GPU使用教程(Google Colab Colaboratory)》

点击链接地址,获取授权码,把获取到的授权码输入之后回车出现如下图所示,再点击这个链接又会获取Google Cloud SDK,操作和之前一样。

出现下面提示说明已经授权成功

Please enter the verification code: Access token retrieved correctly.

《Google免费GPU使用教程(Google Colab Colaboratory)》

补充:如果出现如下提示

Warning: apt-key output should not be parsed (stdout is not a terminal)

这说明认证已经完成,直接执行一步就可以了。

5、挂载Google Drive

 

1

2

!mkdir -p drive

!google-drive-ocamlfuse drive

《Google免费GPU使用教程(Google Colab Colaboratory)》

挂载完成后就可以使用Google云盘中的文件了,(因为这个jupyter笔记本设置的是python3所以下面事例用python3)

 

1

!python3 drive/app/xxx.py

6、更改工作目录

查看工作目录《Google免费GPU使用教程(Google Colab Colaboratory)》

你能看到datalab和驱动器文件夹。因此,您必须在定义每个文件名之前添加drive/app。为了避免这个麻烦,我们可以更改工作目录

更改工作目录为app(你自己使用哪个文件,改下面命令中的名字就可以,禁止使用中文。)

 

1

2

import os

os.chdir("drive/app")

 

相关命令

1、查看是否使用GPU

 

1

2

import tensorflow as tf

tf.test.gpu_device_name()

《Google免费GPU使用教程(Google Colab Colaboratory)》

2、在使用哪个GPU

 

1

2

from tensorflow.python.client import device_lib

device_lib.list_local_devices()

《Google免费GPU使用教程(Google Colab Colaboratory)》

3、RAM信息

 

1

!cat /proc/meminfo

《Google免费GPU使用教程(Google Colab Colaboratory)》

4、CPU信息

 

1

!cat /proc/cpuinfo

《Google免费GPU使用教程(Google Colab Colaboratory)》

91福利python3脚本

把代码保存为.py文件格式

1、推荐使用(服务器&Colaboratory都可以使用)

《Google免费GPU使用教程(Google Colab Colaboratory)》

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值