<jupyter_start><jupyter_text>Manipulate PyTorch Tensors Matrix manipulation<jupyter_code>import torch<jupyter_output><empty_output><jupyter_text>Make the matrices A and B below. Add them together to obtain a matrix C. Print these three matrices.$$A =\begin{bmatrix}1 & 2 \\ 3 & 4\end{bmatrix}  \qquad B =\begin{bmatrix}10 & 20 \\ 30 & 40\end{bmatrix}  \qquad C=A+B =?$$<jupyter_code>import numpy as np
# write your code here

A = np.array([[1,2], [3, 4]])
B = np.array([[10, 20], [30, 40]])
C = A + B

# print
print(A)
print('')
print(B)
print('')
print(C)<jupyter_output>[[1 2]
 [3 4]]

[[10 20]
 [30 40]]

[[11 22]
 [33 44]]<jupyter_text>Print the dimension, size and type of the matrix A. Remember, the commands are dim(), size() and type()<jupyter_code># write your code here

print(A.ndim)    # print the dimension of the matrix A
print('')
print(A.size)    # print the size of the matrix A
print('')
print(type(A))    # print the type of the matrix A<jupyter_output>2

4

<class 'numpy.ndarray'><jupyter_text>Convert the matrix A to be an integer matrix  (type LongTensor). Remember, the command is long(). Then print the type to check it was indeed converted.<jupyter_code># write your code here

A_long = torch.tensor(A, dtype = torch.long)

print(type(A_long))    # print the type of A_long
print('')
print(type(A))    # print the type of A<jupyter_output><class 'torch.Tensor'>

<class 'torch.Tensor'><jupyter_text>Make a random 5 x 2 x 3 Tensor. The command is torch.rand.  Then do the following:   1) Print the tensor,  2) Print its type, 3) Print its dimension,  4) Print its size, 5) Print the size of its middle dimension.<jupyter_code># write your code here

A = torch.rand(5,2,3)

print(A)
print(type(A))    # print the type of A
print(A.dim())    # print the dimension of A
print(A.size())    # print the size of A
print(A[0].dim())    # print the size of the middle (second) dimension<jupyter_output>tensor([[[0.1661, 0.9535, 0.0349],
         [0.1373, 0.5067, 0.3667]],

        [[0.8873, 0.5244, 0.5050],
         [0.5828, 0.3709, 0.3313]],

        [[0.5031, 0.2025, 0.9241],
         [0.8626, 0.1085, 0.7283]],

        [[0.0095, 0.8186, 0.5817],
         [0.7872, 0.8827, 0.5018]],

        [[0.1271, 0.6069, 0.1658],
         [0.7814, 0.8648, 0.1189]]])
<class 'torch.Tensor'>
3
torch.Size([5, 2, 3])
2<jupyter_text>Make 2 x 3 x 4 x 5 tensor filled with zeros then print it. (The command is torch.zeros). See if you can make sense of the display.<jupyter_code># write your code here

A = torch.zeros(2, 3, 4, 5)

print(A)<jupyter_output>tensor([[[[0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]],

         [[0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]],

         [[0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]]],


        [[[0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]],

         [[0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]],

         [[0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.],
          [0., 0., 0., 0., 0.]]]])
{'fraction_non_alphanumeric': 0.2069377990430622, 'fraction_numerical': 0.10197368421052631, 'mean_word_length': 2.2890855457227137, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '284555', 'license': 'Apache-2.0', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1314, 'n_words': 438, 'path': '3week/notebook/assignment-03-a.ipynb', 'repo_name': 'minjoong507/Machine-learning-project', 'size': '3344'}