Tensorflow is an open-source machine learning framework that is used for complex numerical computation. It was developed by the Google Brain team in Google. Tensorflow can train and run deep neural networks that can be used to develop several AI applications.
What is a Tensor?
A tensor can be described as a n-dimensional numerical array. A tensor can be called a generalized matrix. It could be a 0-D matrix (a single number), 1-D matrix (a vector), 2-D matrix or any higher dimensional structure. A tensor is identified by three parameters viz., rank, shape and size. The number of dimensions of the tensor is said to be its rank. The number of columns and rows that the tensor has, is said to be its shape. And, the data type assigned to the tensor’s elements is said to be its type.
Importance of Tensor in Tensorflow:
A tensor can be called as the central data type of Tensorflow. It is because tensors are the fundamental components of computation inside the Tensorflow framework. As the name suggests, Tensorflow is a framework that involves defining and running computations involving tensors.
Let’s discuss all the different ways to create tensors in Tensorflow.
Method #1: Creating tensor using the
The most popular function for creating tensors in Tensorflow is the
constant() function. We need to give values or list of values as argument for creating tensor. If the values given are of type integer, then int32 is the default data type. And if the values given are of floating type, then
float32 is the default data type.
Here, t1 is a one-dimensional tensor and has three integer values of datatype int32. t2 is a two-dimensional tensor and it contains floating-point values of datatype float32. t3 is a three-dimensional tensor and it contains integer values of datatype int32. t4 is a one-dimensional having three strings of datatype string. In t5, we have given it a name ‘N3’, specified the datatype as ‘float16’ and given the value of shape as 3. Since the shape value of t5 is 3, we cannot pass more than 3 elements in it. Otherwise, it will throw an error.
Method #2: Creating tensor using the
zeros() function is used to create tensor with all of its elements as zero. The
shape of the tensor is the only required argument.
Method #3: Creating tensor using the “ones()” function.
ones() function basically does the same thing as the
zeros() function, but the elements are one in this case instead of zero.
Method #4: Creating tensor using the
fill() function is used to create tensors with all the elements in the tensor having the same value. The value of the element is to be passed as an argument and the datatype depends upon that value passed.
Method #5: Creating tensors using the
linspace() function is used in creating tensors in which we specify the starting value, ending value and the number of elements as arguments and the elements of the tensor changes its values based on the arguments.
Method #6: Creating tensors using the
range() function is almost the same as the linspace() function. The only difference is that in the range() function, we specify a value called
delta, that is used to calculate the successive elements of the tensor, instead of the number of elements in the
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- TensorFlow - How to stack a list of rank-R tensors into one rank-(R+1) tensor in parallel
- Pycairo - Creating different shapes
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Creating and updating PowerPoint Presentations in Python using python - pptx
- Python - Model Deployment Using TensorFlow Serving
- PyQt5 – Different padding size at different edge of Label
- PyQt5 - Setting different toolTip to different item of ComboBox
- Creating child process using fork() in Python
- Creating a Basic hardcoded ChatBot using Python-NLTK
- Creating NotePad using PyQt5 - Python
- Creating Files in HDFS using Python Snakebite
- Creating a radar sweep animation using arcade in Python
- Creating payment receipts using Python
- Linear Regression Using Tensorflow
- ML | Logistic Regression using Tensorflow
- Implementing Deep Q-Learning using Tensorflow
- ML | Training Image Classifier using Tensorflow Object Detection API
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.