<jupyter_start><jupyter_text>Exercise 4 Tensor MultiplicationIn this exercise we are going to demonstrate how to perform matrix multiplication on tensors that both have at least 3 dimensions.Let's start with creating a tensor with 3 dimensions<jupyter_code>import numpy as np
mat1 = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
mat1<jupyter_output><empty_output><jupyter_text>Let's verify that the shape has 3 dimensions<jupyter_code>mat1.shape<jupyter_output><empty_output><jupyter_text>Great, it has 3 dimensions.Now we need another matrix that has at least one dimension that matches in size. We can do this by taking the transpose of the matrix.<jupyter_code>mat2 = mat1.T
mat2<jupyter_output><empty_output><jupyter_text>Let's look at the shape<jupyter_code>mat2.shape<jupyter_output><empty_output><jupyter_text>Now we have our two matrices we can mutiply them in a number of different ways, we just have to make sure the inner dimensions match. If we multiply with `mat2` first then we expect our resultant matrix to be:$$mat2_{3\times2\times2}\times mat1_{2\times2\times3} = mat3_{3\times2\times2\times3}$$We end up with a 4-dimensional tensor.<jupyter_code>mat3 = mat2.dot(mat1)
mat3<jupyter_output><empty_output><jupyter_text>And let's verify the shape<jupyter_code>mat3.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09262048192771084, 'fraction_numerical': 0.03313253012048193, 'mean_word_length': 6.817647058823529, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '292998', 'license': 'MIT', 'n_tokens_mistral': 465, 'n_tokens_neox': 440, 'n_words': 145, 'path': 'Lesson02/Exercise09.ipynb', 'repo_name': 'TrainingByPackt/Applied-Data-Learning-with-Keras-eLearning', 'size': '1328'}