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 tf

With Tensorflow

In [44]:
ma = [
    [1,2,3],
    [4,5,6]
]

mb = [
    [7,8],
    [9,10],
    [11,12]
]

t1 = tf.constant(ma)
t1
Out [44]:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>
In [45]:
t2 = tf.constant(mb)
t2
Out [45]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 7,  8],
       [ 9, 10],
       [11, 12]], dtype=int32)>
In [46]:
tf.matmul(ma,mb)
Out [46]:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[ 58,  64],
       [139, 154]], dtype=int32)>

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, tensorB
Out [47]:
(<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
 array([[1, 2, 3],
        [2, 3, 4],
        [3, 4, 5],
        [4, 5, 6]], dtype=int32)>,
 <tf.Tensor: shape=(6, 2), dtype=int32, numpy=
 array([[2, 3],
        [3, 4],
        [4, 5],
        [5, 6],
        [6, 7],
        [7, 8]], dtype=int32)>)
In [48]:
# will throw an error:
tf.matmul(tensorA, tensorB)
---------------------------------------------------------------------------InvalidArgumentError Traceback (most recent call last)Cell In[48], line 2 1 # will throw an error: ----> 2 tf.matmul(tensorA, tensorB) File /opt/conda/lib/python3.11/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.__traceback__) --> 153 raise e.with_traceback(filtered_tb) from None 154 finally: 155 del filtered_tb File /opt/conda/lib/python3.11/site-packages/tensorflow/python/framework/ops.py:5888, in raise_from_not_ok_status(e, name) 5886 def raise_from_not_ok_status(e, name) -> NoReturn: 5887 e.message += (" name: " + str(name if name is not None else "")) -> 5888 raise core._status_to_exception(e) from None InvalidArgumentError: {{function_node __wrapped__MatMul_device_/job:localhost/replica:0/task:0/device:CPU:0}} Matrix size-incompatible: In[0]: [4,3], In[1]: [6,2] [Op:MatMul] name:
In [ ]:
tensorA.shape, tensorB.shape
In [ ]:
reshapedB = tf.reshape(tensorB, shape=(3,4))
reshapedB
In [ ]:
# NOW it should multiply
tf.matmul(tensorA, reshapedB)

Transposing Tensors

In [ ]:
tensorB, reshapedB
In [ ]:
transposedB = tf.transpose(tensorB)
transposedB

The 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, smallBTensor
In [ ]:
# 
# 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)