170819 Anaconda两行命令安装tensorflow-gpu+keras-gpu及Gpu vs Cpu验证

25 篇文章 0 订阅
20 篇文章 0 订阅

参考文献:
清华大学开源软件镜像站
Using GPUs
Linux下Anaconda的安装使用与卸载-注:安装Anaconda最后一步要选yes

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 如下图:
这里写图片描述

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GuokLiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值