Skip to content

Instantly share code, notes, and snippets.

@sbdzdz
Created January 15, 2018 12:26
Show Gist options
  • Select an option

  • Save sbdzdz/9ca1a5ff848c105d57c1dcd96d80120b to your computer and use it in GitHub Desktop.

Select an option

Save sbdzdz/9ca1a5ff848c105d57c1dcd96d80120b to your computer and use it in GitHub Desktop.
Configuring and testing device placement for TensorFlow
import tensorflow as tf
num_cores = 4
GPU = True
CPU = False
if GPU:
num_GPU = 1
num_CPU = 1
if CPU:
num_CPU = 1
num_GPU = 0
config = tf.ConfigProto(intra_op_parallelism_threads=num_cores,
inter_op_parallelism_threads=num_cores,
allow_soft_placement=True,
log_device_placement=True,
device_count = {'CPU' : num_CPU, 'GPU' : num_GPU})
session = tf.Session(config=config)
# Create a graph
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
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)
# Run the operation
print(session.run(c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment