-------------------------------------------------------------------------------------------------
# Broadcasting - enlarging a tensor to match another one, but repeating data.
###############################################################################
import numpy as np
x1 = np.array([1,2,3])
print('x1: '); print (x1)
print('x1.ndim: ', x1.ndim)
print('x1.shape: ', x1.shape)
x2 = np.array([[1,2,3],[4,5,6]])
print('x2: ') ; print (x2)
print('x2.ndim: ', x2.ndim)
print('x2.shape: ', x2.shape)
x3 = x1 + x2
print('x3: '); print(x3)
print('x3.ndim: ', x3.ndim)
print('x3.shape: ', x3.shape)
# Vector Dot Product
###############################################################################
v1 = np.array([1,2,3])
v2 = np.array([1,2,3])
v3 = v1.dot(v2)
print('v3: ', v3)
# Matrix - Vector Multiplication
###############################################################################
m1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
v1 = np.array([10,11,12])
v2 = m1.dot(v1)
print('v2: ', v2)
# Tensor Multiplication
###############################################################################
t1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
t2 = np.array([[10,11,12],[13,14,15],[16,17,18]])
t3 = t1.dot(t2)
print('t3: '); print(t3)
# Reshaping Tensors
###############################################################################
print("t3.shape: ", t3.shape)
t4 = t3.reshape(1,9)
print("t4"); print(t4)
print("t4.shape: ", t4.shape)
t5 = t4.reshape(9,)
print("t5"); print(t5)
print("t5.shape: ", t5.shape)
t6 = t4.reshape(9,1)
print("t6"); print(t6)
print("t6.shape: ", t6.shape)
-------------------------------------------------------------------------------
-- Output
-------------------------------------------------------------------------------
1:
[1 2 3]
x1.ndim: 1
x1.shape: (3,)
x2:
[[1 2 3]
[4 5 6]]
x2.ndim: 2
x2.shape: (2, 3)
x3:
[[2 4 6]
[5 7 9]]
x3.ndim: 2
x3.shape: (2, 3)
v3: 14
v2: [ 68 167 266]
t3:
[[ 84 90 96]
[201 216 231]
[318 342 366]]
t3.shape: (3, 3)
t4
[[ 84 90 96 201 216 231 318 342 366]]
t4.shape: (1, 9)
t5
[ 84 90 96 201 216 231 318 342 366]
t5.shape: (9,)
t6
[[ 84]
[ 90]
[ 96]
[201]
[216]
[231]
[318]
[342]
[366]]
t6.shape: (9, 1)
No comments:
Post a Comment