pip install tensorflow1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install keras2.1.5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorflow-gpu==1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
输入 conda create -n tensorflow python=3.7(也可以3.7 个人喜欢)
2.安装tensorflow-gpu
首先 activate trnsorflow (tensorflow是你创建的环境名称)
然后
conda install tensorflow-gpu
conda install keras
即可
5.gpu版测试代码
方法一
import tensorflow as tf
import timeit
with tf.device(‘/cpu:0’):
cpu_a = tf.random.normal([10000, 1000])
cpu_b = tf.random.normal([1000, 2000])
print(cpu_a.device, cpu_b.device)
with tf.device(‘/gpu:0’):
gpu_a = tf.random.normal([10000, 1000])
gpu_b = tf.random.normal([1000, 2000])
print(gpu_a.device, gpu_b.device)
def cpu_run():
with tf.device(‘/cpu:0’):
c = tf.matmul(cpu_a, cpu_b)
return c
def gpu_run():
with tf.device(‘/gpu:0’):
c = tf.matmul(gpu_a, gpu_b)
return c
warm up
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print(‘warmup:’, cpu_time, gpu_time)
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print(‘run time:’, cpu_time, gpu_time)
结果
方法二
import tensorflow as tf
import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’]=‘2’
a = tf.constant(1.)
b = tf.constant(2.)
print(a+b)
print(‘GPU:’, tf.test.is_gpu_available())