• Skip to content
  • Skip to secondary menu
  • Skip to primary sidebar

PROGRAMMING REVIEW

Slow catching the programming trends

  • [ Home ]
  • [ NPM quiz ]
  • [ WPCli quiz ]
  • [ Sass Quiz ]
  • [ Git Quiz ]
  • [Plugins check]
  • [ WordPress Blog ]
  • Word of caution

Tensor in NumPy and what may be useful to know about tensor shapes

December 6, 2018 ⌛ Tagged With: math, operations, python, tensors

NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object;

import numpy as np
    T = np.array([
  [[1,2,3],    [4,5,6],    [7,8,9]],
  [[11,12,13], [14,15,16], [17,18,19]],
  [[21,22,23], [24,25,26], [27,28,29]],
])
print(T.shape) # (3,3,3)
print(T.ndim) # 3
print(T.size) # 27
print(T)

Each array has attributes ndim (the number of dimensions), shape (the size of each dimension), and size (the total size of the array):

When you need to randomly set the tensor with some default values you may use this:

import numpy as np
np.random.seed(0)  # we used seed to reproduce this again

# x1, x2, x3 are tensors
x1 = np.random.randint(10, size=3)  # 1D
x2 = np.random.randint(10, size=(3, 4))  # 2D
x3 = np.random.randint(10, size=(3, 4, 5))  # 3D

Operation we can do on tensors is dot product:

c = np.tensordot(x1, x2, axes=0)
print(c)

For tensor addition and tensor subtraction operations the tensors need to have the same shape. These are element wise operations.

import numpy as np
np.random.seed(0) 
x21 = np.random.randint(10, size=(3, 4))  # 2D
x22 = np.random.randint(10, size=(3, 4))  # 2D
c = x21 + x22

This would not work because the tensors need to be with the same shape

x21 = np.random.randint(10, size=(4, 4))  # 2D
    x22 = np.random.randint(10, size=(3, 4))  # 2D
c = x21 + x22

The following error ValueError: operands could not be broadcast together with shapes (4,4) (3,4) would occure.

The same shape principle need to be also for the element vise multiplication and element vise division.

Related posts:

  1. Experiment in TensorFlow
  2. Perl shift function
  3. PHP creating variables from strings and associative arrays
  4. Working with beautifulsoup in Python should be easy
  5. Hello world in ai
  6. Perl 6 new syntax
  7. Post and Get requests in Python
  8. How to get python object type?

Tagged With: math, operations, python, tensors

Previous Post: « Post and Get requests in Python
Next Post: Hello world in ai »

Primary Sidebar

My Q&A site profile

profile for prosti on Stack Exchange, a network of free, community-driven Q&A sites

WordPress related stuff

[ WordPress Blog ]
[ WordPress plugins check ]

More profiles

Linked In
GitHub
GitHub blog
Android Quiz apps

Tags

.htaccess android atom babel cdn centos command line csharp css database debug DreamWeaver eclipse ecommerce es5 es6 git google html javascript js Laravel linux metaknowledge mysql nginx node.js npm online-tools perl php python react reactjs scripting security servers sql server system admin tips and tricks tools ubuntu vim web standards youtube

PROGRAMMING REVIEW

☂ since 2010