Buscar

Tensor Explained with Python Numpy

Prévia do material em texto

20/07/2021 Tensor Explained with Python Numpy Examples - Data Analytics
https://vitalflux.com/tensor-explained-with-python-numpy-examples/ 1/7
Data Analytics
Data Science, Machine Learning, AI
Home
Interview Questions
Data Science
Machine Learning »
Python
Select a page
Tensor Explained with Python Numpy Examples
September 14, 2020 by Ajitesh Kumar · 1 Comment
In this post, you will learn about the concepts of Tensor using Python Numpy examples with the help
of simple explanation. When starting to learn deep learning, you must get a good understanding of the
data structure namely tensor as it is used widely as the basic data structure in frameworks such as
tensorflow, PyTorch, Keras etc.
Simply speaking, Tensor is a container of data. The data can be numerical or character. However,
mostly, tensors hold numbers or numerical data. Tensors can be represented as an array data
structure. In this post, you will learn about how to express tensor as 1D, 2D, 3D Numpy array. Before
going ahead and see the examples of 1D, 2D and 3D tensors, lets understand the key features of tensors: 
Rank or Number of Axes: Number of axes of a tensor is also termed as the rank of the tensor.
Simply speaking, the axes or rank of the tensor represented as an array is number of indices that
will be required to access a specific value in the multi-dimensional array aka tensor. Accessing a
specific element in a tensor is also called as tensor slicing. The first axis of the tensor is also called
as a sample axis.
Shape: Tuple of integers representing the dimensions that the tensor have along each axes.
Before getting into the details, lets look at the diagram given below which represents 0D, 1D, 2D and 3D
tensors. 0D tensor is a scalar data or simply a number.
Subscribe
https://vitalflux.com/
https://vitalflux.com/
https://vitalflux.com/category/career/interview-questions/
https://vitalflux.com/category/data-science/
https://vitalflux.com/category/machine-learning/
https://vitalflux.com/category/python/
https://vitalflux.com/tensor-explained-with-python-numpy-examples/
https://vitalflux.com/author/vitalflux/
https://vitalflux.com/category/deep-learning
https://vitalflux.com/tag/pytorch
https://vitalflux.com/tag/keras
https://en.wikipedia.org/wiki/Tensor
20/07/2021 Tensor Explained with Python Numpy Examples - Data Analytics
https://vitalflux.com/tensor-explained-with-python-numpy-examples/ 2/7
Fig 1. Pictorial representation of a Tensor
Here are some examples of 2D, 3D, 4D and 5D tensors:
Vector data— 2D tensors of shape (samples, features)
Timeseries data or sequence data— 3D tensors of shape (samples, timesteps, features)
Images— 4D tensors of shape (samples, height, width, channels)
Video— 5D tensors of shape (samples, frames, height, width, channels)
1D Tensor (Vector) Example
A one-dimensional array also called as vector can be termed as a 1D-tensor. 1D tensor represents the
tensor of rank/axes as 1. Take an example of an array representing the tensor using the following 1D
Numpy array:
In the above example, the axes or rank of the tensor x is 1. The axes of the tensor can be printed using
ndim command invoked on Numpy array. In order to access elements such as 56, 183 and 1, all one
needs to do is use x[0], x[1], x[2] respectively. Note that just one indices is used. Printing x.ndim, x.shape
will print the following: (1, (3,)). This represents the fact that the tensor x has the axes or rank has 1 and
there are three dimension on that axes.
2D Tensor (Matrix) Example
1
2
3
4
5
6
7
8
9
#
# Tensor having rank / axes = 1 and dimension along the axis as 3
#
x = np.array([56, 183, 1])
#
# Dimension = 1
# Shape = 1,
#
x.ndim, x.shape
Subscribe
EU QUERO
-30% -18% -20% -3
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=FMBZbs2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RMKL_zaGDDKv6810c52gqec7cGPXUBRy742nCbeOvDTOrOIak95DHbcNk1oeNKSe0CJ7Tg3buokhXBwpmFD0M9k8ujgIAQmodg1pQMEuBJqVafa9vFM6uFcwBWJRs8sUYiVIqzpOa3pzmiXozo2P6F90O5XOuzY7_2z0CMuqgxl81ZyJ3P5IalEi4_Lyiu_q_OBxIvwOwwcdKbf1LN-I8iJ9kOWbnmtDPTVpR4bzP2AJ1OAJ57W4ZF-N9LO-l3hU3h0kC6xRCMN5KvaFh0iJL8UzbHdCZcgCdvvYGbn8ZjQsqA5RmVIZ-XsrCBvvm4d--o&maxdest=https%3A%2F%2Fwww.abracasa.com.br%3Futm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=DtxeKc2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RPzDgUBgu0z7bnU8jxly4jB1D4SeEmGRc2O03RdbyWx0N7CmkWrP7uH3JwAWslTyvm8apMW7vaR8BGONKtzQMgn9UTXzmz6D77kIzd5k5URtwItn5ccTdDASWDzl8t6cXaxFUv53OIVHHlvzpFYJfZFdcRnW4RO8ZLWAPTF1bZVSIhYJkWWek-2ZYK0zzloIFKP_uvTg3PTad4d2fdMAA_bmM81q8xFRMZDvDVd6V-DUIM1wwrwvOXAGLZTnqaTlAzkAgDHUTVN4FlQLh38UFUC9w6xY8bPXbF5G3OnstZJe6cXy9tahOYtEDGgtY_JgzwJcCixSwpCEFl1NPsv8gLdqsDqxuwartUFlGaD-yLh9Q&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-madrid-cromada-alta-giratoria-preta-or-3301-alta%2Fp%3Fidsku%3D2000237%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=jqnKu82gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RP3KvrgxEx-OQm4xXD_xm6dtQQOdVLwBlt6QQzLLRlQC3Kp7fo_3eghKep3aMipo0ZuAhlAYg3q0Ss-R--TORzxVGpTtgxPMkghK9m15AY0gH9nGkzLXigFonWoLMxxdD5DWgUQu4l-56wnRBzZS8ycG1IryOffnV-wcNJ5K68qv4Bwpbu0Os5yIbe960khz4IIBEKVf9vr9g1tEBos2AQOxQGSdtJgFPi25iQJvm0oFhoP4AyMKdP5-eENGX2vybGPtFfi1-m0P6PtKnbvtgvK5PlbNYJXfcmO1RSzBxV5DZr0Ddjcvwgx6GM64OtaISotyS7KgNxgAktMMDhgMQyxT1_ev7OpjNq1GPh2pfVRcg&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-noruega-alta-giratoria-preta-or-3303-alta%2Fp%3Fidsku%3D2003539%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=3_nRvc2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5ROKG2o9mZ_ujK2p8WO60Ubytw_9-QqmPVOJyEON6G2Yw8ANI_JQV0QzMvvnsCjgkWIUWBbyWY4eRIHMq6twSzARzn3w8JoFuvr_66rBp0zinA3TXQ78LC91TWnO9pJD8-cOB8PMu1pkWTlV_foCCwN9ZSZRY05y0TTYPt5Al6YzCc0YEC2bnAwhsxRbs2_a2XdrPDlPcB6lvMN4pbw96jZ-T7SMWOo2-ELLQBpVTziRwrhLiMGsqkYtSVl_AMLWFB8asU0pjkGQi-n_cvAEc6zatQbRXIi0MxZP-yaXlZbGqHc2cwQdfH5HNxCJNciT92AYueIe3rizcFJGo32k37nRSzelnI5-BRRN9BQrJjESkg&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fkit-home-office-escrivaninha-vintage-louro-freijo-com-gavetas-brancas-poltrona-office-franca%2Fp%3Fidsku%3D2007406%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=J-sBos2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RNrwiuydazN0gbBW6eKDqlyVMpTVV6PtmWGszLYLMYj_XQE4JaHeRBFJSWF5qjDBwV0QFSyS7hKajjIluhuzjo5G4Z7Iwtv4mg7EDpcRT7XB8c19svmfl6o3dyyMdjS9753Ziw09kaJ6AGBqhPQMgWltdtNcViYxhmzZZYW8J40nhG4HQi1kGG7V2zd_StLMH1AwJf6v3SdPdmi6oS9mIVXGbBHedhzLHt6RhByzwlgIcAEBBQ1b-0XYzy56KIxIj_j-Q0PE4qNKknJP5Vk4SZEvBy_ka7vZgIJ1R_ukAe3uBngA7lari-9k1kiPnq0DzG3MBOWcDKD6OgdgGRNHgtJspmzVejNIAPJFaQWT2MYlQ&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-madrid-cromada-giratoria-caramelo-or-3301-baixa%2Fp%3Fidsku%3D2002834%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=igtaiM2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5ROkEWvLcmCx3dB8cbaAvLynrlE424CS5yw213JpQ8xqeEBqABwZjohQ9UHXYAjpi2bAXTZ2G-P-N8gQHNH94G568PvLaxWmcqUznyI6JLOpHkvaLYGQ5owFGfDrPRszaHLw6MoHvL7-pCUq398Zm-oXWk5GB53MRjzSKutX7JyezTuZFzvjJ9pvruOkl1cVQw75KW_JOuSFafdZ7qRxhIJyPKDjGPcCcLR0-ZnoxF3GdDSFUPP1zYiObzKiWqJo3kqpvaTCbwk3X6lOBgwCbRnM5QHYdfPGmO8MID_1Li2qHBE8sWD1wPoo3pBWnP11xd0I65lH6JmPewWe_JGDaNbtem7-RDIOEM7XhAGYoyKg7w&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fpoltrona-costela-com-puff-cafe-com-leite%2Fp%3Fidsku%3D2007195%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel20/07/2021 Tensor Explained with Python Numpy Examples - Data Analytics
https://vitalflux.com/tensor-explained-with-python-numpy-examples/ 3/7
A two-dimensional array also called as matrix can be termed as a 2D-tensor. 2D tensor represents the 2-
dimensional array which is nothing but a matrix. As like matrices, the two-dimensional array can be
represented as a set of rows and columns. Here is the code for 2D numpy array representing 2D tensor:
Printing x.ndim, x.shape will print the following: (2, (3,3)). This represents the fact that the tensor x has
the axes or rank has 2 and there are three dimension on each of the axes. n order to access elements such
as 56, 178 and 50, one would require to use the following respectively: x[0][0], x[1][1], x[2][0]. Note that
you will need two indices to access a particular number.
3D Tensor Example
A three-dimensional array can be termed as a 3D-Tensor. Here is the code representing the 3D tensor:
Printing x.ndim, x.shape will print the following: (2, (3,3)). This represents the fact that the tensor x has
the axes or rank has 2 and there are three dimension on each of the axes. n order to access elements such
as 56, 178 and 50, one would require to use the following respectively: x[0][0], x[1][1], x[2][0]. Note that
you will need two indices to access a particular number.
Understanding Tensor using MNIST Image Example
Here is an explanation of tensor axes / rank and shape using Keras and MNIST dataset.
1
2
3
4
5
6
7
8
9
10
11
#
# Tensor having rank / axes = 2 and dimension along each axis as 3
#
x = np.array([[56, 183, 1],
 [78, 178, 2],
 [50, 165, 0]])
#
# Dimension = 2
# Shape = 3, 3
#
x.ndim, x.shape
1
2
3
4
5
6
7
8
9
10
11
12
#
# Tensor having rank / axes = 3 and dimension along the axis as 3
#
x = np.array([[[56, 183, 1],
 [65, 164, 0]],
 [[85, 176, 1],
 [44, 164, 0]]])
#
# Dimension = 1
# Shape = 1,
#
x.ndim, x.shape
1
2
3
4
5
6
7
8
9
from keras.dataset import mnist
#
# Load the training and test images MNIST Dataset
#
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
#
# Accessing the tensor rank / axes and shape
#
print(train_images.ndim, train_images.shape)
Subscribe
EU QUERO
-30% -18% -20% -3
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=FMBZbs2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RMKL_zaGDDKv6810c52gqec7cGPXUBRy742nCbeOvDTOrOIak95DHbcNk1oeNKSe0CJ7Tg3buokhXBwpmFD0M9k8ujgIAQmodg1pQMEuBJqVafa9vFM6uFcwBWJRs8sUYiVIqzpOa3pzmiXozo2P6F90O5XOuzY7_2z0CMuqgxl81ZyJ3P5IalEi4_Lyiu_q_OBxIvwOwwcdKbf1LN-I8iJ9kOWbnmtDPTVpR4bzP2AJ1OAJ57W4ZF-N9LO-l3hU3h0kC6xRCMN5KvaFh0iJL8UzbHdCZcgCdvvYGbn8ZjQsqA5RmVIZ-XsrCBvvm4d--o&maxdest=https%3A%2F%2Fwww.abracasa.com.br%3Futm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=DtxeKc2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RPzDgUBgu0z7bnU8jxly4jB1D4SeEmGRc2O03RdbyWx0N7CmkWrP7uH3JwAWslTyvm8apMW7vaR8BGONKtzQMgn9UTXzmz6D77kIzd5k5URtwItn5ccTdDASWDzl8t6cXaxFUv53OIVHHlvzpFYJfZFdcRnW4RO8ZLWAPTF1bZVSIhYJkWWek-2ZYK0zzloIFKP_uvTg3PTad4d2fdMAA_bmM81q8xFRMZDvDVd6V-DUIM1wwrwvOXAGLZTnqaTlAzkAgDHUTVN4FlQLh38UFUC9w6xY8bPXbF5G3OnstZJe6cXy9tahOYtEDGgtY_JgzwJcCixSwpCEFl1NPsv8gLdqsDqxuwartUFlGaD-yLh9Q&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-madrid-cromada-alta-giratoria-preta-or-3301-alta%2Fp%3Fidsku%3D2000237%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=jqnKu82gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RP3KvrgxEx-OQm4xXD_xm6dtQQOdVLwBlt6QQzLLRlQC3Kp7fo_3eghKep3aMipo0ZuAhlAYg3q0Ss-R--TORzxVGpTtgxPMkghK9m15AY0gH9nGkzLXigFonWoLMxxdD5DWgUQu4l-56wnRBzZS8ycG1IryOffnV-wcNJ5K68qv4Bwpbu0Os5yIbe960khz4IIBEKVf9vr9g1tEBos2AQOxQGSdtJgFPi25iQJvm0oFhoP4AyMKdP5-eENGX2vybGPtFfi1-m0P6PtKnbvtgvK5PlbNYJXfcmO1RSzBxV5DZr0Ddjcvwgx6GM64OtaISotyS7KgNxgAktMMDhgMQyxT1_ev7OpjNq1GPh2pfVRcg&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-noruega-alta-giratoria-preta-or-3303-alta%2Fp%3Fidsku%3D2003539%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=3_nRvc2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5ROKG2o9mZ_ujK2p8WO60Ubytw_9-QqmPVOJyEON6G2Yw8ANI_JQV0QzMvvnsCjgkWIUWBbyWY4eRIHMq6twSzARzn3w8JoFuvr_66rBp0zinA3TXQ78LC91TWnO9pJD8-cOB8PMu1pkWTlV_foCCwN9ZSZRY05y0TTYPt5Al6YzCc0YEC2bnAwhsxRbs2_a2XdrPDlPcB6lvMN4pbw96jZ-T7SMWOo2-ELLQBpVTziRwrhLiMGsqkYtSVl_AMLWFB8asU0pjkGQi-n_cvAEc6zatQbRXIi0MxZP-yaXlZbGqHc2cwQdfH5HNxCJNciT92AYueIe3rizcFJGo32k37nRSzelnI5-BRRN9BQrJjESkg&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fkit-home-office-escrivaninha-vintage-louro-freijo-com-gavetas-brancas-poltrona-office-franca%2Fp%3Fidsku%3D2007406%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=J-sBos2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RNrwiuydazN0gbBW6eKDqlyVMpTVV6PtmWGszLYLMYj_XQE4JaHeRBFJSWF5qjDBwV0QFSyS7hKajjIluhuzjo5G4Z7Iwtv4mg7EDpcRT7XB8c19svmfl6o3dyyMdjS9753Ziw09kaJ6AGBqhPQMgWltdtNcViYxhmzZZYW8J40nhG4HQi1kGG7V2zd_StLMH1AwJf6v3SdPdmi6oS9mIVXGbBHedhzLHt6RhByzwlgIcAEBBQ1b-0XYzy56KIxIj_j-Q0PE4qNKknJP5Vk4SZEvBy_ka7vZgIJ1R_ukAe3uBngA7lari-9k1kiPnq0DzG3MBOWcDKD6OgdgGRNHgtJspmzVejNIAPJFaQWT2MYlQ&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-madrid-cromada-giratoria-caramelo-or-3301-baixa%2Fp%3Fidsku%3D2002834%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=igtaiM2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5ROkEWvLcmCx3dB8cbaAvLynrlE424CS5yw213JpQ8xqeEBqABwZjohQ9UHXYAjpi2bAXTZ2G-P-N8gQHNH94G568PvLaxWmcqUznyI6JLOpHkvaLYGQ5owFGfDrPRszaHLw6MoHvL7-pCUq398Zm-oXWk5GB53MRjzSKutX7JyezTuZFzvjJ9pvruOkl1cVQw75KW_JOuSFafdZ7qRxhIJyPKDjGPcCcLR0-ZnoxF3GdDSFUPP1zYiObzKiWqJo3kqpvaTCbwk3X6lOBgwCbRnM5QHYdfPGmO8MID_1Li2qHBE8sWD1wPoo3pBWnP11xd0I65lH6JmPewWe_JGDaNbtem7-RDIOEM7XhAGYoyKg7w&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fpoltrona-costela-com-puff-cafe-com-leite%2Fp%3Fidsku%3D2007195%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
20/07/2021 Tensor Explained with Python Numpy Examples - Data Analytics
https://vitalflux.com/tensor-explained-with-python-numpy-examples/ 4/7
Executing the above would print the information about the tensor rank / axes and the shape – 3 (60000,
28, 28). It represents the fact that training images is stored in a 3D tensor of axes 3 and having shape
representing 60,000 matrices of 28×28 integers.
Conclusions
Here is what you learned about tensors with the help of simple Python Numpy code samples.
Tensor can be represented as a multi-dimensional array.
Numpy np.array can be used to create tensor of different dimensions such as 1D, 2D, 3D etc.
A vector is 1D tensor, a matrix is a 2D tensor. 0D tensor is a scalar or a numerical value.
Accessing a specific value of tensor is also called as tensor slicing.
Two key attributes of tensors include A. rank or axes of tensor B. Shape of the tensor
ndim and shape when invoked on Numpy array gives the axes /rank and shape of the tensor
respectively.
Posted in Data Science. Tagged with Data Science.
← Normal Distribution Explained with Python Examples
KMeans Silhouette Score Explained with Python Example →
One Response
Tensor Broadcasting Explained with Examples - Data Analytics September 17, 2020 at 8:53 am
[…] In this post, you will learn about the concepts of Tensor Broadcasting with the help of Python
Numpy examples. Recall that Tensor is defined as the container of data (primarily numerical) most
fundamental data structure used in Keras and Tensorflow. You may want to check out a related
article on Tensor – Tensor explained with Python Numpy examples. […]
Reply
Leave a Reply
Your email address will not be published. Required fields are marked *
Follow me
Author Recent Posts
Ajitesh Kumar
I have been recently working in the area of Data Science and Machine
Learning / Deep Learning. In addition, I am also passionate about various different
technologies including programming languages such as Java/JEE, Javascript, Python, R,
Julia etc and technologies such as Blockchain, mobile computing, cloud-native technologies,
application security, cloud computing platforms, big data etc. I would love to connect with you
on Linkedin.
Subscribe
EU QUERO
-30% -18% -20% -3
https://vitalflux.com/category/data-science/
https://vitalflux.com/tag/data-science/
https://vitalflux.com/normal-distribution-explained-python-examples/
https://vitalflux.com/kmeans-silhouette-score-explained-with-python-example/
https://vitalflux.com/tensor-broadcasting-explained-with-examples/
http://www.vitalflux.com/author/vitalflux
http://www.vitalflux.com/author/vitalflux
https://www.linkedin.com/in/ajitesh/
http://facebook.com/vitalflux
http://twitter.com/vitalflux
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=FMBZbs2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RMKL_zaGDDKv6810c52gqec7cGPXUBRy742nCbeOvDTOrOIak95DHbcNk1oeNKSe0CJ7Tg3buokhXBwpmFD0M9k8ujgIAQmodg1pQMEuBJqVafa9vFM6uFcwBWJRs8sUYiVIqzpOa3pzmiXozo2P6F90O5XOuzY7_2z0CMuqgxl81ZyJ3P5IalEi4_Lyiu_q_OBxIvwOwwcdKbf1LN-I8iJ9kOWbnmtDPTVpR4bzP2AJ1OAJ57W4ZF-N9LO-l3hU3h0kC6xRCMN5KvaFh0iJL8UzbHdCZcgCdvvYGbn8ZjQsqA5RmVIZ-XsrCBvvm4d--o&maxdest=https%3A%2F%2Fwww.abracasa.com.br%3Futm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=DtxeKc2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RPzDgUBgu0z7bnU8jxly4jB1D4SeEmGRc2O03RdbyWx0N7CmkWrP7uH3JwAWslTyvm8apMW7vaR8BGONKtzQMgn9UTXzmz6D77kIzd5k5URtwItn5ccTdDASWDzl8t6cXaxFUv53OIVHHlvzpFYJfZFdcRnW4RO8ZLWAPTF1bZVSIhYJkWWek-2ZYK0zzloIFKP_uvTg3PTad4d2fdMAA_bmM81q8xFRMZDvDVd6V-DUIM1wwrwvOXAGLZTnqaTlAzkAgDHUTVN4FlQLh38UFUC9w6xY8bPXbF5G3OnstZJe6cXy9tahOYtEDGgtY_JgzwJcCixSwpCEFl1NPsv8gLdqsDqxuwartUFlGaD-yLh9Q&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-madrid-cromada-alta-giratoria-preta-or-3301-alta%2Fp%3Fidsku%3D2000237%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=jqnKu82gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RP3KvrgxEx-OQm4xXD_xm6dtQQOdVLwBlt6QQzLLRlQC3Kp7fo_3eghKep3aMipo0ZuAhlAYg3q0Ss-R--TORzxVGpTtgxPMkghK9m15AY0gH9nGkzLXigFonWoLMxxdD5DWgUQu4l-56wnRBzZS8ycG1IryOffnV-wcNJ5K68qv4Bwpbu0Os5yIbe960khz4IIBEKVf9vr9g1tEBos2AQOxQGSdtJgFPi25iQJvm0oFhoP4AyMKdP5-eENGX2vybGPtFfi1-m0P6PtKnbvtgvK5PlbNYJXfcmO1RSzBxV5DZr0Ddjcvwgx6GM64OtaISotyS7KgNxgAktMMDhgMQyxT1_ev7OpjNq1GPh2pfVRcg&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-noruega-alta-giratoria-preta-or-3303-alta%2Fp%3Fidsku%3D2003539%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=3_nRvc2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5ROKG2o9mZ_ujK2p8WO60Ubytw_9-QqmPVOJyEON6G2Yw8ANI_JQV0QzMvvnsCjgkWIUWBbyWY4eRIHMq6twSzARzn3w8JoFuvr_66rBp0zinA3TXQ78LC91TWnO9pJD8-cOB8PMu1pkWTlV_foCCwN9ZSZRY05y0TTYPt5Al6YzCc0YEC2bnAwhsxRbs2_a2XdrPDlPcB6lvMN4pbw96jZ-T7SMWOo2-ELLQBpVTziRwrhLiMGsqkYtSVl_AMLWFB8asU0pjkGQi-n_cvAEc6zatQbRXIi0MxZP-yaXlZbGqHc2cwQdfH5HNxCJNciT92AYueIe3rizcFJGo32k37nRSzelnI5-BRRN9BQrJjESkg&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fkit-home-office-escrivaninha-vintage-louro-freijo-com-gavetas-brancas-poltrona-office-franca%2Fp%3Fidsku%3D2007406%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=J-sBos2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5RNrwiuydazN0gbBW6eKDqlyVMpTVV6PtmWGszLYLMYj_XQE4JaHeRBFJSWF5qjDBwV0QFSyS7hKajjIluhuzjo5G4Z7Iwtv4mg7EDpcRT7XB8c19svmfl6o3dyyMdjS9753Ziw09kaJ6AGBqhPQMgWltdtNcViYxhmzZZYW8J40nhG4HQi1kGG7V2zd_StLMH1AwJf6v3SdPdmi6oS9mIVXGbBHedhzLHt6RhByzwlgIcAEBBQ1b-0XYzy56KIxIj_j-Q0PE4qNKknJP5Vk4SZEvBy_ka7vZgIJ1R_ukAe3uBngA7lari-9k1kiPnq0DzG3MBOWcDKD6OgdgGRNHgtJspmzVejNIAPJFaQWT2MYlQ&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fcadeira-de-escritorio-madrid-cromada-giratoria-caramelo-or-3301-baixa%2Fp%3Fidsku%3D2002834%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel
https://cat.va.us.criteo.com/delivery/ck.php?cppv=3&cpp=igtaiM2gMKvEQN_6gN2RkZqn8-N3258MIjq1-aXcnZf7XObuP1Fjqpgu9fUaT6_7M9mMrLqIjIfRrVugQBNgCbMsqrY4wnXtIXnBdCcEK9GxtlhmC2ePWx91gPh_8VipsMAUBF7GdIhvZZlaK1layNRl5ROkEWvLcmCx3dB8cbaAvLynrlE424CS5yw213JpQ8xqeEBqABwZjohQ9UHXYAjpi2bAXTZ2G-P-N8gQHNH94G568PvLaxWmcqUznyI6JLOpHkvaLYGQ5owFGfDrPRszaHLw6MoHvL7-pCUq398Zm-oXWk5GB53MRjzSKutX7JyezTuZFzvjJ9pvruOkl1cVQw75KW_JOuSFafdZ7qRxhIJyPKDjGPcCcLR0-ZnoxF3GdDSFUPP1zYiObzKiWqJo3kqpvaTCbwk3X6lOBgwCbRnM5QHYdfPGmO8MID_1Li2qHBE8sWD1wPoo3pBWnP11xd0I65lH6JmPewWe_JGDaNbtem7-RDIOEM7XhAGYoyKg7w&maxdest=https%3A%2F%2Fnovaabracasa.vtexcommercestable.com.br%2Fpoltrona-costela-com-puff-cafe-com-leite%2Fp%3Fidsku%3D2007195%26utm_source%3Dcriteo%26utm_source%3Dcriteo%26utm_medium%3Dretargeting%26utm_campaign%3Dlowerfunnel

Continue navegando