<jupyter_start><jupyter_text>PyTorch ML libraryAn open source machine learning framework that accelerates the path from research prototyping to production deployment.![PyTorch](https://upload.wikimedia.org/wikipedia/commons/9/96/Pytorch_logo.png)<jupyter_code># conda install pytorch torchvision cudatoolkit=10.1 -c pytorch<jupyter_output><empty_output><jupyter_text>Cheat Sheet* https://pytorch.org/tutorials/beginner/ptcheat.html  https://pytorch.org/get-started/locally/<jupyter_code>from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
torch.cuda.is_available()
# Cloud Partner Alternatives (mosty paid)
# https://pytorch.org/get-started/cloud-partners/<jupyter_output><empty_output><jupyter_text>Tutorial for those who know NumPyhttps://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html<jupyter_code>x = torch.empty(5, 3)
print(x)
x = torch.rand(5, 3)
print(x)
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
x = torch.tensor([5.5, 3])
print(x)
x = torch.tensor([[5.5, 3],[3.1,60.3]])
print(x)
import numpy as np
x = torch.tensor(np.random.rand(3,6,2))
print(x)
x.size()
# Addition: providing an output tensor as argument
result = torch.empty(5, 3)
y = torch.rand(5, 3)
x = torch.rand(5, 3)
torch.add(x, y, out=result)
print(result)
# adds x to y in place
y.add_(x)
print(y)
# Any operation that mutates a tensor in-place is post-fixed with an _.
# For example: x.copy_(y), x.t_(), will change x.
# Resizing: If you want to resize/reshape tensor, you can use torch.view
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())
# If you have a one element tensor, 
# use .item() to get the value as a Python number
x = torch.randn(1)
print(x)
print(x.item())<jupyter_output>tensor([-0.5410])
-0.5410429239273071<jupyter_text>Converting a Torch Tensor to a NumPy Array<jupyter_code>a = torch.ones(5)
print(a)
b = a.numpy()
print(b,type(b))
# See how the numpy array changed in value.
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 TensorSee 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)
a = np.ones(5)
b = torch.tensor(a)
np.add(a, 1, out=a)
print(a)
print(b)
## All the Tensors on the CPU except a CharTensor support converting to NumPy and back.<jupyter_output><empty_output><jupyter_text>CUDA TensorsTensors 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>tensor([0.4590], device='cuda:0')
tensor([0.4590], dtype=torch.float64)
{'fraction_non_alphanumeric': 0.14124647666771062, 'fraction_numerical': 0.029752583777012214, 'mean_word_length': 4.673179396092363, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 5, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '502844', 'license': 'MIT', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1204, 'n_words': 332, 'path': 'PyTorch_ML_Library/PyTorch Introduction.ipynb', 'repo_name': 'ValRCS/RCS_ML_01_20', 'size': '3193'}