170819 Anaconda兩行命令安裝tensorflow-gpu+keras-gpu及Gpu vs Cpu驗證
阿新 • • 發佈:2019-02-11
Step1 新增清華映象,加快下載速度, 建立tensorflow-gpu環境
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
conda create -n tensorflow-gpu python=3.6
source activate tensorflow-gpu #(linux下+source, windows下無需+source)
Step2 安裝tensorflow-gpu
conda install tensorflow-gpu
Step3 安裝keras-gpu
conda install keras-gpu
注意:一定要加上-gpu,否則系統會預設成cpu
Step4 驗證是gpu還是cpu
- 預設gpu
import tensorflow as tf
# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3 , 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
- 手動設定gpu與cpu
# Creates a graph.
import tensorflow as tf
with tf.device('/cpu:0'):
a = tf.constant([1.0 , 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
若成功執行Gpu則在終端會有相應的gpu提示提示,例如:/gpu: 0 如下圖: