8/5/23, 11:46 PM TensorFlow_ComputerVision_01.
ipynb - Colaboratory
TensorFlow from Scratch -- 01
Author: Ramdas Dharavath
Linkedin: [Link]
Source Code GitHub link: [Link]
Show code
Tensors are Multi Dimentional Arrays
An array is an ordered arrangement of numbers
Show code
Show code
[Link] 1/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
8 --> oD
[0,4,6] --> 1D --> (3,)
2D [[1,2,3],
[4,5,6]] --> 2D --> (2,3)
3D --> [[[4,5,6],[7,2,1], [8,9,2]]] --> (1,3,3)
Import tensorflow as tf
import tensorflow as tf
0D Tensor
tensor_zero_D = [Link](4)
print(tensor_zero_D)
[Link](4, shape=(), dtype=int32)
1D Tensor
tensor_one_d = [Link]([3,4,5])
print(tensor_one_d)
[Link]([3 4 5], shape=(3,), dtype=int32)
tensor_one_d = [Link]([3,4,5],dtype=tf.float32)
print(tensor_one_d)
[Link]([3. 4. 5.], shape=(3,), dtype=float32)
tensor_one_d = [Link]([3,4,5],dtype=tf.float16)
print(tensor_one_d)
[Link]([3. 4. 5.], shape=(3,), dtype=float16)
[Link] 2/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
tensor_one_d = [Link]([3,4.4,5,8.1],dtype=tf.int64)
print(tensor_one_d)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-e4c0cd578aa7> in <cell line: 1>()
----> 1 tensor_one_d = [Link]([3,4.4,5,8.1],dtype=tf.int64)
2 print(tensor_one_d)
3 frames
/usr/local/lib/python3.10/dist-packages/tensorflow/python/framework/constant_op.py i
convert_to_eager_tensor(value, ctx, dtype)
101 dtype = dtypes.as_dtype(dtype).as_datatype_enum
102 ctx.ensure_initialized()
--> 103 return [Link](value, ctx.device_name, dtype)
104
105
TypeError: Cannot convert [3, 4.4, 5, 8.1] to EagerTensor of dtype int64
SEARCH STACK OVERFLOW
solving above error using [Link]
tensor_one_d = [Link]([3,4.4,5,8.1],dtype=tf.float32)
casted_tensor_one_d =[Link](tensor_one_d,dtype=tf.int32)
print(tensor_one_d)
print(casted_tensor_one_d)
[Link]([3. 4.4 5. 8.1], shape=(4,), dtype=float32)
[Link]([3 4 5 8], shape=(4,), dtype=int32)
in bool if zero is false else: true
tensor_one_d = [Link]([3,4.4,0,5,8.1],dtype=tf.float32)
casted_tensor_one_d =[Link](tensor_one_d,dtype=[Link])
print(tensor_one_d)
print(casted_tensor_one_d)
[Link]([3. 4.4 0. 5. 8.1], shape=(5,), dtype=float32)
[Link]([ True True False True True], shape=(5,), dtype=bool)
tensor_bool_d = [Link]([True,False,True])
print(tensor_bool_d)
[Link]([ True False True], shape=(3,), dtype=bool)
[Link] 3/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
tensor_string_d = [Link]("Computer Vision")
print(tensor_string_d)
[Link](b'Computer Vision', shape=(), dtype=string)
tensor_string_d = [Link](["Computer Vision"])
print(tensor_string_d)
[Link]([b'Computer Vision'], shape=(1,), dtype=string)
tensor_one_d = [Link]([3,4,5.6])
print(tensor_one_d)
[Link]([3. 4. 5.6], shape=(3,), dtype=float32)
tensor_one_d = [Link]([-3,4,5])
print(tensor_one_d)
[Link]([-3 4 5], shape=(3,), dtype=int32)
numpy array into tensor
import numpy as np
np_array = [Link]([2,3,4,5])
print(np_array)
[2 3 4 5]
converted_tensor = tf.convert_to_tensor(np_array)
print(converted_tensor)
[Link]([2 3 4 5], shape=(4,), dtype=int64)
2D Tensor
tensor_two_d = [Link]([
[1,2,3],
[4,5,6],
[0,7,5]
])
print(tensor_two_d)
# stack of one d tensor form 2d tensor
[Link](
[[1 2 3]
[Link] 4/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
[4 5 6]
[0 7 5]], shape=(3, 3), dtype=int32)
tensor_two_d.dtype
tf.int32
tensor_two_d.shape
TensorShape([3, 3])
3D Tensor
tensor_three_d = [Link]([
[[1,2,3],
[5,4,3]],
[[1,6,9],
[0,4,1]]
])
print(tensor_three_d)
[Link](
[[[1 2 3]
[5 4 3]]
[[1 6 9]
[0 4 1]]], shape=(2, 2, 3), dtype=int32)
tensor_three_d.shape
TensorShape([2, 2, 3])
tensor_three_d.ndim
tensor_three_d.dtype
tf.int32
4D Tensor
tensor_four_d = [Link]([
[[[1,2,3],
[90,50,44]]],
[Link] 5/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
[[[5,6,7],
[90,30,22]]]
])
print(tensor_four_d)
[Link](
[[[[ 1 2 3]
[90 50 44]]]
[[[ 5 6 7]
[90 30 22]]]], shape=(2, 1, 2, 3), dtype=int32)
tensor_four_d.ndim
tensor_four_d.shape
TensorShape([2, 1, 2, 3])
tensor_four_d.dtype
tf.int32
Eye tensor
eye_tensor = [Link](
num_rows=4,
num_columns=None,
batch_shape=None,
dtype=[Link].float32,
name=None
)
print(eye_tensor)
# construct identity matrix
[Link](
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]], shape=(4, 4), dtype=float32)
eye_tensor = [Link](
num_rows=4,
num_columns=None,
batch_shape=None,
dtype=[Link].float16,
[Link] 6/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
name=None
)
print(eye_tensor)
# construct identity matrix
[Link](
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]], shape=(4, 4), dtype=float16)
eye_tensor = [Link](
num_rows=4,
num_columns=None,
batch_shape=None,
dtype=[Link],
name=None
)
print(eye_tensor)
# construct identity matrix
[Link](
[[ True False False False]
[False True False False]
[False False True False]
[False False False True]], shape=(4, 4), dtype=bool)
eye_tensor = [Link](
num_rows=4,
num_columns=3,
batch_shape=None,
dtype=[Link].float32,
name=None
)
print(eye_tensor)
# construct identity matrix
[Link](
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]], shape=(4, 3), dtype=float32)
eye_tensor = [Link](
num_rows=4,
num_columns=None,
batch_shape=[2,],
dtype=[Link].float32,
name=None
)
print(eye_tensor)
# construct identity matrix
# batch size 2 means i need two 4,4 matrix
[Link] 7/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
[Link](
[[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]], shape=(2, 4, 4), dtype=float32)
fill tensor
fill_tensor = [Link](
[3,4],5,name=None
)
print(fill_tensor)
[Link](
[[5 5 5 5]
[5 5 5 5]
[5 5 5 5]], shape=(3, 4), dtype=int32)
ones tensor
ones_tensor = [Link](
[2,3],
dtype=[Link].int32,
name=None
)
print(ones_tensor)
[Link](
[[1 1 1]
[1 1 1]], shape=(2, 3), dtype=int32)
ones_tensor = [Link](
[2,3,2],
dtype=[Link].float32,
name=None
)
print(ones_tensor)
[Link](
[[[1. 1.]
[1. 1.]
[1. 1.]]
[[1. 1.]
[Link] 8/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
[1. 1.]
[1. 1.]]], shape=(2, 3, 2), dtype=float32)
fill2_tensor = [Link](
[3,3],3,name=None
)
print(fill2_tensor)
[Link](
[[3 3 3]
[3 3 3]
[3 3 3]], shape=(3, 3), dtype=int32)
ones _like tensor
ones_like_tensor = tf.ones_like(
fill2_tensor
)
print(ones_like_tensor)
[Link](
[[1 1 1]
[1 1 1]
[1 1 1]], shape=(3, 3), dtype=int32)
zeros tensor
zeros2_tensor = [Link](
[3,2],
dtype = [Link].float32,
name=None
)
print(zeros2_tensor)
[Link](
[[0. 0.]
[0. 0.]
[0. 0.]], shape=(3, 2), dtype=float32)
ones_like2_tensor = tf.ones_like(zeros2_tensor)
print(ones_like2_tensor)
[Link](
[[1. 1.]
[1. 1.]
[1. 1.]], shape=(3, 2), dtype=float32)
print(tensor_three_d)
[Link] 9/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
[Link](
[[[1 2 3]
[5 4 3]]
[[1 6 9]
[0 4 1]]], shape=(2, 2, 3), dtype=int32)
tensor_three_d.shape
TensorShape([2, 2, 3])
print([Link](tensor_three_d))
[Link]([2 2 3], shape=(3,), dtype=int32)
[Link] return rank of tensor
# [Link] return rank of tensor
t = [Link]([[[1,2,3],[6,7,8]]])
print([Link](t))
[Link](t)
[Link](3, shape=(), dtype=int32)
<[Link]: shape=(), dtype=int32, numpy=3>
t1 = [Link]([[1,2,3],[5,6,7]])
print([Link](t1))
[Link](t1)
[Link](2, shape=(), dtype=int32)
<[Link]: shape=(), dtype=int32, numpy=2>
t1 = [Link]([1,2,3])
print([Link](t1))
[Link](t1)
[Link](1, shape=(), dtype=int32)
<[Link]: shape=(), dtype=int32, numpy=1>
# [Link]
[Link] 10/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
t2 = [Link]([[1,2,3,4],[6,7,8,9]])
[Link](t2)
<[Link]: shape=(), dtype=int32, numpy=8>
t2 = [Link]([[1,2,3,4],[6,7,8,9]])
[Link](t2,out_type=tf.float32)
<[Link]: shape=(), dtype=float32, numpy=8.0>
# [Link]
random_tensor = [Link](
[3,3],
mean=0,
stddev=1.0,
dtype=[Link].float32,
seed=None,
name=None
)
print(random_tensor)
[Link](
[[-0.39316094 0.02094128 -0.6328461 ]
[ 2.299625 0.6251846 0.96148187]
[-0.80120754 -0.38471952 0.34140763]], shape=(3, 3), dtype=float32)
# [Link]
# range of values in between minval,maxvalue
random_tensor2 = [Link](
[4,],
minval=0,
maxval=6,
dtype = [Link].int32,
seed = None,
name=None
)
print(random_tensor2)
[Link]([0 5 2 2], shape=(4,), dtype=int32)
random_uniform_tensor = [Link](
[3,4],
minval = 1,
maxval = 4,
dtype = [Link].float64,
seed = None,
name = None
[Link] 11/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
)
print(random_uniform_tensor)
[Link](
[[3.99208412 1.76304071 2.05448341 2.56923315]
[2.55513969 2.18310585 1.75114135 3.04348884]
[2.79654863 1.39844371 2.08378495 3.53174024]], shape=(3, 4), dtype=float64)
random_uniform_tensor = [Link](
[3,3],
minval = 1,
maxval = None,
dtype = [Link].float64,
seed = None,
name = None
)
print(random_uniform_tensor)
[Link](
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(3, 3), dtype=float64)
# seed argument
# tf.
[Link].set_seed(5)
print([Link](shape=[2,],maxval=4, dtype=tf.float32, seed=10))
print([Link](shape=[3,],maxval=3, dtype=tf.int32, seed=10))
print([Link](shape=[4,], maxval=6, dtype=tf.int64, seed=10))
[Link]([0.36659575 3.8268886 ], shape=(2,), dtype=float32)
[Link]([2 0 1], shape=(3,), dtype=int32)
[Link]([5 0 5 4], shape=(4,), dtype=int64)
[Link].set_seed(5)
print([Link](shape=[2,],maxval=4, dtype=tf.float32, seed=10))
print([Link](shape=[3,],maxval=3, dtype=tf.int32, seed=10))
print([Link](shape=[4,], maxval=6, dtype=tf.int64, seed=10))
[Link]([0.36659575 3.8268886 ], shape=(2,), dtype=float32)
[Link]([2 0 1], shape=(3,), dtype=int32)
[Link]([5 0 5 4], shape=(4,), dtype=int64)
[Link] 12/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
random_uniform_tensor = [Link](
[3,],
minval = 1,
maxval = 20,
dtype = [Link].float64,
seed = None,
name = None
)
print(random_uniform_tensor)
[Link]([ 1.21176329 2.28550813 15.31137277], shape=(3,), dtype=float64)
random_uniform_tensor = [Link](
[3,],
minval = 1,
maxval = 20,
dtype = [Link].float64,
seed = 10,
name = None
)
print(random_uniform_tensor)
[Link]([14.93065576 5.51886788 18.68575432], shape=(3,), dtype=float64)
Tensor index
tensor_indexed = [Link]([1,2,3,4,5,6])
print(tensor_indexed)
[Link]([1 2 3 4 5 6], shape=(6,), dtype=int32)
print(tensor_indexed[0:5])
[Link]([1 2 3 4 5], shape=(5,), dtype=int32)
print(tensor_indexed[3:4])
[Link]([4], shape=(1,), dtype=int32)
print(tensor_indexed[1:4+1])
[Link]([2 3 4 5], shape=(4,), dtype=int32)
print(tensor_indexed[Link])
[Link]([1 3], shape=(2,), dtype=int32)
[Link] 13/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
print(tensor_indexed[2:])
[Link]([3 4 5 6], shape=(4,), dtype=int32)
print(tensor_indexed[1:-2])
[Link]([2 3 4], shape=(3,), dtype=int32)
tensor_two_d2 = [Link]([[2,1,3,4],
[8,7,6,5]])
print(tensor_two_d2)
[Link](
[[2 1 3 4]
[8 7 6 5]], shape=(2, 4), dtype=int32)
print(tensor_two_d2[1:,1])
[Link]([7], shape=(1,), dtype=int32)
print(tensor_two_d2[0:,2])
[Link]([3 6], shape=(2,), dtype=int32)
print(tensor_two_d2)
print(tensor_two_d2[...,1])
[Link](
[[2 1 3 4]
[8 7 6 5]], shape=(2, 4), dtype=int32)
[Link]([1 7], shape=(2,), dtype=int32)
print(tensor_three_d)
[Link](
[[[1 2 3]
[5 4 3]]
[[1 6 9]
[0 4 1]]], shape=(2, 2, 3), dtype=int32)
print(tensor_three_d[0, 0, 1])
[Link](2, shape=(), dtype=int32)
print(tensor_three_d[1,0,2])
[Link](9, shape=(), dtype=int32)
print(tensor_three_d[0,:,1])
[Link] 14/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
[Link]([2 4], shape=(2,), dtype=int32)
print(tensor_three_d[:,:,1])
[Link](
[[2 4]
[6 4]], shape=(2, 2), dtype=int32)
print(tensor_three_d[1,1,1])
[Link](4, shape=(), dtype=int32)
print(tensor_three_d)
[Link](
[[[1 2 3]
[5 4 3]]
[[1 6 9]
[0 4 1]]], shape=(2, 2, 3), dtype=int32)
print(tensor_three_d[0:2, : , -1])
[Link](
[[3 3]
[9 1]], shape=(2, 2), dtype=int32)
print(tensor_three_d[..., :, 1])
[Link](
[[2 4]
[6 4]], shape=(2, 2), dtype=int32)
# range
[Link](1,5)
<[Link]: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>
# [Link]
x_Abs = [Link]([-1,3,4,5.5])
[Link](x_Abs)
[Link] 15/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
<[Link]: shape=(4,), dtype=float32, numpy=array([1. , 3. , 4. , 5.5],
dtype=float32)>
check 0s completed at 11:33 PM
[Link] 16/16