<jupyter_start><jupyter_text>Tensors=======Tensors behave almost exactly the same way in PyTorch as they do inTorch.Create a tensor of size (5 x 7) with uninitialized memory:<jupyter_code>import torch
a = torch.empty(5, 7, dtype=torch.float)<jupyter_output><empty_output><jupyter_text>Initialize a double tensor randomized with a normal distribution with mean=0,var=1:<jupyter_code>a = torch.randn(5, 7, dtype=torch.double)
print(a)
print(a.size())<jupyter_output><empty_output><jupyter_text>Note``torch.Size`` is in fact a tuple, so it supports the same operationsInplace / Out-of-place----------------------The first difference is that ALL operations on the tensor that operatein-place on it will have an ``_`` postfix. For example, ``add`` is theout-of-place version, and ``add_`` is the in-place version.<jupyter_code>a.fill_(3.5)
# a has now been filled with the value 3.5

b = a.add(4.0)
# a is still filled with 3.5
# new tensor b is returned with values 3.5 + 4.0 = 7.5

print(a, b)<jupyter_output><empty_output><jupyter_text>Some operations like ``narrow`` do not have in-place versions, andhence, ``.narrow_`` does not exist. Similarly, some operations like``fill_`` do not have an out-of-place version, so ``.fill`` does notexist.Zero Indexing-------------Another difference is that Tensors are zero-indexed. (In lua, tensors areone-indexed)<jupyter_code>b = a[0, 3]  # select 1st row, 4th column from a<jupyter_output><empty_output><jupyter_text>Tensors can be also indexed with Python's slicing<jupyter_code>b = a[:, 3:5]  # selects all rows, 4th column and  5th column from a<jupyter_output><empty_output><jupyter_text>No camel casing---------------The next small difference is that all functions are now NOT camelCaseanymore. For example ``indexAdd`` is now called ``index_add_``<jupyter_code>x = torch.ones(5, 5)
print(x)
z = torch.empty(5, 2)
z[:, 0] = 10
z[:, 1] = 100
print(z)
x.index_add_(1, torch.tensor([4, 0], dtype=torch.long), z)
print(x)<jupyter_output><empty_output><jupyter_text>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 torch Tensor to numpy Array^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<jupyter_code>a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
a.add_(1)
print(a)
print(b) 	# see how the numpy array changed in value<jupyter_output><empty_output><jupyter_text>Converting numpy Array to torch Tensor^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<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)  # see how changing the np array changed the torch Tensor automatically<jupyter_output><empty_output><jupyter_text>All the Tensors on the CPU except a CharTensor support converting toNumPy and back.CUDA Tensors------------CUDA Tensors are nice and easy in pytorch, and transfering a CUDA tensorfrom the CPU to GPU will retain its underlying type.<jupyter_code># let us run this cell only if CUDA is available
if torch.cuda.is_available():

    # creates a LongTensor and transfers it
    # to GPU as torch.cuda.LongTensor
    a = torch.full((10,), 3, device=torch.device("cuda"))
    print(type(a))
    b = a.to(torch.device("cpu"))
    # transfers it to CPU, back to
    # being a torch.LongTensor<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14673752583407146, 'fraction_numerical': 0.015057573073516387, 'mean_word_length': 4.933450087565674, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '371327', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1115, 'n_words': 371, 'path': 'build/_downloads/957883752592bcba93b1cbd1d8af6f78/tensor_tutorial_old.ipynb', 'repo_name': 'ScorpioDoctor/antares02', 'size': '3387'}