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
- Python | Tensorflow log() method
- Python | Tensorflow nn.sigmoid()
- Python | Tensorflow sin() method
- Python | Tensorflow tan() method
- Python | Tensorflow abs() method
- Python | Tensorflow exp() method
- Python | Tensorflow nn.softplus()
- Python | Tensorflow cos() method
- Python | Tensorflow nn.tanh()
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Python | Tensorflow atanh() method
- Python | Tensorflow cosh() method
- Python | Tensorflow atan() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.