In TensorFlow 2.0 Python Tutorial in Hindi, We will Learn about the TensorFlow Programming element to build Constant Tensor using tf.constant()
In this tutorial going to cover:
- What is constant in TensorFlow?
- What is TF constant?
- How do you print a constant in TensorFlow?
- Is TF constant trainable?
tf.constant(): Creates a constant tensor from a tensor-like object.
Syntax: tf.constant(value, dtype=None, shape=None, name='Const') Args: value: A constant value (or list) of output type `dtype`. dtype: The type of the elements of the resulting tensor. shape: Optional dimensions of resulting tensor. name: Optional name for the tensor. Returns: A Constant Tensor.
Note:
- tf.Session() not in 2.x
- Most of TensorFlow syntax same like Numpy
- Use: While building neural network graph need constant varibale like input data,
it never change while training
Create Constant Tensor in Different Way
# Impost TensorFlow
import tensorflow as tf
# TensorFlow Version
tf.__version__
# Check GPU availability
tf.test.is_gpu_available()
# Create Integer Constant
a = tf.constant(10)
a
# Create Float Constant
b = tf.constant(10.2)
b
# Create String Constant
c = tf.constant("Indian Ai Production")
c
# Creat Bool Constant
d = tf.constant(True)
d
# Creat Constant Numpy array / List / Tuple
import numpy as np
np_array = tf.constant(np.array([1,2,3,4]))
np_array
# Create 1D constant
t_1d = tf.constant([1,2,3,4])
t_1d
# Create 2D constant
t_2d = tf.constant([[1,2],[3,4]])
t_2d
t_2d_1 = tf.constant([1,2,3,4], shape=(2,2), dtype="float32")
t_2d_1
# Create N-D constant
t_3d_1 = tf.constant([[[1,2],[2,3],[4,5]]], dtype="float32")
t_3d_1
# Type of Constant
type(t_3d_1)
# Shape of Constant
t_3d_1.shape
t_2d.shape
# Access constant value
t_1d.numpy()
t_1d.numpy()[2]
# constant dtype
t_3d_1.dtype
# wrong synstax
tf.constant([1,23.4,"Indian"])
Reference: https://www.tensorflow.org/api_docs/python/tf/constant