📜  Google Colab-使用免费GPU

📅  最后修改于: 2020-10-16 02:37:55             🧑  作者: Mango


Google为您的Colab笔记本提供了免费的GPU。

启用GPU

要在笔记本中启用GPU,请选择以下菜单选项-

Runtime / Change runtime type

您将看到以下屏幕作为输出-

启用GPU

选择GPU ,您的笔记本将在处理过程中使用云中提供的免费GPU。要获得GPU处理的感觉,请尝试运行您先前克隆的MNIST教程中的示例应用程序。

!python3 "/content/drive/My Drive/app/mnist_cnn.py"

尝试在未启用GPU的情况下运行相同的Python文件。您注意到执行速度的差异了吗?

测试GPU

您可以通过执行以下代码轻松检查是否已启用GPU-

import tensorflow as tf
tf.test.gpu_device_name()

如果启用了GPU,它将给出以下输出-

'/device:GPU:0'

列出设备

如果您想知道在云中执行笔记本期间使用的设备,请尝试以下代码-

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

您将看到如下输出:

[name: "/device:CPU:0"
   device_type: "CPU"
   memory_limit: 268435456
   locality { }
   incarnation: 1734904979049303143, name: "/device:XLA_CPU:0"
   device_type: "XLA_CPU" memory_limit: 17179869184
   locality { } 
   incarnation: 16069148927281628039
   physical_device_desc: "device: XLA_CPU device", name: "/device:XLA_GPU:0"
   device_type: "XLA_GPU"
   memory_limit: 17179869184
   locality { }
   incarnation: 16623465188569787091
   physical_device_desc: "device: XLA_GPU device", name: "/device:GPU:0"
   device_type: "GPU"
   memory_limit: 14062547764
   locality {
      bus_id: 1
      links { } 
   }
   incarnation: 6674128802944374158
physical_device_desc: "device: 0, name: Tesla T4, pci bus id: 0000:00:04.0, compute capability: 7.5"]

检查RAM

要查看可用于您的进程的内存资源,请输入以下命令-

!cat /proc/meminfo

您将看到以下输出-

MemTotal: 13335276 kB
MemFree: 7322964 kB
MemAvailable: 10519168 kB
Buffers: 95732 kB
Cached: 2787632 kB
SwapCached: 0 kB
Active: 2433984 kB
Inactive: 3060124 kB
Active(anon): 2101704 kB
Inactive(anon): 22880 kB
Active(file): 332280 kB
Inactive(file): 3037244 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 412 kB
Writeback: 0 kB
AnonPages: 2610780 kB
Mapped: 838200 kB
Shmem: 23436 kB
Slab: 183240 kB
SReclaimable: 135324 kB
SUnreclaim: 47916
kBKernelStack: 4992 kB
PageTables: 13600 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 6667636 kB
Committed_AS: 4801380 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 303092 kB
DirectMap2M: 5988352 kB
DirectMap1G: 9437184 kB

现在,您已经准备好使用Google Colab在Python开发机器学习模型。