TensorFlow 2.x Tutorial Archives - Indian AI Production https://indianaiproduction.com/tensorflow-2-x-tutorial/ Artificial Intelligence Education Free for Everyone Wed, 22 Jun 2022 10:48:45 +0000 en-US hourly 1 https://i0.wp.com/indianaiproduction.com/wp-content/uploads/2019/06/Channel-logo-in-circle-473-x-472-px.png?fit=32%2C32&ssl=1 TensorFlow 2.x Tutorial Archives - Indian AI Production https://indianaiproduction.com/tensorflow-2-x-tutorial/ 32 32 163118462 How To Get Minimum Value From Tensors In TensorFlow? https://indianaiproduction.com/tensorflow-minimum-function/ https://indianaiproduction.com/tensorflow-minimum-function/#respond Fri, 22 Oct 2021 05:02:22 +0000 https://indianaiproduction.com/?p=1873 In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.minimum() function. We will learn how to calculate the minimum values from tensors in TensorFlow using tf.minimum() function. tf.minimum() : Returns the minimum value by comparing of x and y (i.e. x > y ? x : y) element-wise.It works with list, tuple, scaler, Numpy arraytf variable/constant/placeholder/SparceMatrix with …

How To Get Minimum Value From Tensors In TensorFlow? Read More »

The post How To Get Minimum Value From Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.minimum() function. We will learn how to calculate the minimum values from tensors in TensorFlow using tf.minimum() function.

tf.minimum() : Returns the minimum value by comparing of x and y (i.e. x > y ? x : y) element-wise.
It works with list, tuple, scaler, Numpy array
tf variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple

Syntax: tf.minimum(x, y, name=None)

Args
xTensor. Must be one of the following types: bfloat16halffloat32float64int8uint8int16uint16int32uint32int64uint64.
yTensor. Must have the same type as x.
nameA name for the operation (optional).
Returns
Tensor. Has the same type as x.

Get Minimum Value from 2 Variables using TensorFlow

import tensorflow as tf
import numpy as np

Get Min Value of Scaler

a = 10
b = 20

tf.minimum(a,b)

### to get max value only
#tf.minimum(a,b).numpy() # 10

Output >>>

<tf.Tensor: shape=(), dtype=int32, numpy=10>

Get Min Value of 2 List

l1 = [1,2,3,4]
l2 = [5,6,7,8]

tf.minimum(l1, l2)

Get Min Value of 2 Tuple

t1 = (1,2,3,4)
t2 = (5,6,7,8)

tf.minimum(t1,t2)

Get Min Value of List/Tuple with Scaler

l1 = [1,2,3,4]
a =10
tf.minimum(l1, a)

Get Min Value of 2 Numpy Array

npa1 = np.array([1,4,3,9,5])
npa2 = np.array([2,2,3,4,5])

tf.minimum(npa1, npa2)

Get Min Value of Numpy array with Scaler

npa1 = np.array([1,2,3,4,5])
a = 10

tf.minimum(npa1, a)

Get Min Value of Numpy array with single element np array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([[2]])

tf.minimum(npa1, npa2)

Get Min Value of 2 TF Constant Tensor

tf_ct1 = tf.constant([2,2,3,8])
tf_ct2 = tf.constant([3,2,5,4])

tf.minimum(tf_ct1, tf_ct2)

Get Min Value of TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.minimum(tf_ct1, a)

Get Min Value of 2 TF Variable

tf_vr1 = tf.Variable([2,2,3,8])
tf_vr2 = tf.Variable([3,2,5,4])

tf.minimum(tf_vr1, tf_vr2)

Get Min Value of TF Variable with Scaler

tf_vr1 = tf.Variable([3,2,5,4])
a = 3
tf.minimum(tf_vr1, a)

The post How To Get Minimum Value From Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-minimum-function/feed/ 0 1873
How to Get Maximum Value from Tensors in TensorFlow? https://indianaiproduction.com/tensorflow-maximum-function/ https://indianaiproduction.com/tensorflow-maximum-function/#respond Thu, 21 Oct 2021 04:22:59 +0000 https://indianaiproduction.com/?p=1870 In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.maximum() function. We will learn how to calculate the maximum values from tensors in TensorFlow using tf.maximum() function. tf.maximum() : Returns the maximum value by comparing of x and y (i.e. x > y ? x : y) element-wise.It work with list, tuple, scaler, Numpy arraytf variable/constant/placeholder/SparceMatrix with …

How to Get Maximum Value from Tensors in TensorFlow? Read More »

The post How to Get Maximum Value from Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.maximum() function. We will learn how to calculate the maximum values from tensors in TensorFlow using tf.maximum() function.

tf.maximum() : Returns the maximum value by comparing of x and y (i.e. x > y ? x : y) element-wise.
It work with list, tuple, scaler, Numpy array
tf variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple

Syntax: tf.maximum(x, y, name=None)

Args
xTensor. Must be one of the following types: bfloat16halffloat32float64int8uint8int16uint16int32uint32int64uint64.
yTensor. Must have the same type as x.
nameA name for the operation (optional).
Returns
Tensor. Has the same type as x.

Get maximum Value from 2 Variables using TensorFlow

import tensorflow as tf
import numpy as np

Get Max Value of Scaler

a = 10
b = 20
tf.maximum(a,b)

#tf.maximum(a,b).numpy() # 20

Output>>>

<tf.Tensor: shape=(), dtype=int32, numpy=20>

Get Max Value from 2 List

l1 = [1,2,3,4]
l2 = [5,6,7,8]

tf.maximum(l1, l2)
l1 = [1,2,3,4]
l2 = [5]

tf.maximum(l1, l2)

Get Max Value from 2 Tuple

t1 = (1,2,3,4)
t2 = (5,6,7,8)

tf.maximum(t1,t2)

Get Max Value of List/Tuple with Scaler

l1 = [1,2,3,4]
a =10
tf.maximum(l1, a)

Get Max Value of 2 Numpy Array

npa1 = np.array([1,4,3,9,5])
npa2 = np.array([2,2,3,4,5])

tf.maximum(npa1, npa2)

Get Max Value of Numpy array with Scaler

npa1 = np.array([1,2,3,4,5])
a = 3

tf.maximum(npa1, a)

Get Max Value of Numpy array with single element np array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([[2]])

tf.maximum(npa1, npa2)

Get Max Value of 2 TF Constant Tensor

tf_ct1 = tf.constant([2,2,3,8])
tf_ct2 = tf.constant([3,2,5,4])

tf.maximum(tf_ct1, tf_ct2)

Get Max Value of TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.maximum(tf_ct1, a)
tf_ct1 = tf.constant([1,2,3,4])
l1 = [3,6,8,2]
tf.maximum(tf_ct1, l1)

Get Max Value of 2 TF Variable

tf_vr1 = tf.Variable([2,2,3,8])
tf_vr2 = tf.Variable([3,2,5,4])

tf.maximum(tf_vr1, tf_vr2)

Get Max Value of TF Variable with Scaler

tf_vr1 = tf.Variable([3,2,5,4])
a = 3
tf.maximum(tf_vr1, a)

The post How to Get Maximum Value from Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-maximum-function/feed/ 0 1870
How To Calculate Power Of Tensors In TensorFlow? https://indianaiproduction.com/tensorflow-power-function/ https://indianaiproduction.com/tensorflow-power-function/#respond Wed, 20 Oct 2021 04:20:26 +0000 https://indianaiproduction.com/?p=1865 In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.pow() function. We will learn how to calculate the power of tensors in TensorFlow using tf.pow() function. tf.pow(): Calculate the power of one value to another, Element wise power calculationIt can be given the power of scaler, Numpy arrayIt allow tf variable/constant/placeholder/SparceMatrix with Specific datatype Data …

How To Calculate Power Of Tensors In TensorFlow? Read More »

The post How To Calculate Power Of Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.pow() function. We will learn how to calculate the power of tensors in TensorFlow using tf.pow() function.

tf.pow(): Calculate the power of one value to another, Element wise power calculation
It can be given the power of scaler, Numpy array
It allow tf variable/constant/placeholder/SparceMatrix with Specific datatype

Data Type : float16, float32, float64, int32, int64, complex64, or complex128.

Note: Don’t pass data in integer format, it will through error

Syntax: tf.pow(x, y, name=None)

Args
xTensor of type float16float32float64int32int64complex64, or complex128.
yTensor of type float16float32float64int32int64complex64, or complex128.
nameA name for the operation (optional).
Returns
Tensor.

Calculate the Power using TensorFlow

import tensorflow as tf
import numpy as np

Power of Scaler

a = 4
b = 2

tf.pow(a, b)

Output>>>

<tf.Tensor: shape=(), dtype=int32, numpy=16>

Power of Numpy Array

npa1 = np.array([4,9,16], dtype = np.float64)

tf.pow(npa1, 2)
npa1 = np.array([1,2,3], dtype = np.float64)
npa2 = np.array([1,2,3], dtype = np.float64)

tf.pow(npa1, npa2)
y = np.ones(6).reshape(2, 1, 3, 1)*4
print("y : ", y)
tf.pow(y, 2)

Power of Tensor

tf_ct1 = tf.constant([4,9,16], dtype='float')

tf.pow(tf_ct1, 3)
tf_ct1 = tf.constant([4,9,16], dtype='float')
tf_ct2 = tf.constant([3,2,1], dtype='float')

tf.pow(tf_ct1, tf_ct2)

Power of TF Variable

tf_vr1 = tf.Variable([4,9,16], dtype='half')

tf.pow(tf_vr1, 2)
tf_vr1 = tf.Variable([4,9,16], dtype='float')
tf_vr2 = tf.Variable([3,2,1], dtype='float')

tf.pow(tf_vr1, tf_vr2)
tf_vr1 = tf.Variable([4,9,16], dtype='float')
tf_vr2 = tf.Variable([3], dtype='float')

tf.pow(tf_vr1, tf_vr2)

The post How To Calculate Power Of Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-power-function/feed/ 0 1865
How to do division/divide of Tensors in TensorFlow? https://indianaiproduction.com/tensorflow-division-function/ https://indianaiproduction.com/tensorflow-division-function/#respond Tue, 19 Oct 2021 04:25:39 +0000 https://indianaiproduction.com/?p=1854 In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.divide() function. We will learn how to do the division of tensors in TensorFlow using tf.devide() function. tf.divide() : Do Element wise division,It can be divide scaler, Numpy array but not with list, tuple.tf variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple Note: / …

How to do division/divide of Tensors in TensorFlow? Read More »

The post How to do division/divide of Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.divide() function. We will learn how to do the division of tensors in TensorFlow using tf.devide() function.

tf.divide() : Do Element wise division,
It can be divide scaler, Numpy array but not with list, tuple.
tf variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple

Note: / or // operator can be use to divide 2 tensors

Syntax: tf.divide(x, y, name=None)

Args
xTensor
yTensor
nameA name for the operation (optional).
Returns
Tensor with same shape as input

Division of 2 Variables with TensorFlow

import tensorflow as tf
import numpy as np

Scaler division

a = 10
b = 20
print(" a/b =", a/b)
print(" a/b =", a//b)

tf.divide(a,b) #0.5

tf.divide(b, a) #2.0

Divide 2 Numpy Array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([1,2,3,4,5])

tf.divide(npa1, npa2)

Divide Numpy array with Scaler

npa1 = np.array([1,2,3,4,5])
a = 10

tf.divide(npa1, a)

Divide Numpy array with single element np array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([[1]])

tf.divide(npa1, npa2)

Divide 2 TF Constant Tensor

tf_ct1 = tf.constant([1,2,3,4])
tf_ct2 = tf.constant([1,2,3,4])

tf.divide(tf_ct1, tf_ct2)
# Division with operator /
tf_ct1 = tf.constant([1,2,3,4])
tf_ct2 = tf.constant([1,2,3,4])

tf_ct1 / tf_ct2
# Division with operator /
tf_ct1 = tf.constant([1,2,3,4])
tf_ct2 = tf.constant([1,2,3,4])

tf_ct1 // tf_ct2

Divide TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.divide(tf_ct1, a)
# division with operator /
tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf_ct1 / a

Divide 2 TF Variable

tf_vr1 = tf.Variable([1,2,3,4])
tf_vr2 = tf.Variable([1,2,3,4])

tf.divide(tf_vr1, tf_vr2)

Divide TF Variable with Scaler

tf_vr1 = tf.Variable([1,2,3,4])
a = 10
tf.divide(tf_vr1, a)

Divide 2 Numpy array with different Shape

x = np.ones(6).reshape(1, 2, 1, 3) # (2,2,3,3)
print("x: ", x)
y = np.ones(6).reshape(2, 1, 3, 1)
print("y: ", y)

tf.divide(x, y)

The post How to do division/divide of Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-division-function/feed/ 0 1854
How to Calculate Square Root of Tensors in TensorFlow? https://indianaiproduction.com/tensorflow-sqrt-function/ https://indianaiproduction.com/tensorflow-sqrt-function/#respond Tue, 19 Oct 2021 04:11:08 +0000 https://indianaiproduction.com/?p=1855 In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.sqrt() function. We will learn how to calculate the square root of tensors in TensorFlow using tf.sqrt() function. tf.sqrt() : Calculate Element wise square root of numpy array and TF tensor,It can be give sqrt of list, tuple, scalerIt allow tf variable/constant/placeholder/SparceMatrix with …

How to Calculate Square Root of Tensors in TensorFlow? Read More »

The post How to Calculate Square Root of Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
In TensorFlow 2.0 Python Tutorial, We will Learn about the TensorFlow Math Module tf.sqrt() function. We will learn how to calculate the square root of tensors in TensorFlow using tf.sqrt() function.

tf.sqrt() : Calculate Element wise square root of numpy array and TF tensor,
It can be give sqrt of list, tuple, scaler
It allow tf variable/constant/placeholder/SparceMatrix with Specific datatype

Data Type : bfloat16, half, float32, float64, complex64, complex128.

Note: Don’t give ‘int’ data type, tf.sqrt() will through error

Syntax: tf.sqrt(x, name=None)

Args
xtf.Tensor of type bfloat16halffloat32float64complex64complex128
nameA name for the operation (optional).
Returns
tf.Tensor of same size, type and sparsity as x.If x is a SparseTensor, returns SparseTensor(x.indices, tf.math.sqrt(x.values, ...), x.dense_shape)

Calculate the Square Root using TensorFlow

import tensorflow as tf
import numpy as np

Square Root of Scaler

a = 9.0
tf.sqrt(a)

Square Root of List

l1 = [4.0,9.0,16.0]

tf.sqrt(l1)

Square Root of Tuple

t1 = (4.0,9.0,16.0)

tf.sqrt(t1)

Square Root of Numpy Array

npa1 = np.array([4,9,16], dtype = np.float64)

tf.sqrt(npa1)
y = np.ones(6).reshape(2, 1, 3, 1)*4
print("Y: " , y)
tf.sqrt(y)

Square Root of TensorFlow Tensor

tf.sqrt(tf.convert_to_tensor(4, dtype='float')) # using scaler value
tf.sqrt(tf.convert_to_tensor([4,9,16], dtype='float')) # using list

Square Root of TF Constant Tensor

tf_ct1 = tf.constant([4,9,16], dtype='float')

tf.sqrt(tf_ct1)

Square Root of TF Variable

tf_vr1 = tf.Variable([4,9,16], dtype='half')

tf.sqrt(tf_vr1)

The post How to Calculate Square Root of Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-sqrt-function/feed/ 0 1855
How To Multiplication Of 2 Tensors In TensorFlow? https://indianaiproduction.com/tensorflow-multiply-function/ https://indianaiproduction.com/tensorflow-multiply-function/#respond Wed, 13 Oct 2021 04:19:18 +0000 https://indianaiproduction.com/?p=1850 We will learn how to do multiplication in TensorFlow using tf.multiply() function. tf.multiply() : Do Element wise Multiplication,It can be multiply list, tuple, scaler,tf variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple Note: * operator can be used to multiply 2 tensors Syntax: tf.multiply(x, y, name=None) Args x A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128. …

How To Multiplication Of 2 Tensors In TensorFlow? Read More »

The post How To Multiplication Of 2 Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
We will learn how to do multiplication in TensorFlow using tf.multiply() function.

tf.multiply() : Do Element wise Multiplication,
It can be multiply list, tuple, scaler,
tf variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple

Note: * operator can be used to multiply 2 tensors

Syntax: tf.multiply(x, y, name=None)

Args
xA Tensor. Must be one of the following types: bfloat16halffloat32float64uint8int8uint16int16int32int64complex64complex128.
yTensor. Must have the same type as x.
nameA name for the operation (optional).
Returns
Tensor. Has the same type as x.
Raises
InvalidArgumentError: When x and y have incompatible shapes or types.

Multiplication of 2 Variables with TensorFlow

import tensorflow as tf
import numpy as np

Scaler Multiply

a = 10
b = 20

tf.multiply(a,b)

Multiply 2 List

l1 = [1,2,3,4]
l2 = [5,6,7,8]

tf.multiply(l1, l2)

Multiply 2 Tuple

t1 = (1,2,3,4)
t2 = (5,6,7,8)

tf.multiply(t1,t2)

Multiply List/Tuple with Scaler

l1 = [1,2,3,4]
a =10
tf.multiply(a, l1)

Multiply of 2 list/Tuple with Diiferent Size

If the size of 2 variables is different then tf.subtract() function will through error. If any one of the variables contains only a single item then it will do the element-wise operation.

#this code will through error
l1 = [1,2,3,4]
l2 = [5,6]

tf.multiply(l1, l2)

Multiply 2 Numpy Array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([1,2,3,4,5])

tf.multiply(npa1, npa2)

Multiply Numpy array with Scaler

npa1 = np.array([1,2,3,4,5])
a = 10

tf.multiply(npa1, a)

Multiply Numpy array with single element np array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([[1]])

tf.multiply(npa1, npa2)

Multiply 2 TF Constant Tensor

tf_ct1 = tf.constant([1,2,3,4])
tf_ct2 = tf.constant([1,2,3,4])

tf.multiply(tf_ct1, tf_ct2)
tf_ct1 = tf.constant([1,2,3,4])
tf_ct2 = tf.constant([1,2,3,4])

tf_ct1 * tf_ct2

Multiply TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.multiply(tf_ct1, a)
tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf_ct1 * a

Multiply 2 TF Variable

tf_vr1 = tf.Variable([1,2,3,4])
tf_vr2 = tf.Variable([1,2,3,4])

tf.multiply(tf_vr1, tf_vr2)

Multiply TF Variable with Scaler

tf_vr1 = tf.Variable([1,2,3,4])
a = 10
tf.multiply(tf_vr1, a)

Multiply 2 Numpy array with different Shape

x = np.ones(6).reshape(1, 2, 1, 3) # (2,2,3,3)
print("x: ", x)
y = np.ones(6).reshape(2, 1, 3, 1)
print("y: ", y)

tf.multiply(x, y)

The post How To Multiplication Of 2 Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-multiply-function/feed/ 0 1850
How To Subtraction of 2 Tensors In TensorFlow? https://indianaiproduction.com/tensorflow-subtract-function/ https://indianaiproduction.com/tensorflow-subtract-function/#respond Tue, 12 Oct 2021 03:49:02 +0000 https://indianaiproduction.com/?p=1847 We will learn how to do suntraction in TensorFlow using tf.subtract() function. tf.subtract() : Do Element wise subtraction with x & yIt can subtract list, tuple, scaler, TensorFlow variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple. Note: – operator can be used to subtract 2 tensors Syntax: tf.subtract(x, y, name=None) Args x A Tensor. Must be one of the following …

How To Subtraction of 2 Tensors In TensorFlow? Read More »

The post How To Subtraction of 2 Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
We will learn how to do suntraction in TensorFlow using tf.subtract() function.

tf.subtract() : Do Element wise subtraction with x & y
It can subtract list, tuple, scaler, TensorFlow variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple.

Note: – operator can be used to subtract 2 tensors

Syntax: tf.subtract(x, y, name=None)

Args
xTensor. Must be one of the following types: bfloat16halffloat32float64uint8int8uint16int16int32int64complex64complex128uint32uint64.
yTensor. Must have the same type as x.
nameA name for the operation (optional).
Returns
Tensor. Has the same type as x.

Subtraction of 2 Variables with TensorFlow

import tensorflow as tf
import numpy as np

Scaler subtract

a = 10
b = 20

tf.subtract(a,b)

subtract 2 List

l1 = [1,2,3,4]
l2 = [5,6,7,8]

tf.subtract(l2, l1)

subtract 2 Tuple

t1 = (1,2,3,4)
t2 = (5,6,7,8)

tf.subtract(t1,t2)

subtract List/Tuple with Scaler

l1 = [1,2,3,4]
a =10

tf.subtract(l1, a)

subtract of 2 list/Tuple with Diiferent Size

If the size of 2 variables is different then tf.subtract() function will through error. If any one of the variables contains only a single item then it will do the element-wise operation.

#this code will through error
l1 = [1,2,3,4]
l2 = [5,6]

tf.subtract(l1, l2)

subtract 2 Numpy Array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([1,2,3,4,5])

tf.subtract(npa1, npa2)

subtract Numpy array with Scaler

npa1 = np.array([1,2,3,4,5])
a = 10

tf.subtract(npa1, a)

subtract Numpy array with single element np array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([[1]])

tf.subtract(npa1, npa2)

subtract 2 TF Constant Tensor

tf_ct1 = tf.constant([1,2,3,4])
tf_ct2 = tf.constant([1,2,3,4])

tf.subtract(tf_ct1, tf_ct2)

subtract TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.subtract(tf_ct1, a)

subtract 2 TF Variable

tf_vr1 = tf.Variable([1,2,3,4])
tf_vr2 = tf.Variable([1,2,3,4])

tf.subtract(tf_vr1, tf_vr2)

subtract TF Variable with Scaler

tf_vr1 = tf.Variable([1,2,3,4])
a = 10
tf.subtract(a, tf_vr1)

subtract 2 Numpy array with different Shape

x = np.ones(6).reshape(1, 2, 1, 3) # (2,2,3,3)
print("x: ", x)
y = np.ones(6).reshape(2, 1, 3, 1)
print("y: ", y)
 
tf.subtract(x, y)

The post How To Subtraction of 2 Tensors In TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-subtract-function/feed/ 0 1847
How to addition/add 2 Tensors in TensorFlow? https://indianaiproduction.com/tensorflow-add-function/ https://indianaiproduction.com/tensorflow-add-function/#respond Mon, 11 Oct 2021 03:42:27 +0000 https://indianaiproduction.com/?p=1844 We will learn how to do addition in TensorFlow using tf.add() function. tf.add() : Do Element wise Addition with x & yIt can add list, tuple, scaler, TensorFlow variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple. Note: + operator can be use to add 2 tensors Syntax: tf.add(x, y, name=None) Args x A tf.Tensor. Must …

How to addition/add 2 Tensors in TensorFlow? Read More »

The post How to addition/add 2 Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
We will learn how to do addition in TensorFlow using tf.add() function.

tf.add() : Do Element wise Addition with x & y
It can add list, tuple, scaler, TensorFlow variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple.

Note: + operator can be use to add 2 tensors

Syntax: tf.add(x, y, name=None)

Args
xtf.Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.
ytf.Tensor. Must have the same type as x.
nameA name for the operation (optional)

Addition of 2 Variables with TensorFlow

import tensorflow as tf
import numpy as np

Scaler Adding

a = 10
b = 20
tf.add(a,b) 

Add 2 List

l1 = [1,2,3,4]
l2 = [5,6,7,8]

tf.add(l1, l2)

Add List/Tuple with Scaler

l1 = [1,2,3,4]
a =10
tf.add(l1, a)

Addition of 2 list/Tuple with Diiferent Size

If the size of 2 variables is different then tf.add() function will through error. If any one of the variables contains only a single item then it will do the element-wise operation.

#this code will through error
l1 = [1,2,3,4]
l2 = [5,6]

tf.add(l1, l2)

Add 2 Numpy Array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([1,2,3,4,5])

tf.add(npa1, npa2)

Add Numpy array with Scaler

npa1 = np.array([1,2,3,4,5])
a = 10

tf.add(npa1, a)

Add Numpy array with single element np array

npa1 = np.array([1,2,3,4,5])
npa2 = np.array([[1]])

tf.add(npa1, npa2)

Add 2 TF Constant Tensor

tf_ct1 = tf.constant([1,2,3,4])
tf_ct2 = tf.constant([1,2,3,4])

tf.add(tf_ct1, tf_ct2)

Add TF Constant Tensor with Scaler

tf_ct1 = tf.constant([1,2,3,4])
a = 10
tf.add(tf_ct1, a)

Add 2 TF Variable

tf_vr1 = tf.Variable([1,2,3,4])
tf_vr2 = tf.Variable([1,2,3,4])

tf.add(tf_vr1, tf_vr2)

Add TF Variable with Scaler

tf_vr1 = tf.Variable([1,2,3,4])
a = 10
tf.add(tf_vr1, a)

Add 2 Numpy array with different Shape

x = np.ones(6).reshape(1, 2, 1, 3) # (2,2,3,3)
print("x: ", x)
y = np.ones(6).reshape(2, 1, 3, 1)
print("y: ", y)

tf.add(x, y)

The post How to addition/add 2 Tensors in TensorFlow? appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/tensorflow-add-function/feed/ 0 1844
Create Zeros vs Zeros _like vs Zeros _initializer Tensor Using TensorFlow 2.0 Python Tutorial https://indianaiproduction.com/create-tensorflow-zeros-tensor/ https://indianaiproduction.com/create-tensorflow-zeros-tensor/#respond Fri, 02 Jul 2021 04:17:19 +0000 https://indianaiproduction.com/?p=1806 In TensorFlow 2.0, we can create Zeros Tensor(every element in tensor are zeros) in different ways using tf.zeros() and tf.zeros_like() and tf.zeros_initializer(). Create Zeros Tesnor using tf.zeros() Creates a tensor with all elements set to zero (0) Syntax: Create Zeros Tesnor using tf.zeros_like() Creates a tensor with all elements set to zero. Syntax: Create Zeros Tesnor using tf.zeros_initializer() Initializer that generates …

Create Zeros vs Zeros _like vs Zeros _initializer Tensor Using TensorFlow 2.0 Python Tutorial Read More »

The post Create Zeros vs Zeros _like vs Zeros _initializer Tensor Using TensorFlow 2.0 Python Tutorial appeared first on Indian AI Production.

]]>
In TensorFlow 2.0, we can create Zeros Tensor(every element in tensor are zeros) in different ways using tf.zeros() and tf.zeros_like() and tf.zeros_initializer().

Create Zeros Tesnor using tf.zeros()

Creates a tensor with all elements set to zero (0)

Syntax:

tf.zeros(shape, dtype=tf.dtypes.float32, name=None) -> Return ZerosTensor

"""
Create Zeros vs Zeros _like vs Zeros _initializer Tensor Using TensorFlow 2.0 Python Tutorial | DL | ML: https://youtu.be/uIzg8RG1IZo
"""

# ### Import TensorFlow 2
import tensorflow as tf

# ### Creates a tensor with all elements set to zero.
zeros_2dt = tf.zeros((2,2), dtype=tf.int32)
zeros_2dt

zeros_3dt = tf.zeros((2,2,4), dtype=tf.int8)
zeros_3dt

zeros_3dt.shape

zeros_3dt.dtype

zeros_3dt.numpy()

zeros_3dt.numpy().tolist()

Create Zeros Tesnor using tf.zeros_like()

Creates a tensor with all elements set to zero.

Syntax:

tf.zeros_like(input, dtype=None, name=None) -> Return Zeros Tensor
# #### Creates a tensor with all elements set to zero.

cont_2dt = tf.constant([[1,2,3],[4,5,6]])
cont_2dt

zl_2dt_from_cont = tf.zeros_like(cont_2dt)
zl_2dt_from_cont

var_2dt = tf.Variable([[1,2,3],[4,5,6]])
var_2dt

zl_2dt_from_var = tf.zeros_like(var_2dt, dtype=tf.bool)
zl_2dt_from_var

Create Zeros Tesnor using tf.zeros_initializer()

Initializer that generates tensors initialized to 0.

Initializers allow you to pre-specify an initialization strategy, encoded in the Initializer object, without knowing the shape and dtype of the variable being initialized.

Syntax:

tf.zeros_initializer()
def zeros_variable(shape, dtype, initializer):
    return tf.Variable(initializer(shape=shape, dtype=dtype))

zi_t_var= zeros_variable((2,2), tf.float32, tf.zeros_initializer())
zi_t_var

zeros_variable((3,2,5), tf.bool, tf.zeros_initializer())

tf.Variable(tf.zeros_initializer()((2,2), tf.float32))

REF:
tf.zeros: https://www.tensorflow.org/api_docs/python/tf/zeros
tf.zeros_like: https://www.tensorflow.org/api_docs/python/tf/zeros_like
tf.zeros_initializer: https://www.tensorflow.org/api_docs/python/tf/zeros_initializer

The post Create Zeros vs Zeros _like vs Zeros _initializer Tensor Using TensorFlow 2.0 Python Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/create-tensorflow-zeros-tensor/feed/ 0 1806
Create Ones vs Ones_like vs Ones_initializer Tensor Using TensorFlow 2.0 Python Tutorial https://indianaiproduction.com/create-tensorflow-ones-tensor/ https://indianaiproduction.com/create-tensorflow-ones-tensor/#respond Wed, 30 Jun 2021 03:55:58 +0000 https://indianaiproduction.com/?p=1803 In TensorFlow 2.0, we can create Ones Tensor(every element in tensor are ones) in different ways using tf.ones() and tf.ones_like() and tf.ones_initializer(). Create Ones Tesnor using tf.ones() Creates a tensor with all elements set to one (1) Syntax: Create Ones Tesnor using tf.ones_like() Creates a tensor of all ones that has the same shape as …

Create Ones vs Ones_like vs Ones_initializer Tensor Using TensorFlow 2.0 Python Tutorial Read More »

The post Create Ones vs Ones_like vs Ones_initializer Tensor Using TensorFlow 2.0 Python Tutorial appeared first on Indian AI Production.

]]>
In TensorFlow 2.0, we can create Ones Tensor(every element in tensor are ones) in different ways using tf.ones() and tf.ones_like() and tf.ones_initializer().

Create Ones Tesnor using tf.ones()

Creates a tensor with all elements set to one (1)

Syntax:

tf.ones(shape, dtype=tf.dtypes.float32, name=None) -> Return Ones Tensor
"""
Create Ones vs Ones_like vs Ones_initializer Tensor Using TensorFlow 2.0 Python Tutorial | DL | ML: https://youtu.be/wAnh5U1hmo0
"""

# Import TensorFlow 2
import tensorflow as tf

# create ones tensor
ones_2dt = tf.ones((2,2), tf.float32)
ones_2dt

ones_3dt = tf.ones((2,2,4), tf.float32)
ones_3dt

Create Ones Tesnor using tf.ones_like()

Creates a tensor of all ones that has the same shape as the input

Syntax:

tf.ones_like(input, dtype=None, name=None) -> Return Ones Tensor
# Import TensorFlow 2
import tensorflow as tf

# create ones tensor from input

cont_2dt = tf.constant([[1,2,3],[4,5,6]])
cont_2dt

ones_2dt_from_cont = tf.ones_like(cont_2dt, tf.float32)
ones_2dt_from_cont

var_2dt_from_var = tf.Variable([[1,2,3],[4,5,6]])
var_2dt_from_var

ones_2dt_from_var = tf.ones_like(var_2dt_from_var, tf.int32)
ones_2dt_from_var

Create Ones Tesnor using tf.ones_initializer()

Initializer that generates tensors initialized to 1.

Initializers allow you to pre-specify an initialization strategy, encoded in the Initializer object, without knowing the shape and dtype of the variable being initialized.

Syntax:

tf.ones_initializer()
# Import TensorFlow 2
import tensorflow as tf

# create ones tensor TensorFlow variable from the input

def ones_variable(shape, dtype, initializer):
    return tf.Variable(initializer(shape=shape, dtype=dtype))

ones_variable((2,2), tf.float32,  tf.ones_initializer())

ones_variable((2,3,5), tf.int32,  tf.ones_initializer())

REF:
tf.ones: https://www.tensorflow.org/api_docs/python/tf/ones
tf.ones_like: https://www.tensorflow.org/api_docs/python/tf/ones_like
tf.ones_initialize: https://www.tensorflow.org/api_docs/python/tf/ones_initializer

The post Create Ones vs Ones_like vs Ones_initializer Tensor Using TensorFlow 2.0 Python Tutorial appeared first on Indian AI Production.

]]>
https://indianaiproduction.com/create-tensorflow-ones-tensor/feed/ 0 1803