Tensorflow computing acceleration

In tensorflow, you can specify each running device through the tf.device function, which can be GPU or CPU. For example, the name of CPU in tensorflow is /cpu:0, even if there are multiple CPUs in the computer, tensorflow does not Will distinguish them, but the GPU name on each device is different, the name of the Nth GPU is /gpu:n, the name of the first GPU is /gpu:0, and the name of the second GPU is /gpu:1 , and so on.

In addition, tensorflow provides a quick way to view the device running each operation, that is, set the log_device_placement parameter when generating the session to print the device running each operation

import tensorflow as tf

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

c = a+b

sess = tf.Session(config = tf.ConfigProto(log_device_placement = True))

print (sex (c))

In the tensorflow that has been configured in the GPU environment, tensorflow will automatically place the operation on the GPU preferentially

import tensorflow as tf

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:1')

c = a+b

sess = tf.Session(config = tf.ConfigProto(log_device_placement = True))

print (sex (c))

Here, the generation of constants a and b is performed on the cpu, and the addition operation is performed on the second GPU.

Attention:

Not all operations can run on GPU, take tf.Variable() for example, tf.Variable(1,name = '1') is wrong, because on GPU, tf.Variable() operation Only real numbers are supported, such as tf.float16, float32, double, and in the previous example, it is an integer. In order to avoid such a situation, you can add such a statement before generating the session: allow_soft_placement When this parameter is set to True, the statement that cannot be operated by the GPU can be transferred to the CPU to run

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324677343&siteId=291194637