Colab使用

2020-03-05  本文已影响0人  孑立的老章鱼
指定TensorFlow版本

colab默认的TensorFlow为1.x版本,要使用2.x不用重新安装,只需在导入tensorflow之前输入%tensorflow_version 2.x
查看是否使用了GPU可以使用tf.test.gpu_device_name()

%tensorflow_version 2.x
import tensorflow as tf
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
TensorFlow 2.x selected.
Found GPU at: /device:GPU:0

谷歌官方也说了:尽量不要使用pip install来指定tensorflow版本,colab内置的tensorflow是对谷歌服务器专门优化过的,比pip安装的版本表现更好。


colab.png
指定CPU或GPU计算
%tensorflow_version 2.x
import tensorflow as tf
import timeit

device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  print(
      '\n\nThis error most likely means that this notebook is not '
      'configured to use a GPU.  Change this in Notebook Settings via the '
      'command palette (cmd/ctrl-shift-P) or the Edit menu.\n\n')
  raise SystemError('GPU device not found')

def cpu():
  with tf.device('/cpu:0'):
    random_image_cpu = tf.random.normal((100, 100, 100, 3))
    net_cpu = tf.keras.layers.Conv2D(32, 7)(random_image_cpu)
    return tf.math.reduce_sum(net_cpu)

def gpu():
  with tf.device('/device:GPU:0'):
    random_image_gpu = tf.random.normal((100, 100, 100, 3))
    net_gpu = tf.keras.layers.Conv2D(32, 7)(random_image_gpu)
    return tf.math.reduce_sum(net_gpu)
  
# We run each op once to warm up; see: https://stackoverflow.com/a/45067900
cpu()
gpu()

# Run the op several times.
print('Time (s) to convolve 32x7x7x3 filter over random 100x100x100x3 images '
      '(batch x height x width x channel). Sum of ten runs.')
print('CPU (s):')
cpu_time = timeit.timeit('cpu()', number=10, setup="from __main__ import cpu")
print(cpu_time)
print('GPU (s):')
gpu_time = timeit.timeit('gpu()', number=10, setup="from __main__ import gpu")
print(gpu_time)
print('GPU speedup over CPU: {}x'.format(int(cpu_time/gpu_time)))
Time (s) to convolve 32x7x7x3 filter over random 100x100x100x3 images (batch x height x width x channel). Sum of ten runs.
CPU (s):
3.862475891000031
GPU (s):
0.10837535100017703
GPU speedup over CPU: 35x
文件系统
文件上传

files.upload 会返回已上传文件的字典。 此字典的键为文件名,值为已上传的数据。

from google.colab import files

uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))
dist.txt(text/plain) - 130 bytes, last modified: 2020/3/4 - 100% done
Saving dist.txt to dist.txt
User uploaded file "dist.txt" with length 130 bytes
文件下载

files.download 会通过浏览器将文件下载到本地计算机。

from google.colab import files

with open('example.txt', 'w') as f:
  f.write('some content')

files.download('example.txt')
谷歌云盘挂载
from google.colab import drive
drive.mount('/content/drive')
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=xxxxxxxxxxxxx
Enter your authorization code: 
·········· 
Mounted at /content/drive
上一篇下一篇

猜你喜欢

热点阅读