What is a tensor?
A tensor is a generalization of vectors and matrices. It is easily understood as a multidimensional array. For example, in machine learning, we can organize data in an m-way array and refer it as a data tensor. Data related to images, sounds, movies, etc, can be represented using tensors. We can thus easily analyze the relationships among words or concepts using tensors. Tensors are often used in artificial neural networks.
How to create a tensor using Python?
We can create a tensor using numpy nd-array. For example, we can use the following Python code to create a tensor that has 3 elements across each axis.
import numpy A = numpy.random.randint(low=1, high=10, size=(3, 3, 3)) print(A)
Here, we are using the numpy.random.randint() function to generate random integers within the range [low, high). And the size argument specifies the shape of the tensor. Here, size=(3, 3, 3) specifies that the created tensor will have 3 elements across each axis.
The output of the given program will be like the following:
[[[6 3 6] [4 4 1] [7 4 9]] [[1 6 2] [3 7 4] [2 1 5]] [[9 2 1] [3 3 6] [4 7 3]]]






0 Comments