Arithmetic operations of Numpy ndarrays are element-wise. If an array has addition, subtraction, multiplication or division with a scalar, each element will have the same operation and the result is also an array.
#import numpy as np
#create an array of 0 to 9
b = np.arange(10)
b
#output
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#addition of an array with a scalar
b + 4
#Output
array([ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
#subtraction
b-2
#Output
array([-2, -1, 0, 1, 2, 3, 4, 5, 6, 7])
#multiplication
b * 1.5
#Output
array([ 0. , 1.5, 3. , 4.5, 6. , 7.5, 9. , 10.5, 12. , 13.5])
#division
b / 4
#Output
array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. , 2.25])
Two Numpy arrays of same dimensions can perform similar arithmetic operations as with a scalar. The resulting array has the same dimension as the input, and each element of the resulting array is the result of arithmetic operation between the inputitng arrays.
#create another array of same dimension as b
c = np.arange(4, 14)
c
#Output
array([ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
#addition of two arrays
b + c
#Output
array([ 4, 6, 8, 10, 12, 14, 16, 18, 20, 22])
#division between two arrays
b / c
#Output
array([0. , 0.2 , 0.33333333, 0.42857143, 0.5 ,
0.55555556, 0.6 , 0.63636364, 0.66666667, 0.69230769])
Numpy array can also be applied with particular mathematic functions, like trigonometric and exponential functions, which are also element-wise.
#sine of array
b * np.sin(c)
#Output
array([-0. , -0.95892427, -0.558831 , 1.9709598 , 3.95743299,
2.06059243, -3.26412667, -6.99993145, -4.29258334, 3.78150333])
#square root of array
b * np.sqrt(c)
#Output
array([ 0. , 2.23606798, 4.89897949, 7.93725393, 11.3137085 ,
15. , 18.97366596, 23.21637353, 27.71281292, 32.44996148])
#exponential of array
b * np.exp(c)
#Output
array([0.00000000e+00, 1.48413159e+02, 8.06857587e+02, 3.28989948e+03,
1.19238319e+04, 4.05154196e+04, 1.32158795e+05, 4.19118992e+05,
1.30203833e+06, 3.98172053e+06])
To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…
In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…
If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…
According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…
Creating an Android mobile app with a deep learning AI model involves several key steps:…
Click here to go to source files for R Machine Learning