<jupyter_start><jupyter_text><NAME>, 2018320192  Getting Started<jupyter_code>import torch
x = torch.arange(12, dtype=torch.float64)
x
x.shape
x.size()
x = x.reshape((3, 4))
x
x.reshape(-1)
torch.FloatTensor(2, 3)
x = torch.Tensor(2, 3)
x
x.dtype
#Discussion : torch.Tensor automatically sets dtype as torch.float32
torch.empty(2, 3)
torch.zeros((2, 3, 4))
torch.ones((2, 3, 4))
y = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
y
torch.randn(3, 4)<jupyter_output><empty_output><jupyter_text>Operations<jupyter_code>x = torch.tensor([1, 2, 4, 8], dtype=torch.float32)
y = torch.ones_like(x) * 2
#Discussion : ones_like() makes a matrix with same size but filled with ones
print('x =', x)
print('x + y', x + y)
print('x - y', x - y)
print('x * y', x * y)
print('x / y', x / y)
torch.exp(x)
x = torch.arange(12, dtype=torch.float32).reshape((3, 4))
y = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]], dtype=torch.float32)
print(x.dtype)
print(y)
torch.mm(x, y.t())
#Discussion : t() makes transpose matrix
torch.dot(x.reshape(-1), y.reshape(-1))
#Discussion : torch.dot() requires one-dimensional vector
torch.cat((x, y), dim=0)
torch.cat((x, y), dim=1)
x == y
x.sum()
import numpy as np
np.asscalar(x.norm())<jupyter_output>c:\users\jasson31\appdata\local\programs\python\python36\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead<jupyter_text>Broadcast Mechanism<jupyter_code>a = torch.arange(3, dtype=torch.float).reshape((3, 1))
b = torch.arange(2, dtype=torch.float).reshape((1, 2))
a, b
a + b<jupyter_output><empty_output><jupyter_text>Indexing and Slicing<jupyter_code>x[1:3]
x[1, 2] = 9
x
x[0:2, :] = 12
x<jupyter_output><empty_output><jupyter_text>Saving Memory<jupyter_code>before = id(y)
y = y + x
id(y) == before
z = torch.zeros_like(y)
print('id(z):', id(z))
z[:] = x + y
print('id(z):', id(z))
before = id(z)
torch.add(x, y, out=z)
id(z) == before<jupyter_output><empty_output><jupyter_text>Mutual Transformation of PyTorch and NumPy<jupyter_code>a = x.numpy()
print(type(a))
b = torch.tensor(a)
print(type(b))<jupyter_output><class 'numpy.ndarray'>
<class 'torch.Tensor'>
{'fraction_non_alphanumeric': 0.19670631290027446, 'fraction_numerical': 0.04528819762122598, 'mean_word_length': 5.813084112149533, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '652545', 'license': 'Apache-2.0', 'n_tokens_mistral': 1011, 'n_tokens_neox': 970, 'n_words': 208, 'path': 'homework/Ch4.1 Data Manipulation.ipynb', 'repo_name': 'jasson31/d2l-pytorch', 'size': '2190'}