import tensorflow as tf
tf.__version__
import numpy as np
np.__version__
import sklearn
sklearn.__version__
import pandas
pandas.__version__
import matplotlib
matplotlib.__version__
Make sure to reset default graph while using jupyter notebook. Otherwise, running the cell twice will result in two computational graphs.
For tensorflow gpu version, all tensors are placed in the gpu by default. To carry out operations in cpu, one should explicitly specify with tf.device('/cpu:0')
tf.reset_default_graph()
#The names are used for tagging nodes in computational graph
a = tf.constant(6.0,name='constant_a')
b = tf.constant(3.0,name='constant_b')
c = tf.constant(5.5,name='constant_c')
d = tf.constant(100.5,name='constant_d')
add = tf.add(a,b,name="add_ab")
subtract = tf.subtract(b,c,name="subtract_bc")
square = tf.square(d,name='square_d')
final_sum = tf.add_n([add,subtract,square],name="final_sum")
with tf.device('/cpu:0'):
cpu_tensor = tf.constant(10.2,name="constant_cpu_tensor")
use the shell command prefixed by !
We execute the command to delete all previos tensorboard logs
!rm tensorboard_logs/*
with tf.Session() as sess:
writer = tf.summary.FileWriter('./tensorboard_logs', sess.graph)
writer.close()
!ls tensorboard_logs/
Make sure that port 6006 in the container is forwarded to all hosts.
docker run -p 0.0.0.0:6005:6006 ...After starting tensorboard, go to your browser and
localhost:6005To stop tensorboard, hit the stop button in the kernel
!tensorboard --logdir=tensorboard_logs/
sess = tf.Session()
print(sess.run(add))
print(sess.run(subtract))
print(sess.run(square))
print(sess.run(final_sum))
sess.close()
sess = tf.Session()
print(sess.list_devices())
sess.close()
To Check on which device the tensor is being executed, set the following config and see the console output
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(add))
print(sess.run(cpu_tensor))
sess.close()
terminal output :
constant_d: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2018-11-15 16:37:54.501918: I tensorflow/core/common_runtime/placer.cc:927] constant_d: (Const)/job:localhost/replica:0/task:0/device:GPU:0
constant_cpu_tensor: (Const): /job:localhost/replica:0/task:0/device:CPU:0
2018-11-15 16:37:54.501935: I tensorflow/core/common_runtime/placer.cc:927] constant_cpu_tensor: (Const)/job:localhost/replica:0/task:0/device:CPU:0