Python: How to create tensors with known values

In this tutorial, we are going to discuss creating tensors with known values using Python. So, at first, we are gonna know about tensors.
In different programs, we declare the variables but for working using TensorFlow we use tensors which is a multidimensional array that can contain strings, Boolean and numbers.

We can create tensors in 6 different ways: –

  1. By using constant() function
  2. By using zeros() function
  3. By using ones() function
  4. By using linspace() function
  5. By the use of range() function
  6. By using fill() function

Installing and Importing Tensorflow Python

Open your anaconda prompt then type: –

pip install tensorflow

This will install the latest version of TensorFlow in your machine

Now we import the TensorFlow library : –

import tensorflow as tensorflow #You can name it anything

For more on installation see: –

Install tensorflow

Creating Tensor Using Constant() Function

This function is one of the most commonly used function to create tensors in Python it returns the value given by the user the common syntax of this function is: –
tensorflow.constant(value, dtype, shape, name)
where value is the array that we enter, dtype is the datatype that is default to None and is not necessary to write, shape is the shape of the tensors that we are entering it is not necessary to enter it and finally the name is the name of the tensor.

CODE: –

tensor1=tensorflow.constant([1,2,3]) #create 1-D tensor with 3 integer values
tensor2=tensorflow.constant(['bob','sam','john']) #create 1-D tensor with 3 string values
tensor3=tensorflow.constant([[1,2,3],[4,5,6]]) #create a 2-D tensor of shape(2,3) having integer values
tensor4=tensorflow.constant([1.3,2.3,4.3],tensorflow.float32,shape=[3]) #create a 1-D tensor with data type as float and shape of 3

print(tensor1)
print(tensor2)
print(tensor3)
print(tensor4) 

Code Output: –

tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor([b'bob' b'sam' b'john'], shape=(3,), dtype=string)
tf.Tensor(
[[1 2 3]
 [4 5 6]], shape=(2, 3), dtype=int32)
tf.Tensor([1.3 2.3 4.3], shape=(3,), dtype=float32)

 

Creating Tensor Using Zeros() Function

This function returns the tensor containing all values set zero. Its common syntax is: –
tensorflow.zeros(shape, dtype, name)
It’s datatype by default is float32.

CODE: –

tensor5=tensorflow.zeros([5,6]) # creates a 2-D tensor with shape of (5,6) 
tensor6=tensorflow.zeros([5],tensorflow.int64) # creates a 1-D tensor with shape (5,) and datatype int64

print(tensor5)
print(tensor6)

Code Output: –

tf.Tensor(
[[0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]], shape=(5, 6), dtype=float32)
tf.Tensor([0 0 0 0 0], shape=(5,), dtype=int64)

Creating Tensor Using Ones() Function

This function returns the tensor containing all values set one. Its common syntax is: –
tensorflow.ones(shape, dtype, name)
It’s datatype by default is float32.

CODE: –

tensor7=tensorflow.ones([5,6]) # creates a 2-D tensor with shape of (5,6) 
tensor8=tensorflow.ones([5],tensorflow.int64) # creates a 1-D tensor with shape (5,) and datatype int64 

print(tensor7) 
print(tensor8)

Code Output:

tf.Tensor(
[[1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]], shape=(5, 6), dtype=float32)
tf.Tensor([1 1 1 1 1], shape=(5,), dtype=int64)

Creating Tensor Using Linspace() Function

This function returns a linearly spaced tensors in python when the start end and the number of points are given. In this the tensor has the same data type as that of the starting point. It’s common syntax is: –
tensorflow.linspace(start, end, num, name)
Where start is the number we want to start with it is inclusive, end is the end of the range whereas the num is the number of values in the tensor from start to end both inclusive.

CODE: –

tensor9=tensorflow.linspace(12.0,14.0,5)
print(tensor9)
# tenerr=tensorflow.linspace(12,14,4)

Note – tenerr will show error as the starting point is integer and there are no 4 numbers that are linearly spaced between 12 and 14 that are integers

Code Output: –

tf.Tensor([12.  12.5 13.  13.5 14. ], shape=(5,), dtype=float32)

Creating Tensor Using Range() Function

This function returns a 1-Dimensional tensor with a range of values depending on the value of delta we can also call it delta spaced as the spacing between subsequent tensor values depend upon the delta value. It’s common syntax is: –
tensorflow.range(start, limit, delta, dtype, name)
Where start is the starting value that is not essential to add it’s default value is 0 if not initialized, the tensor extends till limit but it is not inclusive and delta is the increment or decrements.

CODE: –

tensor10=tensorflow.range(4,10,delta=1.5)
tensor11=tensorflow.range(10,delta=1.5) #In this we did not include start so it start from 0
tensor12=tensorflow.range(10,4,delta=-1.5) #This is the decrement range tensor

print(tensor10)
print(tensor11)
print(tensor12)

Code Output: –

tf.Tensor([4.  5.5 7.  8.5], shape=(4,), dtype=float32)
tf.Tensor([0.  1.5 3.  4.5 6.  7.5 9. ], shape=(7,), dtype=float32)
tf.Tensor([10.   8.5  7.   5.5], shape=(4,), dtype=float32)

Creating Tensor Using Fill() Function

This function fills the tensor with the same value that is given according to the shape given by the user. It has the same datatype as that of the value. It’s common syntax is: –
tensorflow.fill(dims, value, name)
Where dims is the dimension of the tensor and value is the value that we want the tensor to have this can be a string or a number.

CODE: –

tensor13=tensorflow.fill([3,4],5)
tensor14=tensorflow.fill([3,4],'adi') 

print(tensor13)
print(tensor14)

Code Output: –

tf.Tensor(
[[5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]], shape=(3, 4), dtype=int32)
tf.Tensor(
[[b'adi' b'adi' b'adi' b'adi']
 [b'adi' b'adi' b'adi' b'adi']
 [b'adi' b'adi' b'adi' b'adi']], shape=(3, 4), dtype=string)

So, these are the different ways by which we can create tensors with known values.

Also read: –

Basics Of Tensorflow

One response to “Python: How to create tensors with known values”

  1. Aman says:

    Thanks, the blog was very helpful, and also very easy to understand.

Leave a Reply

Your email address will not be published. Required fields are marked *