参考Tensorflow Machine Leanrning Cookbook
tf.ConfigProto()主要的作用是配置tf.Session的运算方式,比如gpu运算或者cpu运算
具体代码如下:
import tensorflow as tf
session_config = tf.ConfigProto(
log_device_placement=True,
inter_op_parallelism_threads=0,
intra_op_parallelism_threads=0,
allow_soft_placement=True)
sess = tf.Session(config=session_config)
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='b')
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)
print(sess.run(c))
具体解释
log_device_placement=True
inter_op_parallelism_threads=0
intra_op_parallelism_threads=0
allow_soft_placement=True
其他选项
当使用GPU时候,Tensorflow运行自动慢慢达到最大GPU的内存
session_config.gpu_options.allow_growth = True
当使用GPU时,设置GPU内存使用最大比例
session_config.gpu_options.per_process_gpu_memory_fraction = 0.4
是否能够使用GPU进行运算
tf.test.is_built_with_cuda()
另外的处理方法
import tensorflow as tf
sess = tf.Session()
with tf.device('/cpu:0'):
a = tf.constant([1.0, 3.0, 5.0], shape=[1, 3])
b = tf.constant([2.0, 4.0, 6.0], shape=[3, 1])
with tf.device('/gpu:0'):
c = tf.matmul(a, b)
c = tf.reshape(c, [-1])
with tf.device('/gpu:0'):
d = tf.matmul(b, a)
flat_d = tf.reshape(d, [-1])
combined = tf.multiply(c, flat_d)
print(sess.run(combined))