Hi everyone in this TensorFlow 2.0 basic tutorial we will look at the basic operations in TensorFlow 2.0 version. Tensorflow developed by google in the 2015 year. It developed to make mathematical operations for deep learning applications. Everything is defined as a tensor in TensorFlow or other deep learning libraries. TensorFlow supports three types of tensors. These are can define as a constant, variable, placeholder. A tensor is basically consists of an n-dimensional array which scalar, vector, matrix, tensor. The following image represents these tensor data types.


image.png

Firstly, we need to import the TensorFlow library like the following command before using it.

import tensorflow as tf
import numpy as np

Constants

As the name suggests, constants are tensors whose values cannot be changed afterward. Where can use this constant in the neural network algorithm? There are changeable and unchangeable values in the neural networks. We can use constant type to define input and output values. We can define the constants like the following command.

a = tf.constant(5)
print(a)
tf.Tensor(5, shape=(), dtype=int32)

Variables

Variables values are changeable afterward unlike constants. We can use variables to define weights and bias values in neural networks. We can define the variables like the following command.


a = tf.Variable(5)
print(a)
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=5>

Placeholders

Placeholders are used to feed values throughout the runtime. We assign values to a placeholder while running the training model. So we don’t need to define any data in the initialization. But, these placeholders are used in the TensorFlow 1.x version, so we can’t use these placeholders in TensorFlow 2.0 version. When you run the following command in TensorFlow 2.0 version you will get an error message.

a = tf.placeholder(tf.float32)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-7f46acaa4892> in <module>()
----> 1 a = tf.placeholder(tf.float32)

AttributeError: module 'tensorflow' has no attribute 'placeholder'

Constants

First, Let’s look at the some constant definations.


  • We can define a scalar constant.
x = tf.constant(5)
print("x shape : ",x.shape)
print("--------------------")
print(x)
x shape :  () 
-------------------- 
tf.Tensor(5, shape=(), dtype=int32)
  • We can define a vertor constant.
x = tf.constant([1,2,3,4,5])
print("x shape : ",x.shape)
print("--------------------")
print(x)
x shape :  (5,) -------------------- tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
  • We can define a matrix constant.
x = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
print("x shape : ",x.shape)
print("--------------------")
print(x)
x shape :  (3, 3)
--------------------
tf.Tensor(
[[1 2 3]
 [4 5 6]
 [7 8 9]], shape=(3, 3), dtype=int32)
  • We can define a tensor constant.
x = tf.constant([[[3,9,6],[4,8,6]],[[1,4,7],[2,5,8]]])
print("x shape : ",x.shape)
print("--------------------")
print(x)
x shape :  (2, 2, 3)
--------------------
tf.Tensor(
[[[3 9 6]
  [4 8 6]]

 [[1 4 7]
  [2 5 8]]], shape=(2, 2, 3), dtype=int32)

To create a tensor consist of zeros of all elements we can use the tf.zeros().

x = tf.zeros(shape=(2,3), dtype=tf.float32)
print(x)
tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]], shape=(2, 3), dtype=float32)

To create a tensor consist of ones of all elements we can use the tf.ones().

x = tf.ones(shape=(2,3), dtype=tf.float32)
print(x)
tf.Tensor(
[[1. 1. 1.]
 [1. 1. 1.]], shape=(2, 3), dtype=float32)

If we want to create another tensor that is the same size as a previously created tensor, we can use tf.zeros_like().

y = tf.zeros_like(x, dtype=tf.float32)
print(y)
tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]], shape=(2, 3), dtype=float32)

To create a diagonal tensor with tensorflow we can use the following command.

eye = tf.eye(num_rows=5, num_columns=5)
print(eye)
tf.Tensor(
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]], shape=(5, 5), dtype=float32)

Sequences

We can generate a sequence vector that has a start, stop, and number parameters.

linspace = tf.linspace(start=2, stop=12, num=5) # this will create a vector that consist of 5 values of 2-12 range
print(linspace)
tf.Tensor([ 2.   4.5  7.   9.5 12. ], shape=(5,), dtype=float64)
range_ = tf.range(start=0, limit=12, delta=1)
print(range_)
print("-------------------------------")
range_ = tf.range(12, delta=2) # start is zeros, limit is 12, delta is 2
print(range_)
tf.Tensor([ 0  1  2  3  4  5  6  7  8  9 10 11], shape=(12,), dtype=int32) 
------------------------------- 
tf.Tensor([ 0  2  4  6  8 10], shape=(6,), dtype=int32)

Random Tensors

We can generate lots of types random tensor using tensorflow.


To create random values with normal distribituion.

n_random = tf.random.normal(shape=(2,3), mean=0, stddev=1, dtype=tf.float32)
print(n_random)
tf.Tensor(
[[-0.00580942 -0.912885    0.66685474]
 [ 0.89941424 -1.7862207  -0.77971214]], shape=(2, 3), dtype=float32)

To create random values with uniform distribituion.

u_random = tf.random.uniform(shape=(2,3), minval=-5, maxval=5)
print(u_random)
tf.Tensor(
[[ 4.417536  -4.932959  -2.5741792]
 [ 3.9005604 -2.0956028  2.3735962]], shape=(2, 3), dtype=float32)

Variables

Variables are created using the tf.Variable class in TensorFlow. The variable values can also consist of constant values and random values.

n_random = tf.random.normal(shape=(2,3), mean=0, stddev=1, dtype=tf.float32)
constant = tf.constant([1,2,3,4,5], dtype=tf.float32)

var1 = tf.Variable(n_random, dtype=tf.float32)
print("var1 : ", var1)
print("-------------------------")
var2 = tf.Variable(constant, dtype=tf.float32)
print("var2 : ", var2)
n_random = tf.random.normal(shape=(2,3), mean=0, stddev=1, dtype=tf.float32)
constant = tf.constant([1,2,3,4,5], dtype=tf.float32)

var1 = tf.Variable(n_random, dtype=tf.float32)
print("var1 : ", var1)
print("-------------------------")
var2 = tf.Variable(constant, dtype=tf.float32)
print("var2 : ", var2)