Main Defining Characteristics
Shape: Size of each dimension
Datatype : Type of the elements in tensor
IMPORTANT: Dont create new tensors inside a large loop!. This will create graphs and slows down everything!
import tensorflow as tf
tf.reset_default_graph()
# Constant 1-D Tensor populated with value list.
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7])
print(tensor)
tensor_2 = tf.constant(1,shape=(5,5),dtype=tf.float32)
print(tensor_2)
Building the graph
a = tf.constant(1,name="A")
b = tf.constant(2,name="B")
c = a+b #This tensor is an operation
print(a)
print(b)
print(c)
print(c.op)
Important attributes
print("shape", tensor_2.shape)
print("data type", tensor_2.dtype)
print("name", tensor_2.name)
print("op", tensor_2.op)
print("graph", tensor_2.graph)
tf.reset_default_graph()
string_tensor = tf.constant(['a','b','c'])
print(string_tensor.shape)
print(string_tensor.dtype)
import numpy as np
tf.reset_default_graph()
random_image = np.random.randint(0,256,(128,128,3))
image_tensor = tf.constant(random_image)
print(image_tensor)
#Appears to do the same job
#But useful when you have pandas dataframe
imgage_tensor_2 = tf.convert_to_tensor(random_image)
print(imgage_tensor_2)
Operation that is carried out on every element in the tensor
tf.reset_default_graph()
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7],name="reduce_this_tensor")
sum_t = tf.reduce_sum(tensor)
print(sum_t)
sess = tf.Session()
print("Reduce sum " , sess.run(sum_t))
print("Reduce product " , sess.run(tf.reduce_prod(tensor)))
!rm tensorboard_logs/*
writer = tf.summary.FileWriter('./tensorboard_logs',sess.graph)
writer.close()
sess.close()
!tensorboard --logdir=tensorboard_logs/