Professional Documents
Culture Documents
Tensorflow From Scratch - 01
Tensorflow From Scratch - 01
ipynb - Colaboratory
Show code
Show code
Show code
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 1/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
8 --> oD
2D [[1,2,3],
[4,5,6]] --> 2D --> (2,3)
Import tensorflow as tf
import tensorflow as tf
0D Tensor
tensor_zero_D = tf.constant(4)
print(tensor_zero_D)
1D Tensor
tensor_one_d = tf.constant([3,4,5])
print(tensor_one_d)
tensor_one_d = tf.constant([3,4,5],dtype=tf.float32)
print(tensor_one_d)
tensor_one_d = tf.constant([3,4,5],dtype=tf.float16)
print(tensor_one_d)
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 2/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
tensor_one_d = tf.constant([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 = tf.constant([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 ops.EagerTensor(value, ctx.device_name, dtype)
104
105
tensor_one_d = tf.constant([3,4.4,5,8.1],dtype=tf.float32)
casted_tensor_one_d =tf.cast(tensor_one_d,dtype=tf.int32)
print(tensor_one_d)
print(casted_tensor_one_d)
tensor_one_d = tf.constant([3,4.4,0,5,8.1],dtype=tf.float32)
casted_tensor_one_d =tf.cast(tensor_one_d,dtype=tf.bool)
print(tensor_one_d)
print(casted_tensor_one_d)
tensor_bool_d = tf.constant([True,False,True])
print(tensor_bool_d)
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 3/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
tensor_string_d = tf.constant("Computer Vision")
print(tensor_string_d)
tensor_string_d = tf.constant(["Computer Vision"])
print(tensor_string_d)
tensor_one_d = tf.constant([3,4,5.6])
print(tensor_one_d)
tensor_one_d = tf.constant([-3,4,5])
print(tensor_one_d)
import numpy as np
np_array = np.array([2,3,4,5])
print(np_array)
[2 3 4 5]
converted_tensor = tf.convert_to_tensor(np_array)
print(converted_tensor)
2D Tensor
tensor_two_d = tf.constant([
[1,2,3],
[4,5,6],
[0,7,5]
])
print(tensor_two_d)
# stack of one d tensor form 2d tensor
tf.Tensor(
[[1 2 3]
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 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 = tf.constant([
[[1,2,3],
[5,4,3]],
[[1,6,9],
[0,4,1]]
])
print(tensor_three_d)
tf.Tensor(
[[[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 = tf.constant([
[[[1,2,3],
[90,50,44]]],
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 5/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
[[[5,6,7],
[90,30,22]]]
])
print(tensor_four_d)
tf.Tensor(
[[[[ 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 = tf.eye(
num_rows=4,
num_columns=None,
batch_shape=None,
dtype=tf.dtypes.float32,
name=None
)
print(eye_tensor)
# construct identity matrix
tf.Tensor(
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]], shape=(4, 4), dtype=float32)
eye_tensor = tf.eye(
num_rows=4,
num_columns=None,
batch_shape=None,
dtype=tf.dtypes.float16,
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 6/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
name=None
)
print(eye_tensor)
# construct identity matrix
tf.Tensor(
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]], shape=(4, 4), dtype=float16)
eye_tensor = tf.eye(
num_rows=4,
num_columns=None,
batch_shape=None,
dtype=tf.dtypes.bool,
name=None
)
print(eye_tensor)
# construct identity matrix
tf.Tensor(
[[ True False False False]
[False True False False]
[False False True False]
[False False False True]], shape=(4, 4), dtype=bool)
eye_tensor = tf.eye(
num_rows=4,
num_columns=3,
batch_shape=None,
dtype=tf.dtypes.float32,
name=None
)
print(eye_tensor)
# construct identity matrix
tf.Tensor(
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]], shape=(4, 3), dtype=float32)
eye_tensor = tf.eye(
num_rows=4,
num_columns=None,
batch_shape=[2,],
dtype=tf.dtypes.float32,
name=None
)
print(eye_tensor)
# construct identity matrix
# batch size 2 means i need two 4,4 matrix
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 7/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
tf.Tensor(
[[[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 = tf.fill(
[3,4],5,name=None
)
print(fill_tensor)
tf.Tensor(
[[5 5 5 5]
[5 5 5 5]
[5 5 5 5]], shape=(3, 4), dtype=int32)
ones tensor
ones_tensor = tf.ones(
[2,3],
dtype=tf.dtypes.int32,
name=None
)
print(ones_tensor)
tf.Tensor(
[[1 1 1]
[1 1 1]], shape=(2, 3), dtype=int32)
ones_tensor = tf.ones(
[2,3,2],
dtype=tf.dtypes.float32,
name=None
)
print(ones_tensor)
tf.Tensor(
[[[1. 1.]
[1. 1.]
[1. 1.]]
[[1. 1.]
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 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 = tf.fill(
[3,3],3,name=None
)
print(fill2_tensor)
tf.Tensor(
[[3 3 3]
[3 3 3]
[3 3 3]], shape=(3, 3), dtype=int32)
ones_like_tensor = tf.ones_like(
fill2_tensor
)
print(ones_like_tensor)
tf.Tensor(
[[1 1 1]
[1 1 1]
[1 1 1]], shape=(3, 3), dtype=int32)
zeros tensor
zeros2_tensor = tf.zeros(
[3,2],
dtype = tf.dtypes.float32,
name=None
)
print(zeros2_tensor)
tf.Tensor(
[[0. 0.]
[0. 0.]
[0. 0.]], shape=(3, 2), dtype=float32)
ones_like2_tensor = tf.ones_like(zeros2_tensor)
print(ones_like2_tensor)
tf.Tensor(
[[1. 1.]
[1. 1.]
[1. 1.]], shape=(3, 2), dtype=float32)
print(tensor_three_d)
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 9/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
tf.Tensor(
[[[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(tf.shape(tensor_three_d))
# tf.rank return rank of tensor
t = tf.constant([[[1,2,3],[6,7,8]]])
print(tf.rank(t))
tf.rank(t)
t1 = tf.constant([[1,2,3],[5,6,7]])
print(tf.rank(t1))
tf.rank(t1)
t1 = tf.constant([1,2,3])
print(tf.rank(t1))
tf.rank(t1)
# tf.size
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 10/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
t2 = tf.constant([[1,2,3,4],[6,7,8,9]])
tf.size(t2)
t2 = tf.constant([[1,2,3,4],[6,7,8,9]])
tf.size(t2,out_type=tf.float32)
# tf.random.normal
random_tensor = tf.random.normal(
[3,3],
mean=0,
stddev=1.0,
dtype=tf.dtypes.float32,
seed=None,
name=None
)
print(random_tensor)
tf.Tensor(
[[-0.39316094 0.02094128 -0.6328461 ]
[ 2.299625 0.6251846 0.96148187]
[-0.80120754 -0.38471952 0.34140763]], shape=(3, 3), dtype=float32)
# tf.random.uniform
# range of values in between minval,maxvalue
random_tensor2 = tf.random.uniform(
[4,],
minval=0,
maxval=6,
dtype = tf.dtypes.int32,
seed = None,
name=None
)
print(random_tensor2)
random_uniform_tensor = tf.random.uniform(
[3,4],
minval = 1,
maxval = 4,
dtype = tf.dtypes.float64,
seed = None,
name = None
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 11/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
)
print(random_uniform_tensor)
tf.Tensor(
[[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 = tf.random.uniform(
[3,3],
minval = 1,
maxval = None,
dtype = tf.dtypes.float64,
seed = None,
name = None
)
print(random_uniform_tensor)
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(3, 3), dtype=float64)
# seed argument
# tf.
tf.random.set_seed(5)
print(tf.random.uniform(shape=[2,],maxval=4, dtype=tf.float32, seed=10))
print(tf.random.uniform(shape=[3,],maxval=3, dtype=tf.int32, seed=10))
print(tf.random.uniform(shape=[4,], maxval=6, dtype=tf.int64, seed=10))
tf.random.set_seed(5)
print(tf.random.uniform(shape=[2,],maxval=4, dtype=tf.float32, seed=10))
print(tf.random.uniform(shape=[3,],maxval=3, dtype=tf.int32, seed=10))
print(tf.random.uniform(shape=[4,], maxval=6, dtype=tf.int64, seed=10))
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 12/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
random_uniform_tensor = tf.random.uniform(
[3,],
minval = 1,
maxval = 20,
dtype = tf.dtypes.float64,
seed = None,
name = None
)
print(random_uniform_tensor)
random_uniform_tensor = tf.random.uniform(
[3,],
minval = 1,
maxval = 20,
dtype = tf.dtypes.float64,
seed = 10,
name = None
)
print(random_uniform_tensor)
Tensor index
tensor_indexed = tf.constant([1,2,3,4,5,6])
print(tensor_indexed)
print(tensor_indexed[0:5])
print(tensor_indexed[3:4])
print(tensor_indexed[1:4+1])
print(tensor_indexed[0:4:2])
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 13/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
print(tensor_indexed[2:])
print(tensor_indexed[1:-2])
tensor_two_d2 = tf.constant([[2,1,3,4],
[8,7,6,5]])
print(tensor_two_d2)
tf.Tensor(
[[2 1 3 4]
[8 7 6 5]], shape=(2, 4), dtype=int32)
print(tensor_two_d2[1:,1])
print(tensor_two_d2[0:,2])
print(tensor_two_d2)
print(tensor_two_d2[...,1])
tf.Tensor(
[[2 1 3 4]
[8 7 6 5]], shape=(2, 4), dtype=int32)
tf.Tensor([1 7], shape=(2,), dtype=int32)
print(tensor_three_d)
tf.Tensor(
[[[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])
print(tensor_three_d[1,0,2])
print(tensor_three_d[0,:,1])
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 14/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
print(tensor_three_d[:,:,1])
tf.Tensor(
[[2 4]
[6 4]], shape=(2, 2), dtype=int32)
print(tensor_three_d[1,1,1])
print(tensor_three_d)
tf.Tensor(
[[[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])
tf.Tensor(
[[3 3]
[9 1]], shape=(2, 2), dtype=int32)
print(tensor_three_d[..., :, 1])
tf.Tensor(
[[2 4]
[6 4]], shape=(2, 2), dtype=int32)
# range
tf.range(1,5)
# tf.abs
x_Abs = tf.constant([-1,3,4,5.5])
tf.abs(x_Abs)
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 15/16
8/5/23, 11:46 PM TensorFlow_ComputerVision_01.ipynb - Colaboratory
https://colab.research.google.com/drive/1Jy4tvtKOHOWq_AjGlpMWuHaUMJ5VYjP_#scrollTo=zEe0GdL2sP0X&printMode=true 16/16