TensorFlow使用GPU测试
tensorflow
一个面向所有人的开源机器学习框架
项目地址:https://gitcode.com/gh_mirrors/te/tensorflow
免费下载资源
·
查看当前TensorFlow版本
import tensorflow as tf
print(tf.__version__)
查看当前主机上运行的设备
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus)
print(cpus)
查看GPU是否可用,指定在CPU/GPU上运行
import tensorflow as tf
# 指定在cpu上运行
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([10000, 1000])
cpu_b = tf.random.normal([1000, 2000])
cpu_c = tf.matmul(cpu_a, cpu_b)
print("cpu_a:", cpu_a.device)
print("cpu_b:", cpu_b.device)
print("cpu_c:", cpu_c.device)
# 查看gpu是否可用
print(tf.config.list_physical_devices('GPU'))
# 指定在gpu上运行
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([10000, 1000])
gpu_b = tf.random.normal([1000, 2000])
gpu_c = tf.matmul(gpu_a, gpu_b)
print("gpu_a:", gpu_a.device)
print("gpu_b:", gpu_b.device)
print("gpu_c:", gpu_c.device)
比较在CPU和GPU上的运行时间
import tensorflow as tf
import timeit
def cpu_run():
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([10000, 1000])
cpu_b = tf.random.normal([1000, 2000])
c = tf.matmul(cpu_a, cpu_b)
return c
def gpu_run():
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([10000, 1000])
gpu_b = tf.random.normal([1000, 2000])
c = tf.matmul(gpu_a, gpu_b)
return c
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print("cpu:", cpu_time, " gpu:", gpu_time)
GitHub 加速计划 / te / tensorflow
184.55 K
74.12 K
下载
一个面向所有人的开源机器学习框架
最近提交(Master分支:2 个月前 )
a49e66f2
PiperOrigin-RevId: 663726708
3 个月前
91dac11a
This test overrides disabled_backends, dropping the default
value in the process.
PiperOrigin-RevId: 663711155
3 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)