<jupyter_start><jupyter_text>1. Tensors- Same as numpy<jupyter_code># scala
t0 = torch.tensor(4.)

print(t0)
print(t0.dtype)
print(t0.shape)
# Vector
t1 = torch.tensor([1., 2, 3, 4])

print(t1)
print(t1.dtype)
print(t1.shape)
# Matrix
t2 = torch.tensor(
    [[5., 6], 
     [7, 8], 
     [9, 10]])

print(t2)
print(t2.dtype)
print(t2.shape)
# 3-D tensor
t3 = torch.tensor([
    [[11, 12, 13], 
     [13, 14, 15]], 
    [[15, 16, 17], 
     [17, 18, 19.]]])

print(t3)
print(t3.dtype)
print(t3.shape)<jupyter_output>tensor([[[11., 12., 13.],
         [13., 14., 15.]],

        [[15., 16., 17.],
         [17., 18., 19.]]])
torch.float32
torch.Size([2, 2, 3])<jupyter_text>2. Tensor Operations<jupyter_code># Create tensors.
x = torch.tensor(3.)
w = torch.tensor(4., requires_grad=True)
b = torch.tensor(5., requires_grad=True)
# Arithmetic operations
y = w * x + b
y<jupyter_output><empty_output><jupyter_text>Compute Gradients<jupyter_code># The derivates of the input tensors are stored in the .grad property
y.backward()

# Display gradients
print('dy/dx:', x.grad) # None becoz x requires_grad=False
print('dy/dw:', w.grad) # dy/dw = x
print('dy/db:', b.grad) # dy/db = 1<jupyter_output>dy/dx: None
dy/dw: tensor(3.)
dy/db: tensor(1.)<jupyter_text>3. Numpy array  torch tensor<jupyter_code>import numpy as np

x = np.array([[1, 2], [3, 4.]])

print(x)
print(x.shape)
print(x.dtype)
# np array -> torch tensor
y = torch.from_numpy(x)

print(y)
print(y.shape)
print(y.dtype)
# torch tensor -> np array
z = y.numpy()

print(z)
print(z.shape)
print(z.dtype)<jupyter_output>[[1. 2.]
 [3. 4.]]
(2, 2)
float64
{'fraction_non_alphanumeric': 0.19863013698630136, 'fraction_numerical': 0.06413449564134496, 'mean_word_length': 3.811377245508982, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '346564', 'license': 'Unlicense', 'n_tokens_mistral': 766, 'n_tokens_neox': 707, 'n_words': 159, 'path': 'Pytorch/Practices/1_Tensors.ipynb', 'repo_name': 'dangkhoa1992/Advanced-Machine-Learning-Specialization', 'size': '1606'}