{
  "slug": "Tensorflow-tutorial--Basic-operations-on-tensors-fca6c839ca08",
  "title": "Tensorflow tutorial, Basic operations on tensors",
  "subtitle": "In this tutorial, I am going to learn the fundamentals of tensors.",
  "excerpt": "In this tutorial, I am going to learn the fundamentals of tensors.",
  "date": "2024-03-29",
  "tags": [
    "Design Rag System",
    "Machine Learning"
  ],
  "readingTime": "7 min",
  "url": "https://medium.com/@mobinshaterian/tensorflow-tutorial-basic-operations-on-tensors-fca6c839ca08",
  "hero": "https://cdn-images-1.medium.com/max/800/1*wrm5tOXU9aNaOUlHQzzYXQ.jpeg",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Tensorflow tutorial, Basic operations on tensors"
    },
    {
      "type": "paragraph",
      "html": "In this tutorial, I am going to learn the fundamentals of tensors."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Install TensorFlow"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "pip install tensorflow[and-cuda]"
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/HPjBY1H-U4U"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*wrm5tOXU9aNaOUlHQzzYXQ.jpeg",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 640,
      "height": 832
    },
    {
      "type": "heading",
      "level": 2,
      "text": "GPU setup"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "nvidia-smi"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Install on CPU or GPU"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "# For GPU users\npip install tensorflow[and-cuda]# For CPU users\npip install tensorflow"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Verify the installation"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "Verify the CPU setup:python3 -c \"\nimport tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000,\n1000])))\"If a tensor is returned, you've installed TensorFlow successfully.Verify the GPU setup:python3 -c \"\nimport tensorflow as tf;\nprint(tf.config.list_physical_devices('GPU'))\"If a list of GPU devices is returned, you've installed TensorFlow successfully."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*Ixcm6GBWpeJtqEBjDkG29w.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 1416,
      "height": 631
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Test"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import tensorflow as tfprint(tf.__version__)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Basic"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import osos.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"2\"\nimport tensorflow as tf"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "# Initialization"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant(4.0)<tf.Tensor: shape=(), dtype=float32, numpy=4.0>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant(4.0, shape=(1,1))<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[4.]],\ndtype=float32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant(4.0, shape=(1,1), dtype=tf.float32)<tf.Tensor: shape=(1, 1), dtype=float32,\nnumpy=array([[4.]], dtype=float32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant([[1,2,3],[4,5,6]])<tf.Tensor: shape=(2, 3), dtype=int32, numpy=array([[1, 2, 3],\n[4, 5, 6]], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.ones((3,3))<tf.Tensor: shape=(3, 3), dtype=float32, numpy=array([[1., 1., 1.],       [1., 1.,\n1.],       [1., 1., 1.]], dtype=float32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.zeros((3,2))<tf.Tensor: shape=(3, 2), dtype=float32, numpy=array([[0., 0.],       [0., 0.],\n[0., 0.]], dtype=float32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.eye(3)<tf.Tensor: shape=(3, 3), dtype=float32, numpy=array([[1., 0., 0.],       [0., 1., 0.],\n[0., 0., 1.]], dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Normal distribution"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.random.normal((3,3), mean=0 , stddev=1)<tf.Tensor: shape=(3, 3), dtype=float32,\nnumpy=array([[ 0.8438105 , -1.3465139 ,  0.3282705 ],       [-1.3032753 ,  0.15576549,  2.9160573 ],\n[-0.57551485,  0.25881144, -1.4114394 ]], dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Uniform distribution"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.random.uniform((5,3) , minval=0, maxval=1)2.16.1<tf.Tensor: shape=(5, 3), dtype=float32,\nnumpy=array([[0.14925945, 0.68959737, 0.01986969],       [0.0124476 , 0.8936583 , 0.03025806],\n[0.22682643, 0.3770913 , 0.15388298],       [0.55487454, 0.16352475, 0.5743084 ],       [0.54566336,\n0.6106169 , 0.2642449 ]], dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Range"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.range(9)<tf.Tensor: shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8],\ndtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.range(start=1, limit=20, delta=2)<tf.Tensor: shape=(10,), dtype=int32, numpy=array([ 1,  3,\n5,  7,  9, 11, 13, 15, 17, 19], dtype=int32)>"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Cast"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.cast(x , dtype=tf.float64)<tf.Tensor: shape=(10,), dtype=float64, numpy=array([ 1.,  3.,  5.,\n7.,  9., 11., 13., 15., 17., 19.])>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Mathematics"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant([1,2,3])y = tf.constant([9,8,7])z  = tf.add(x,y)z  = x + y<tf.Tensor: shape=(3,),\ndtype=int32, numpy=array([10, 10, 10], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "z  = x - y<tf.Tensor: shape=(3,), dtype=int32, numpy=array([-8, -6, -4], dtype=int32)>z  = x /\ny<tf.Tensor: shape=(3,), dtype=float64, numpy=array([0.11111111, 0.25      , 0.42857143])>z  = x *\nyz = tf.multiply(x, y)<tf.Tensor: shape=(3,), dtype=int32, numpy=array([ 9, 16, 21], dtype=int32)>z\n= tf.tensordot(x, y , axes=1)<tf.Tensor: shape=(), dtype=int32, numpy=46>x = tf.reduce_sum(x*y ,\naxis=0)<tf.Tensor: shape=(), dtype=int32, numpy=46>z = x ** 5<tf.Tensor: shape=(3,), dtype=int32,\nnumpy=array([  1,  32, 243], dtype=int32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Matrix Multiply"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.random.normal((2,3))y = tf.random.normal((3,4))<tf.Tensor: shape=(2, 3), dtype=float32,\nnumpy=array([[ 1.396877  , -1.4690485 , -0.5183508 ],       [ 0.01694403,  1.6056603 ,\n0.76242363]], dtype=float32)><tf.Tensor: shape=(3, 4), dtype=float32, numpy=array([[-1.6349603 ,\n0.2476521 , -0.2984849 ,  0.16962565],       [-0.4788278 , -1.9370278 ,  1.0966969 , -2.8167336 ],\n[-0.9770468 ,  0.02774622, -1.6080935 , -0.03016708]],      dtype=float32)>z = x @ yz = tf.matmul(x,\ny)<tf.Tensor: shape=(2, 4), dtype=float32, numpy=array([[-1.0739641,  3.177145 , -1.194491 ,\n4.3905015],       [-1.5414612, -3.0848582,  0.5298166, -4.5428433]], dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Indexing"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant([2,3,4,5,6,3,34])print(x[:])tf.Tensor([ 2  3  4  5  6  3 34], shape=(7,),\ndtype=int32)print(x[::2])tf.Tensor([ 2  4  6 34], shape=(4,),\ndtype=int32)print(x[::-1])tf.Tensor([34  3  6  5  4  3  2], shape=(7,), dtype=int32)x_ind =\ntf.gather(x , [3,4])<tf.Tensor: shape=(2,), dtype=int32, numpy=array([5, 6], dtype=int32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Reshape"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant([2,3,4,5,6,3,34])y = tf.reshape(x , (3,2))2.16.1<tf.Tensor: shape=(3, 2),\ndtype=int32, numpy=array([[ 2,  3],       [ 4,  5],       [ 6, 34]], dtype=int32)>"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Number of dimensions"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.constant([2,3,4,5,6,34])x.ndim =>1x = tf.constant([[2,3,4] , [0,4,5]])x.ndim => 2"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Changeable Tensor"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x = tf.Variable([[2,3,4] , [0,4,5]])<tf.Variable 'Variable:0' shape=(2, 3) dtype=int32,\nnumpy=array([[2, 3, 4],       [0, 4, 5]], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "x[0].assign([[9,21,31]])<tf.Variable 'UnreadVariable' shape=(2, 3) dtype=int32, numpy=array([[ 9,\n21, 31],       [ 0,  4,  5]], dtype=int32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Random Generator"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "random_1 = tf.random.Generator.from_seed(42)random_1 = random_1.normal(shape=(3,2))<tf.Tensor:\nshape=(3, 2), dtype=float32, numpy=array([[-0.7565803 , -0.06854702],       [ 0.07595026, -1.2573844\n],       [-0.23193763, -1.8107855 ]], dtype=float32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "random_2 = tf.random.Generator.from_seed(42)random_2 = random_2.normal(shape=(3,2))<tf.Tensor:\nshape=(3, 2), dtype=float32, numpy=array([[-0.7565803 , -0.06854702],       [ 0.07595026, -1.2573844\n],       [-0.23193763, -1.8107855 ]], dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Shuffle a tensor"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "not_shuffled = tf.constant([ [10 ,20], [40,50], [90,100]])<tf.Tensor: shape=(3, 2), dtype=int32,\nnumpy=array([[ 10,  20],       [ 40,  50],       [ 90, 100]],\ndtype=int32)>tf.random.shuffle(not_shuffled)<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[\n40,  50],       [ 90, 100],       [ 10,  20]], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tf.random.set_seed(42) # global level random seedtf.random.shuffle(not_shuffled , seed=\n42)<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[ 10,  20],       [ 40,  50],       [ 90,\n100]], dtype=int32)><tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[ 10,  20],       [ 40,\n50],       [ 90, 100]], dtype=int32)>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*vLkje7C9HPJ2uezNNXjWhg.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 1204,
      "height": 239
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Trun Numpy into tensor"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import numpy as npnumpay_A = np.arange(1,25, dtype=np.int32numpay_Aarray([ 1,  2,  3,  4,  5,  6,\n7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,       18, 19, 20, 21, 22, 23, 24], dtype=int32)A =\ntf.constant(numpay_A)<tf.Tensor: shape=(24,), dtype=int32, numpy=array([ 1,  2,  3,  4,  5,  6,  7,\n8,  9, 10, 11, 12, 13, 14, 15, 16, 17,       18, 19, 20, 21, 22, 23, 24], dtype=int32)>A =\ntf.constant(numpay_A , shape=(2,12))<tf.Tensor: shape=(2, 12), dtype=int32, numpy=array([[ 1,  2,\n3,  4,  5,  6,  7,  8,  9, 10, 11, 12],       [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]],\ndtype=int32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Rank"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "rank_4_tensor = tf.zeros(shape=[2,3,4,5])"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "<tf.Tensor: shape=(2, 3, 4, 5), dtype=float32, numpy=array([[[[0., 0., 0., 0., 0.],         [0., 0.,\n0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]],        [[0., 0., 0., 0.,\n0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]],\n[[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0.,\n0., 0., 0.]]],       [[[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0.,\n0.],         [0., 0., 0., 0., 0.]],        [[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],\n[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]],        [[0., 0., 0., 0., 0.],         [0., 0.,\n0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]]]], dtype=float32)>"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "rank_4_tensor[0]"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "<tf.Tensor: shape=(3, 4, 5), dtype=float32, numpy=array([[[0., 0., 0., 0., 0.],        [0., 0., 0.,\n0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.]],       [[0., 0., 0., 0., 0.],\n[0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.]],       [[0., 0., 0.,\n0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.]]],\ndtype=float32)>"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "rank_4_tensor.shape"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "TensorShape([2, 3, 4, 5])"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "rank_4_tensor.ndim4"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tf.size(rank_4_tensor)<tf.Tensor: shape=(), dtype=int32, numpy=120>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Add Extra Dimension to exist tensor"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "rank_3 = tf.constant(np.random.randint(0,100, size=(2,3,4)))<tf.Tensor: shape=(2, 3, 4),\ndtype=int64, numpy=array([[[15, 43,  0, 82],        [68, 26, 52,  6],        [22,  6, 18, 30]],\n[[56,  8, 42, 58],        [12,  7, 81, 55],        [18, 46, 21, 80]]])>"
    },
    {
      "type": "paragraph",
      "html": "‍‍"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "rank_4 = rank_3[... , tf.newaxis]<tf.Tensor: shape=(2, 3, 4, 1), dtype=int64, numpy=array([[[[15],\n[43],         [ 0],         [82]],        [[68],         [26],         [52],         [ 6]],\n[[22],         [ 6],         [18],         [30]]],       [[[56],         [ 8],         [42],\n[58]],        [[12],         [ 7],         [81],         [55]],        [[18],         [46],\n[21],         [80]]]])>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "rank_4 = rank_3[ tf.newaxis , ... ]<tf.Tensor: shape=(1, 2, 3, 4), dtype=int64, numpy=array([[[[15,\n43,  0, 82],         [68, 26, 52,  6],         [22,  6, 18, 30]],        [[56,  8, 42, 58],\n[12,  7, 81, 55],         [18, 46, 21, 80]]]])>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*rqenzSwDearpPoWcXBwMyw.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 987,
      "height": 209
    },
    {
      "type": "code",
      "lang": "python",
      "code": "rank_4 = rank_3[ : , tf.newaxis , : , :  ]<tf.Tensor: shape=(2, 1, 3, 4), dtype=int64,\nnumpy=array([[[[15, 43,  0, 82],         [68, 26, 52,  6],         [22,  6, 18, 30]]],       [[[56,\n8, 42, 58],         [12,  7, 81, 55],         [18, 46, 21, 80]]]])>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "rank_4 = tf.expand_dims(rank_3 , axis=1)<tf.Tensor: shape=(2, 1, 3, 4), dtype=int64,\nnumpy=array([[[[15, 43,  0, 82],         [68, 26, 52,  6],         [22,  6, 18, 30]]],       [[[56,\n8, 42, 58],         [12,  7, 81, 55],         [18, 46, 21, 80]]]])>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Manipulating tensor"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor = tf.constant([[10,7],[3,4]])<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[10,  7],\n[ 3,  4]], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor + 10<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[20, 17],       [13, 14]],\ndtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tf.multiply(tensor , 10)<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[100,  70],       [ 30,\n40]], dtype=int32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Matrix manipulation"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*O_bhL8z_F5WPJIxZLBQ4cA.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 580,
      "height": 379
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[10,  7],       [ 3,  4]],\ndtype=int32)>tf.matmul(tensor , tensor)<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[121,\n98],       [ 42,  37]], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor @ tensor<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[121,  98],       [ 42,  37]],\ndtype=int32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Not matrix multiply"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor * tensor<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[100,  49],       [  9,  16]],\ndtype=int32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Transpose and Reshape"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*SpFZIbmIozq_YOXLismJ4A.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 1337,
      "height": 386
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Change dtype in tensor"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "A = tf.constant([7, 10])A.dtypetf.int32B = tf.cast(A , dtype=tf.float32)B.dtypetf.float32"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Aggregating"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor = tf.constant([-7 , -10])tf.abs(tensor)<tf.Tensor: shape=(2,), dtype=int32, numpy=array([ 7,\n10], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor = tf.constant(np.random.randint(0,100, size=(3,3)))<tf.Tensor: shape=(3, 3), dtype=int64,\nnumpy=array([[78, 98,  6],       [27,  1, 92],       [31, 13, 94]])>tf.size(tensor) , tensor.shape ,\ntensor.ndim(<tf.Tensor: shape=(), dtype=int32, numpy=9>, TensorShape([3, 3]), 2)"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tf.reduce_min(tensor)<tf.Tensor: shape=(), dtype=int64, numpy=1>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tf.reduce_max(tensor)<tf.Tensor: shape=(), dtype=int64, numpy=98>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tensor = tf.cast(tensor , dtype=tf.float32)tf.math.reduce_variance(tensor)<tf.Tensor: shape=(),\ndtype=float32, numpy=1206.3209>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Max Argument"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "A = tf.random.uniform(shape=[50])tf.argmax(A)<tf.Tensor: shape=(), dtype=int64,\nnumpy=47>A[47]<tf.Tensor: shape=(), dtype=float32, numpy=0.9869994>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "A = tf.random.uniform(shape=[50,2])tf.argmax(A,0)<tf.Tensor: shape=(2,), dtype=int64,\nnumpy=array([36, 27])>A[36,0]<tf.Tensor: shape=(), dtype=float32, numpy=0.9909723>A[27 ,\n1]<tf.Tensor: shape=(), dtype=float32, numpy=0.9717448>tf.reduce_max(A , 0)<tf.Tensor: shape=(2,),\ndtype=float32, numpy=array([0.9909723, 0.9717448], dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Squeeze"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "B = tf.constant(tf.random.uniform(shape=[20]) , shape=(1,1,1,1,20))<tf.Tensor: shape=(1, 1, 1, 1,\n20), dtype=float32, numpy=array([[[[[0.803156  , 0.49777734, 0.37054038, 0.9118674 , 0.637642  ,\n0.18209696, 0.63791955, 0.27701473, 0.04227114, 0.84219384,           0.90637195, 0.222556  ,\n0.9198462 , 0.68789077, 0.42705178,           0.878158  , 0.6943959 , 0.46567595, 0.52925766,\n0.33019018]]]]],      dtype=float32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "S = tf.squeeze(B)<tf.Tensor: shape=(20,), dtype=float32, numpy=array([0.803156  , 0.49777734,\n0.37054038, 0.9118674 , 0.637642  ,       0.18209696, 0.63791955, 0.27701473, 0.04227114,\n0.84219384,       0.90637195, 0.222556  , 0.9198462 , 0.68789077, 0.42705178,       0.878158  ,\n0.6943959 , 0.46567595, 0.52925766, 0.33019018],      dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "One-hot encoding"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "C = [0,1,2,3,5]tf.one_hot(C , depth=6)<tf.Tensor: shape=(5, 6), dtype=float32, numpy=array([[1., 0.,\n0., 0., 0., 0.],       [0., 1., 0., 0., 0., 0.],       [0., 0., 1., 0., 0., 0.],       [0., 0., 0.,\n1., 0., 0.],       [0., 0., 0., 0., 0., 1.]], dtype=float32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tf.one_hot(C , depth=6, on_value=1.0 , off_value=100.0)<tf.Tensor: shape=(5, 6), dtype=float32,\nnumpy=array([[  1., 100., 100., 100., 100., 100.],       [100.,   1., 100., 100., 100., 100.],\n[100., 100.,   1., 100., 100., 100.],       [100., 100., 100.,   1., 100., 100.],       [100., 100.,\n100., 100., 100.,   1.]], dtype=float32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Square"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "A = tf.range(1,10)<tf.Tensor: shape=(9,), dtype=int32, numpy=array([1, 2, 3, 4, 5, 6, 7, 8, 9],\ndtype=int32)>tf.square(A)<tf.Tensor: shape=(9,), dtype=int32, numpy=array([ 1,  4,  9, 16, 25, 36,\n49, 64, 81], dtype=int32)>"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "A = tf.cast(A , dtype=tf.float32)tf.sqrt(A)<tf.Tensor: shape=(9,), dtype=float32, numpy=array([1.\n, 1.4142135, 1.7320508, 2.       , 2.236068 , 2.4494898,       2.6457512, 2.828427 , 3.       ],\ndtype=float32)>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Finding access to GPU"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "tf.config.list_physical_devices()[PhysicalDevice(name='/physical_device:CPU:0',\ndevice_type='CPU')]tf.config.list_physical_devices(\"GPU\")[]!nvidia-smi"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://towardsdatascience.com/how-to-install-tensorflow-gpu-on-ubuntu-18-04-1c1d2d6d6fd2\" target=\"_blank\" rel=\"noreferrer noopener\">How to install TensorFlow GPU on UBUNTU 18.04</a>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*WFIJwo0AvGvaD6U22To3pw.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 1319,
      "height": 444
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*eiJY9PDJPiVs3j7Wcycs4A.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 1262,
      "height": 329
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*a99LtZ0iB3AaByIj0vnUXg.png",
      "alt": "Tensorflow tutorial, Basic operations on tensors",
      "caption": "",
      "width": 1286,
      "height": 471
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Stackademic 🎓"
    },
    {
      "type": "paragraph",
      "html": "Thank you for reading until the end. Before you go:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "Please consider <strong>clapping</strong> and <strong>following</strong> the writer! 👏",
        "Follow us <a href=\"https://twitter.com/stackademichq\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>X</strong></a><strong> | </strong><a href=\"https://www.linkedin.com/company/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>LinkedIn</strong></a><strong> | </strong><a href=\"https://www.youtube.com/c/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>YouTube</strong></a><strong> | </strong><a href=\"https://discord.gg/in-plain-english-709094664682340443\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Discord</strong></a>",
        "Visit our other platforms: <a href=\"https://plainenglish.io\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>In Plain English</strong></a><strong> | </strong><a href=\"https://cofeed.app/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>CoFeed</strong></a><strong> | </strong><a href=\"https://venturemagazine.net/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Venture</strong></a><strong> | </strong><a href=\"https://blog.cubed.run\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Cubed</strong></a>",
        "More content at <a href=\"https://stackademic.com\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Stackademic.com</strong></a>"
      ]
    }
  ]
}
