24mcs1025 Ex1 Part A
24mcs1025 Ex1 Part A
**
MCSE603P: Deep Learning Lab
EXERCISE 1: Introduction to TensorFlow
Submitted By:
Keerthana R (24MCS1025)
M.Tech CSE
SCOPE/VIT Chennai
Submitted To:
Dr.Rajalakshmi R
Associate Professor
SCOPE/VIT Chennai
Creating a vector
[4]: vector = tf.constant([23,24,25,26])
vector
2.Creating a matrix
1
[5]: matrix = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
matrix
3.Creating a tensor
[6]: tensor =tf.constant([[[67,68],[69,70]],[[71,72],[73,74]]])
tensor
[[71, 72],
[73, 74]]], dtype=int32)>
2 2. Find the shape, rank and size of the tensors (which you
created for Q1).
1.Printing Shape of all tensors
[7]: print("Shape of scalar",scalar.shape)
print("Shape of vector",vector.shape)
print("Shape of matrix",matrix.shape)
print("Shape of tensor",tensor.shape)
Shape of scalar ()
Shape of vector (4,)
Shape of matrix (3, 3)
Shape of tensor (2, 2, 2)
2.Printing Rank of all tensors
[8]: print("Rank of scalar",tf.rank(scalar))
print("Rank of vector",tf.rank(vector))
print("Rank of matrix",tf.rank(matrix))
print("Rank of tensor",tf.rank(tensor))
2
[9]: print("Size of scalar",tf.size(scalar))
print("Size of vector",tf.size(vector))
print("Size of matrix",tf.size(matrix))
print("Size of tensor",tf.size(tensor))
tf.Tensor(
[[0.8091574 0.594347 0.67483544 … 0.95423675 0.73473 0.8098073 ]
[0.60397184 0.5028323 0.07199478 … 0.97781396 0.4862994 0.7226087 ]
[0.25994086 0.23808265 0.33961916 … 0.29676974 0.28038502 0.36464262]
[0.6383574 0.68120027 0.24551308 … 0.7787988 0.8700501 0.25336266]
[0.24806392 0.05760801 0.12655997 … 0.8647369 0.03902519 0.2348597 ]],
shape=(5, 300), dtype=float32)
tf.Tensor(
[[0.29074728 0.01891863 0.8829454 … 0.7341484 0.9688134 0.12316489]
[0.42282426 0.909804 0.15008497 … 0.01267278 0.12384367 0.7326139 ]
[0.35857046 0.21476388 0.9160445 … 0.0170027 0.17271769 0.40384483]
[0.18319094 0.63475573 0.5678283 … 0.30026555 0.7924441 0.6353425 ]
[0.80496764 0.6398188 0.42550123 … 0.32466578 0.7163081 0.9910978 ]],
shape=(5, 300), dtype=float32)
tf.Tensor(
[[82.89256 72.2052 81.85942 73.42679 76.32473 ]
[82.59998 74.5827 82.203445 74.39873 75.46951 ]
[68.58944 63.571373 68.09952 63.0169 62.47265 ]
[81.72216 69.42413 76.112305 72.12494 73.24727 ]
[80.05264 70.77235 76.11891 72.066605 69.63198 ]], shape=(5, 5),
dtype=float32)
3
5 5.Multiply the two tensors (which you created for Question 3)
using dot product.
[14]: tf.tensordot(t1,tf.transpose(t2),axes=1)
…,
4
[[0.63778865, 0.6490238 , 0.03876233],
[0.59320354, 0.34076023, 0.09987032],
[0.15262663, 0.20540273, 0.9321383 ],
…,
[0.31028306, 0.09064078, 0.80564225],
[0.13211608, 0.5236708 , 0.8006624 ],
[0.79365826, 0.16971159, 0.82635 ]],
7 7. Find the min and max values of the tensor (created in Q6).
1.Minimum Value
[18]: min =tf.reduce_min(t3)
min
2.Maximum Value
[21]: max=tf.reduce_max(t3)
max
# 8.Created a tensor with random values of shape [1, 224, 224, 3] then squeeze it to
change the shape to [224, 224, 3]
1.Created tensor of shape [1,224,224,3]
5
t4, t4.shape
…,
6
[0.80708146, 0.5467967 , 0.33809483],
[0.9515544 , 0.5343975 , 0.33860803],
…,
[0.4060731 , 0.7671851 , 0.72042096],
[0.95961964, 0.8141395 , 0.11800551],
[0.3762436 , 0.51210093, 0.0970763 ]]]], dtype=float32)>,
TensorShape([1, 224, 224, 3]))
Squeezing
[24]: t4.squeeze = tf.squeeze(t4)
t4.squeeze, t4.squeeze.shape
…,
7
[[0.8945197 , 0.8394604 , 0.55770063],
[0.30466652, 0.3937807 , 0.99953973],
[0.94918406, 0.8456913 , 0.06104887],
…,
[0.8713335 , 0.06324041, 0.2139194 ],
[0.57146466, 0.5657935 , 0.7970512 ],
[0.70135677, 0.42073858, 0.82387316]],
# 9. Create a tensor with shape [10] using your own choice of values, then find # the
index which has the maximum value
[34]: t5 = tf.random.uniform(shape=[10], minval=0, maxval=5, dtype=tf.int32)
t5
#tf.argmax(t5)
[35]: tf.argmax(t5)
8
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>
[ ]: