Tensorflow in tf.ConfigProto () Detailed

参考Tensorflow Machine Leanrning Cookbook

The main role tf.ConfigProto () is arranged tf.Session operation mode, cpu or arithmetic operation such gpu

Specific code as follows:

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))

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

 

Specific explanation

log_device_placement=True

When set to True, it will print out TensorFlow use the kind of operation


inter_op_parallelism_threads=0

A number of threads the thread provided inside the parallel arithmetic operations, such as matrix multiplication, if set to 0, the optimal number of threads handling


intra_op_parallelism_threads=0

A plurality of arithmetic operations in parallel the number of threads, such as c = a + b, d = e + f. Parallel operation


allow_soft_placement=True

Sometimes, different devices, it's cpu and gpu are different, if this option is set to True, the device does not meet the requirements when running, will automatically allocate GPU or CPU.

 


other options

When using the GPU time, Tensorflow automatically run slowly for maximum GPU memory

session_config.gpu_options.allow_growth = True
1


When a GPU, GPU memory maximum set ratio

session_config.gpu_options.per_process_gpu_memory_fraction = 0.4
1


Ability to use the GPU for computing

tf.test.is_built_with_cuda()
1

 


Additional processing method

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])

= tf.multiply Combined (c, flat_d)
Print (sess.run (Combined))
---------------------
Author: debris flows in a clear stream
Source: CSDN
original: https: //blog.csdn.net/qq_31261509/article/details/79746114
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/jfdwd/p/11183446.html