Table Of Contents
Matrix Multiplication
Multiplying 2 matrixes.
Given 2 matrixes:
ma = [
[1,2,3],
[4,5,6]
]
mb = [
[7,8],
[9,10],
[11,12]
]
These get multiplied to create a new matrix:
# top row of first matrix multiplication results
58 = (1*7 + 2*9 + 3*11)
64 = (1*8 + 2*10 + 3*12)
# bottom row of first matrix multiplication results
139 = (4*7 + 5*9 + 6*11)
154 = (4*8 + 5*10 + 6*12)
# resulting matrix
[
[58,64],
[139, 154]
]
RULES
- the "inner" dimensions must match between the two matrixes
- a 3x2 can multiply with a 2x4 (3x2, 2x3, the "inners" are 2)
- a 2x4 can multiply by a 4x3 (2x4, 4x3, the "inners" are 4)
- a 3x2 CANNOT multiply by 3x3
In [43]:
import tensorflow as tfWith Tensorflow
In [44]:
ma = [
[1,2,3],
[4,5,6]
]
mb = [
[7,8],
[9,10],
[11,12]
]
t1 = tf.constant(ma)
t1Out [44]:
In [45]:
t2 = tf.constant(mb)
t2Out [45]:
In [46]:
tf.matmul(ma,mb)Out [46]:
Reshaping tensor shapes in order to multiply
In [47]:
maA = [
[1,2,3],
[2,3,4],
[3,4,5],
[4,5,6]
]
maB = [
[2,3],
[3,4],
[4,5],
[5,6],
[6,7],
[7,8]
]
tensorA = tf.constant(maA)
tensorB = tf.constant(maB)
tensorA, tensorBOut [47]:
In [48]:
# will throw an error:
tf.matmul(tensorA, tensorB)In [ ]:
tensorA.shape, tensorB.shapeIn [ ]:
reshapedB = tf.reshape(tensorB, shape=(3,4))
reshapedBIn [ ]:
# NOW it should multiply
tf.matmul(tensorA, reshapedB)Transposing Tensors
In [ ]:
tensorB, reshapedBIn [ ]:
transposedB = tf.transpose(tensorB)
transposedBThe Dot Product
Matrix multiplication is also known as the dot product.
tf.tensordot can be used to multiply tensors as well...
In [ ]:
smallA = [
[1,2],
[3,4],
[5,6]
]
smallB = [
[7,8],
[9,10],
[11,12]
]
smallATensor = tf.constant(smallA)
smallBTensor = tf.constant(smallB)
smallATensor, smallBTensorIn [ ]:
#
# Transpose smallA
#
transposedSmallA = tf.transpose(smallATensor)
#
# Transpose smallB
#
transposedSmallB = tf.transpose(smallBTensor)
#
# Reshape smallA
#
reshapedSmallA = tf.reshape(smallATensor, shape=(2,3))
#
# Reshape smallB
#
reshapedSmallB = tf.reshape(smallBTensor, shape=(2,3))
print('----INSPECTING RESULTS----')
print('of tf.transpose && tf.reshape')
print('----- -----')
print(f'smallA: {smallA}')
print(f'transposedSmallA: {transposedSmallA}')
print(f'reshapedSmallA: {reshapedSmallA}')In [ ]:
#
# Multiply Matrixes: tensordot & transposed
#
tf.tensordot(transposedSmallA, smallBTensor, axes=1)In [ ]:
#
# Multiply Matrixes: matmul & reshaped
#
tf.matmul(smallATensor, reshapedSmallB)