<jupyter_start><jupyter_text>Lab 02: Manipulate PyTorch Tensors -- exercise<jupyter_code># For Google Colaboratory
import sys, os
if 'google.colab' in sys.modules:
    from google.colab import drive
    drive.mount('/content/gdrive')
    file_name = 'pytorch_tensor_part1_exercise.ipynb'
    import subprocess
    path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode("utf-8")
    print(path_to_file)
    path_to_file = path_to_file.replace(file_name,"").replace('\n',"")
    os.chdir(path_to_file)
    !pwd
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># write your code here
A = torch.Tensor( [[1,2], [3,4]] )
B = torch.Tensor( [[10,20], [30,40]] )
C = A + B
print(C)<jupyter_output>tensor([[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.dim())
print(A.size())
print(A.type())<jupyter_output>2
torch.Size([2, 2])
torch.FloatTensor<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 = A.long()
print(A.type())<jupyter_output>torch.LongTensor<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, '\n')
print(A.type(),'\n')
print(A.dim(), '\n')
print(A.size(), '\n')
print(A.size(1), '\n')<jupyter_output>tensor([[[0.3027, 0.7093, 0.4380],
         [0.5074, 0.0826, 0.5419]],

        [[0.8826, 0.7337, 0.9555],
         [0.3317, 0.8209, 0.7698]],

        [[0.2590, 0.1312, 0.1132],
         [0.0460, 0.9927, 0.0722]],

        [[0.8769, 0.8436, 0.3024],
         [0.0934, 0.2494, 0.8765]],

        [[0.4738, 0.1376, 0.9198],
         [0.8779, 0.7058, 0.5215]]]) 

torch.FloatTensor 

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.2154761904761905, 'fraction_numerical': 0.0994047619047619, 'mean_word_length': 2.344278606965174, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '371804', 'license': 'MIT', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1334, 'n_words': 391, 'path': 'codes/labs_lecture02/lab02_pytorch_tensor1/pytorch_tensor_part1_exercise.ipynb', 'repo_name': 'alanwuha/CE7454_2019', 'size': '3360'}