<jupyter_start><jupyter_text>Tensors Tensors are similar to NumPy ndarrays, with the addition being that Tensors can also be used on a GPU to accelerate computing.<jupyter_code>import torch
import numpy as np<jupyter_output><empty_output><jupyter_text>Construct a 5x3 matrix, uninitialized:<jupyter_code>x = torch.Tensor(5,3)
x<jupyter_output><empty_output><jupyter_text>Construct a randomly initialized matrix<jupyter_code>x = torch.rand(5,3)
x<jupyter_output><empty_output><jupyter_text>Get its size<jupyter_code>x.size()<jupyter_output><empty_output><jupyter_text>* Note: torch.Size is in fact a tuple, so it supports the same operations  Operations<jupyter_code>y = torch.rand(5,3)
x + y
torch.add(x,y)
result = torch.Tensor(5, 3)
torch.add(x, y, out=result)
result<jupyter_output><empty_output><jupyter_text>Addition in-place:<jupyter_code>y.add_(x)
print(y)<jupyter_output>1.3135  0.7746  1.7219
 0.7025  0.1372  0.5644
 1.1724  0.4825  0.8630
 0.6898  1.6982  1.0051
 1.0233  0.4390  0.9666
[torch.FloatTensor of size 5x3]<jupyter_text>You can use standard numpy-like indexing with all bells and whistles!<jupyter_code>print(x[:, 1])<jupyter_output>0.1406
 0.1292
 0.2478
 0.8066
 0.4108
[torch.FloatTensor of size 5]<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 memory locations, and changing one will change the other.Convertng torch Tensor to numpy Array<jupyter_code>a = torch.ones(5)
a
b = a.numpy()
b<jupyter_output><empty_output><jupyter_text>See how the numpy array changed in value.<jupyter_code>a.add_(1)
a
b<jupyter_output><empty_output><jupyter_text>Covering numpy Array to torch TensorSee how changing the np array changedthe torch Tensor atomatically<jupyter_code>a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
a
b<jupyter_output><empty_output><jupyter_text>All the  Tensors on the CPU except a CharTensor support convering to Numpy and back.  CUDA Tensors Tensors can be moved onto GPU using the .cude function.<jupyter_code>if torch.cuda.is_available():
    x = x.cuda()
    y = y.cuda()
    x + y
    print(x + y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0975816716164616, 'fraction_numerical': 0.051336444633008065, 'mean_word_length': 5.914956011730205, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '396024', 'license': 'MIT', 'n_tokens_mistral': 927, 'n_tokens_neox': 864, 'n_words': 236, 'path': 'lab4/3.pytorch_basics.ipynb', 'repo_name': 'Data-Science-FMI/introduction-to-ml-2018', 'size': '2357'}