<jupyter_start><jupyter_text>+, -, *, /<jupyter_code>x1 = torch.empty(1)
x2 = torch.empty(3)
x3 = torch.empty(2, 3)
x4 = torch.empty(2, 2, 3)

print(x1)
print(x2)
print(x3)
print(x4)
x1 = torch.rand(2, 2)
print(x1)
x1 = torch.zeros(2, 2)
print(x1)
x1 = torch.ones(2, 2)
print(x1)
x1 = torch.ones(2, 2)
x2 = torch.empty(2, 2, dtype=torch.int)
x3 = torch.empty(2, 2, dtype=torch.double)
x4 = torch.empty(2, 2, dtype=torch.float16)

print(x1.dtype)
print(x2.dtype)
print(x3.dtype)
print(x4.dtype)
x1 = torch.ones(2, 2)
print(x1.size())
x1 = torch.tensor([2.5, 0.1])
print(x1)
x = torch.rand(2,2)
y = torch.rand(2,2)
print(x)
print(y)
z1 = x + y
z2 = torch.add(x,y)
print(z1)
print(z2)
x = torch.rand(2,2)
y = torch.rand(2,2)
print(x)
print(y)

y.add_(y)
print(y)
x = torch.rand(2,2)
y = torch.rand(2,2)
print(x)
print(y)
z1 = x - y
z2 = torch.sub(x,y)
print(z1)
print(z2)
x = torch.rand(2,2)
y = torch.rand(2,2)
print(x)
print(y)
z1 = x * y
z2 = torch.mul(x,y)
print(z1)
print(z2)
x = torch.rand(2,2)
y = torch.rand(2,2)
print(x)
print(y)

y.mul_(y)
print(y)
x = torch.rand(2,2)
y = torch.rand(2,2)
print(x)
print(y)
z1 = x / y
z2 = torch.div(x,y)
print(z1)
print(z2)<jupyter_output>tensor([[0.5599, 0.8761],
        [0.9152, 0.5830]])
tensor([[0.1566, 0.5797],
        [0.2742, 0.1163]])
tensor([[3.5750, 1.5112],
        [3.3380, 5.0124]])
tensor([[3.5750, 1.5112],
        [3.3380, 5.0124]])<jupyter_text>slice<jupyter_code>x = torch.rand(5, 3)
print(x)
print(x[:, 0])
print(x[1, :])
print(x[1, 1])

# only one value
print(x[1, 1].item())<jupyter_output>tensor([[0.8413, 0.5320, 0.1970],
        [0.2707, 0.3864, 0.1628],
        [0.2838, 0.0278, 0.1252],
        [0.8109, 0.7748, 0.8848],
        [0.3527, 0.6280, 0.1444]])
tensor([0.8413, 0.2707, 0.2838, 0.8109, 0.3527])
tensor([0.2707, 0.3864, 0.1628])
tensor(0.3864)
0.38635510206222534<jupyter_text>reshape<jupyter_code>x = torch.rand(4, 4)
print(x)
y = x.view(16)
print(y)
z = x.view(-1,8)
print(z.size())<jupyter_output>tensor([[0.7925, 0.4120, 0.1338, 0.6396],
        [0.0628, 0.3492, 0.2247, 0.0810],
        [0.6201, 0.1994, 0.8933, 0.1221],
        [0.5823, 0.9720, 0.1244, 0.5458]])
tensor([0.7925, 0.4120, 0.1338, 0.6396, 0.0628, 0.3492, 0.2247, 0.0810, 0.6201,
        0.1994, 0.8933, 0.1221, 0.5823, 0.9720, 0.1244, 0.5458])
torch.Size([2, 8])<jupyter_text>numpy to torch<jupyter_code>a = torch.ones(5)
print(a)

b = a.numpy()
print(type(b))
print(b)

# in GPU, sharing the same position
a.add_(1)
print(a)
print(b)
a = np.ones(5)
print(a)

b = torch.from_numpy(a)
print(type(b))
print(b)

# in GPU, sharing the same position
a += 1
print(a)
print(b)
# tensor on GPU can't change to numpy

if torch.cuda.is_available():
    device = torch.device('cuda')
    x = torch.ones(5, device=device)
    y = torch.ones(5)
    y = y.to(device)
    z = x + y
    z = z.to('cpu')<jupyter_output>C:\Users\a4022\anaconda3\lib\site-packages\torch\cuda\__init__.py:143: UserWarning: 
GeForce RTX 3070 Laptop GPU with CUDA capability sm_86 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_61 sm_70 sm_75 compute_37.
If you want to use the GeForce RTX 3070 Laptop GPU GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

  warnings.warn(incompatible_device_warn.format(device_name, capability, " ".join(arch_list), device_name))<jupyter_text>add auto gradient<jupyter_code>x = torch.ones(5, requires_grad=True)
print(x)<jupyter_output>tensor([1., 1., 1., 1., 1.], requires_grad=True)
{'fraction_non_alphanumeric': 0.19769403824521936, 'fraction_numerical': 0.1498875140607424, 'mean_word_length': 4.066951566951567, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '626149', 'license': 'MIT', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1731, 'n_words': 340, 'path': 'PyTorch/Tensor_basics.ipynb', 'repo_name': 'rd8312/ML-and-DL', 'size': '3556'}