<jupyter_start><jupyter_text>What is PyTorch?================It’s a Python-based scientific computing package targeted at two sets ofaudiences:-  A replacement for NumPy to use the power of GPUs-  a deep learning research platform that provides maximum flexibility   and speedGetting Started---------------Tensors^^^^^^^Tensors are similar to NumPy’s ndarrays, with the addition being thatTensors can also be used on a GPU to accelerate computing.<jupyter_code>import torch<jupyter_output><empty_output><jupyter_text>Construct a 5x3 matrix, uninitialized:<jupyter_code>x = torch.empty(5, 3)
print(x)<jupyter_output>tensor([[ 0.0000e+00,  1.0842e-19,  2.5088e+20],
        [-1.5849e+29,  1.4569e-19,  2.7517e+12],
        [ 7.5338e+28,  3.0313e+32,  6.3828e+28],
        [ 1.4603e-19,  1.0899e+27,  6.8943e+34],
        [ 1.1835e+22,  7.0976e+22,  1.8515e+28]])<jupyter_text>Construct a randomly initialized matrix:<jupyter_code>x = torch.rand(5, 3)
print(x)<jupyter_output>tensor([[0.0963, 0.6163, 0.8066],
        [0.8684, 0.1400, 0.8114],
        [0.1585, 0.7010, 0.8655],
        [0.1266, 0.8027, 0.6492],
        [0.7014, 0.2315, 0.2354]])<jupyter_text>Construct a matrix filled zeros and of dtype long:<jupyter_code>x = torch.zeros(5, 3, dtype=torch.long)
print(x)<jupyter_output>tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])<jupyter_text>Construct a tensor directly from data:<jupyter_code>x = torch.tensor([5.5, 3])
print(x)<jupyter_output>tensor([5.5000, 3.0000])<jupyter_text>or create a tensor based on an existing tensor. These methodswill reuse properties of the input tensor, e.g. dtype, unlessnew values are provided by user<jupyter_code>x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes
print(x)

x = torch.randn_like(x, dtype=torch.float)    # override dtype!
print(x)                                      # result has the same size<jupyter_output>tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[-0.7854, -0.8631, -0.1627],
        [ 0.5484,  0.3686, -0.2852],
        [-0.3235, -0.2453, -0.3229],
        [ 0.2250,  0.4170,  0.6299],
        [-0.9773, -0.0817, -0.5379]])<jupyter_text>Get its size:<jupyter_code>print(x.size())<jupyter_output>torch.Size([5, 3])<jupyter_text>Note``torch.Size`` is in fact a tuple, so it supports all tuple operations.Operations^^^^^^^^^^There are multiple syntaxes for operations. In the followingexample, we will take a look at the addition operation.Addition: syntax 1<jupyter_code>y = torch.rand(5, 3)
print(x + y)<jupyter_output>tensor([[-0.2533, -0.0578, -0.0812],
        [ 1.0172,  0.6883,  0.1773],
        [ 0.1370,  0.7107, -0.1719],
        [ 0.4513,  1.1937,  0.7522],
        [-0.5006,  0.1965, -0.2918]])<jupyter_text>Addition: syntax 2<jupyter_code>print(torch.add(x, y))<jupyter_output>tensor([[-0.2533, -0.0578, -0.0812],
        [ 1.0172,  0.6883,  0.1773],
        [ 0.1370,  0.7107, -0.1719],
        [ 0.4513,  1.1937,  0.7522],
        [-0.5006,  0.1965, -0.2918]])<jupyter_text>Addition: providing an output tensor as argument<jupyter_code>result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)<jupyter_output>tensor([[-0.2533, -0.0578, -0.0812],
        [ 1.0172,  0.6883,  0.1773],
        [ 0.1370,  0.7107, -0.1719],
        [ 0.4513,  1.1937,  0.7522],
        [-0.5006,  0.1965, -0.2918]])<jupyter_text>Addition: in-place<jupyter_code># adds x to y
y.add_(x)
print(y)<jupyter_output>tensor([[-0.2533, -0.0578, -0.0812],
        [ 1.0172,  0.6883,  0.1773],
        [ 0.1370,  0.7107, -0.1719],
        [ 0.4513,  1.1937,  0.7522],
        [-0.5006,  0.1965, -0.2918]])<jupyter_text>NoteAny operation that mutates a tensor in-place is post-fixed with an ``_``.    For example: ``x.copy_(y)``, ``x.t_()``, will change ``x``.You can use standard NumPy-like indexing with all bells and whistles!<jupyter_code>print(x[:, 1])<jupyter_output>tensor([-0.8631,  0.3686, -0.2453,  0.4170, -0.0817])<jupyter_text>Resizing: If you want to resize/reshape tensor, you can use ``torch.view``:<jupyter_code>x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())<jupyter_output>torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])<jupyter_text>If you have a one element tensor, use ``.item()`` to get the value as aPython number<jupyter_code>x = torch.randn(1)
print(x)
print(x.item())<jupyter_output>tensor([0.0294])
0.029441991820931435<jupyter_text>**Read later:**  100+ Tensor operations, including transposing, indexing, slicing,  mathematical operations, linear algebra, random numbers, etc.,  are described  `here `_.NumPy Bridge------------Converting a Torch Tensor to a NumPy array and vice versa is a breeze.The Torch Tensor and NumPy array will share their underlying memorylocations, and changing one will change the other.Converting a Torch Tensor to a NumPy Array^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<jupyter_code>a = torch.ones(5)
print(a)
b = a.numpy()
print(b)<jupyter_output>[1. 1. 1. 1. 1.]<jupyter_text>See how the numpy array changed in value.<jupyter_code>a.add_(1)
print(a)
print(b)<jupyter_output>tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]<jupyter_text>Converting NumPy Array to Torch Tensor^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^See how changing the np array changed the Torch Tensor automatically<jupyter_code>import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)<jupyter_output>[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)<jupyter_text>All the Tensors on the CPU except a CharTensor support converting toNumPy and back.CUDA Tensors------------Tensors can be moved onto any device using the ``.to`` method.<jupyter_code># let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17986176562990888, 'fraction_numerical': 0.11247251021049325, 'mean_word_length': 3.4901269393511987, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '284459', 'license': 'MIT', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2503, 'n_words': 610, 'path': 'pytorch_blitz/01_tensor_tutorial.ipynb', 'repo_name': 'harrywang/buffet', 'size': '6366'}