<jupyter_start><jupyter_text>What is PyTorch?It’s a Python based scientific computing package targeted at two sets of audiences:-  Tensorial library that uses the power of GPUs-  A deep learning research platform that provides maximum flexibility and speed Import the library<jupyter_code>import torch  # <Ctrl> / <Shift> + <Return><jupyter_output><empty_output><jupyter_text>Getting help in Jupyter<jupyter_code>torch.sq  # <Tab>
# What about all `*Tensor`s?
torch.*Tensor?
torch.nn.Module()  # <Shift>+<Tab>
# Annotate your functions / classes!
torch.nn.Module?
torch.nn.Module??<jupyter_output><empty_output><jupyter_text>Dropping to Bash: magic!<jupyter_code>! ls -lh
%%bash
for f in $(ls *.*); do
    echo $(wc -l $f)
done
# Help?
%%bash?
# Getting some general help
%magic<jupyter_output><empty_output><jupyter_text>Python native data typesPython has many native datatypes. Here are the important ones: - **Booleans** are either `True` or `False`. - **Numbers** can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even complex numbers. - **Strings** are sequences of Unicode characters, e.g. an html document. - **Lists** are ordered sequences of values. - **Tuples** are ordered, immutable sequences of values. - **Sets** are unordered bags of values. - **Dictionaries** are unordered bags of key-value pairs. See [here](http://www.diveintopython3.net/native-datatypes.html) for a complete overview. More resources 1. Brief Python introduction [here](https://learnxinyminutes.com/docs/python3/). 2. Full Python tutorial [here](https://docs.python.org/3/tutorial/). 3. A Whirlwind Tour of Python [here](https://github.com/jakevdp/WhirlwindTourOfPython). 4. Python Data Science Handbook [here](https://github.com/jakevdp/PythonDataScienceHandbook).  Torch!<jupyter_code>t = torch.Tensor(2, 3, 4)
type(t)
t.size()
# t.size() is a classic tuple =>
print('t size:', ' \u00D7 '.join(map(str, t.size())))
print(f'point in a {t.numel()} dimensional space')
print(f'organised in {t.dim()} sub-dimensions')
t
# Mind the underscore!
t.random_(10)
t
r = torch.Tensor(t)
r.resize_(3, 8)
r
r.zero_()
t
# This *is* important, sigh...
s = r.clone()
s.fill_(1)
s
r<jupyter_output><empty_output><jupyter_text>Vectors (1D Tensors)<jupyter_code>v = torch.Tensor([1, 2, 3, 4]); v
print(f'dim: {v.dim()}, size: {v.size()[0]}')
w = torch.Tensor([1, 0, 2, 0]); w
# Element-wise multiplication
v * w
# Scalar product: 1*1 + 2*0 + 3*2 + 4*0
v @ w
x = torch.Tensor(5).random_(10); x
print(f'first: {x[0]}, last: {x[-1]}')
# Extract sub-Tensor [from:to)
x[1:2 + 1]
v
v = torch.arange(1, 4 + 1); v
print(v.pow(2), v)
print(v.pow_(2), v)<jupyter_output><empty_output><jupyter_text>Matrices (2D Tensors)<jupyter_code>m = torch.Tensor([[2, 5, 3, 7],
                  [4, 2, 1, 9]]); m
m.dim()
print(m.size(0), m.size(1), m.size(), sep=' -- ')
m.numel()
m[0][2]
m[0, 2]
m[:, 1]
m[:, [1]]
m[[0], :]
m[0, :]
v = torch.arange(1, 4 + 1); v
m @ v
m[[0], :] @ v
m[[1], :] @ v
m + torch.rand(2, 4)
m - torch.rand(2, 4)
m * torch.rand(2, 4)
m / torch.rand(2, 4)
m.t()
# Same as
m.transpose(0, 1)<jupyter_output><empty_output><jupyter_text>Constructors<jupyter_code>torch.arange(3, 8 + 1)
torch.arange(5.7, -3, -2.1)
torch.linspace(3, 8, 20).view(1, -1)
torch.zeros(3, 5)
torch.ones(3, 2, 5)
torch.eye(3)
# Pretty plotting config
%run plot_conf.py
plt_style()
# Numpy bridge!
plt.hist(torch.randn(1000).numpy(), 100);
plt.hist(torch.randn(10**6).numpy(), 100);  # how much does this chart weight?
# use rasterized=True for SVG/EPS/PDF!
plt.hist(torch.rand(10**6).numpy(), 100);<jupyter_output><empty_output><jupyter_text>Casting<jupyter_code>torch.*Tensor?
m
m.double()
m.byte()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
m.to(device)
m_np = m.numpy(); m_np
m_np[0, 0] = -1; m_np
m
n_np = np.arange(5)
n = torch.from_numpy(n_np)
print(n_np, n)
n.mul_(2)
n_np<jupyter_output><empty_output><jupyter_text>More fun<jupyter_code>a = torch.Tensor([[1, 2, 3, 4]])
b = torch.Tensor([[5, 6, 7, 8]])
print(a, b)
torch.cat((a, b), 0)
torch.cat((a, b), 1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1866601514781334, 'fraction_numerical': 0.03615929635963841, 'mean_word_length': 5.002932551319648, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 44, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, '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': '407923', 'license': 'Apache-2.0', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1679, 'n_words': 421, 'path': 'pytorch/New_Tuts/01-tensor_tutorial.ipynb', 'repo_name': 'AadityaGupta/Artificial-Intelligence-Deep-Learning-Machine-Learning-Tutorials', 'size': '4093'}