tensorflow24《TensorFlow实战Google深度学习框架》笔记-10-01 GPU基本操作 code

来源:互联网 发布:思迅软件试用版 编辑:程序博客网 时间:2024/05/29 03:34
# 《TensorFlow实战Google深度学习框架》10 TensorBoard计算加速# win10 Tensorflow1.0.1 python3.5.3# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1# filename:ts10.01.py # GPU基本操作# coding=utf-8import tensorflow as tfa = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')c = a + b# 通过log_device_placement参数来记录运行每一个运算的设备。sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))print(sess.run(c))# 通过tf.device将运算指定到特定的设备上。with tf.device('/cpu:0'):    a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')    b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')with tf.device('/gpu:0'): # tf.device('/gpu:1'):    c = a + bsess = tf.Session(config=tf.ConfigProto(log_device_placement=True))print(sess.run(c))a_cpu = tf.Variable(0, name="a_cpu")with tf.device('/gpu:0'):    a_gpu = tf.Variable(0, name="a_gpu")    # 通过allow_soft_placement参数自动将无法放在GPU上的操作放回CPU上。sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))sess.run(tf.global_variables_initializer())
0 0