准备阶段:

  • Anaconda 3(官网下载)
  • python3.x(Anaconda3自带)
  • GPU(最低NVDIA GTX 650)
  • tensorflow-gpu(Anaconda中安装)
  • tensorflow基本语法操作

代码测试

写一个手动调用gpu设备的代码

# coding:utf-8
'''
**************************************************
@File   :深度学习实战[安东尼奥] -> Cap1-hello world
@IDE    :PyCharm
@Author :Small_wind
@Date   :2019/11/17 10:46
**************************************************
'''
import tensorflow as tf

##1.先写个hello world测试下
message=tf.constant("hello world")#常量字符串

sess=tf.Session()
print(sess.run(message))
sess.close()

##2.常量、变量
#略过

##3.调用GPU/CPU设备
c=[]
with tf.device('/gpu:0'):
    rand_t=tf.random_uniform([50,50],0,10,dtype=tf.float32,seed=0)
    a=tf.Variable(rand_t)
    b=tf.Variable(rand_t)
    c.append(tf.matmul(a,b))
    init=tf.global_variables_initializer()

#要验证Tensorflow是否确实再使用指定设备,可设置log_device_placement=True
sess=tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(init)
print(sess.run(c))
sess.close()

结果

在这里插入图片描述

当然,在Anaconda安装了tensorflow-gpu版本后,之后运行程序时,会自动优先调用gpu的(如果有gpu)

GitHub 加速计划 / te / tensorflow
33
4
下载
一个面向所有人的开源机器学习框架
最近提交(Master分支:23 天前 )
4f64a3d5 Instead, check for this case in `ResolveUsers` and `ResolveOperand`, by querying whether the `fused_expression_root` is part of the `HloFusionAdaptor`. This prevents us from stepping into nested fusions. PiperOrigin-RevId: 724311958 3 个月前
aa7e952e Fix a bug in handling negative strides, and add a test case that exposes it. We can have negative strides that are not just -1, e.g. with a combining reshape. PiperOrigin-RevId: 724293790 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐