Tuesday 11 October 2016

Playing with tensorflow

import tensorflow as tf
import numpy as np

#Declare variable in tensorflow

#constant(value, dtype, shape, name)

strng = tf.constant("hello tensor");
session = tf.Session();
print session.run(strng);


#shape means the shape of output. In this case it will be 3x4 matrix. Where the missing values will be filled
#using last value i.e 4

cont_tensor = tf.constant([1,2,3,4],dtype=tf.int32,shape=[3,4],name="SimpleTensor");
print session.run(cont_tensor);

cont_tensor2=tf.constant(1, dtype=tf.float16, shape=[4,4], name="TensorWithAllValues");
result = session.run(cont_tensor2);
#print tensor's name
print cont_tensor2.name

#Define Place holder
m1PH = tf.placeholder(tf.int32, shape=[3,3], name="Matrix1PlaceHoler")
m2PH = tf.placeholder(tf.int32, shape=[3,3], name="Matrix2PlaceHolder")

#Define operations
#addOperation =  tf.add(m1PH, m2PH, name="Add Operation");
mulOperation = tf.matmul(m1PH, m2PH);

#create a matrix of random values and feed it to both place holders
randomMatrix = np.random.random_integers(1,10, [3,3])

print session.run(mulOperation, feed_dict={m1PH: randomMatrix, m2PH: randomMatrix})

#saving graph for tensorboard
tf.train.SummaryWriter("/home/shahzeb/tensorflow/tensorboardlogs",session.graph)

#Once code is executed, launch tensorboard to view the graph generated.
#execute the following command
#$> tensorboard --logdir=/path/to/log/dir #in my case its /home/shahzeb/...

No comments:

Post a Comment