Testing

Some libraries pre-installed in tensorflow docker image

Tensor flow image comes prebuilt with almost all libraries we ll need. Some of the libraries are listed below.

Python - 3.5

In [1]:
import tensorflow as tf
tf.__version__
Out[1]:
'1.12.0'
In [2]:
import numpy as np
np.__version__
Out[2]:
'1.15.4'
In [3]:
import sklearn
sklearn.__version__
Out[3]:
'0.20.0'
In [4]:
import pandas
pandas.__version__
Out[4]:
'0.23.4'
In [5]:
import matplotlib
matplotlib.__version__
Out[5]:
'3.0.1'

Setting up computational graph

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

In [6]:
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")

Executing shell commands

use the shell command prefixed by !

We execute the command to delete all previos tensorboard logs

In [7]:
!rm tensorboard_logs/*

Start session and write the graph to disk

In [8]:
with tf.Session() as sess:
    writer = tf.summary.FileWriter('./tensorboard_logs', sess.graph)
    writer.close()
In [9]:
!ls tensorboard_logs/
events.out.tfevents.1542301977.3ea3f3d6bab6

Using tensorboard

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:6005

To stop tensorboard, hit the stop button in the kernel

In [10]:
!tensorboard --logdir=tensorboard_logs/
TensorBoard 1.12.0 at http://3ea3f3d6bab6:6006 (Press CTRL+C to quit)
^C

Run operations on computational graph

In [11]:
sess = tf.Session()
print(sess.run(add))
print(sess.run(subtract))
print(sess.run(square))
print(sess.run(final_sum))
sess.close()
9.0
-2.5
10100.25
10106.75

List devices

In [12]:
sess = tf.Session()
print(sess.list_devices())
sess.close()
[_DeviceAttributes(/job:localhost/replica:0/task:0/device:CPU:0, CPU, 268435456, 18077401770470186509), _DeviceAttributes(/job:localhost/replica:0/task:0/device:XLA_GPU:0, XLA_GPU, 17179869184, 10727227273985801305), _DeviceAttributes(/job:localhost/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 1956605614279332794), _DeviceAttributes(/job:localhost/replica:0/task:0/device:GPU:0, GPU, 4985192448, 8019372778990901934)]

Check device execution

To Check on which device the tensor is being executed, set the following config and see the console output

In [13]:
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(add))
print(sess.run(cpu_tensor))
sess.close()
9.0
10.2

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