<jupyter_start><jupyter_text><jupyter_code># check if you have GPU
import torch
torch.cuda.is_available()

# torch tensor
import torch

t = torch.tensor(1, dtype=torch.float32, requires_grad=True)
print(t, t.shape)
print(t.numel())

t = torch.tensor([1], dtype=torch.float32, requires_grad=True)
print(t, t.shape)
print(t.numel())

t = torch.tensor([[1]], dtype=torch.float32, requires_grad=True)
print(t, t.shape)
print(t.numel())


import torch.nn as nn
# softmax allways on single dimension

m = nn.Softmax(dim=0)
input = torch.randn(2, 2)
print(input)

# do the softmax on tensor
output = m(input)
print(output)

# show that sum of tensor elemen on 0 axis is 1
print(output.sum(0))

# total sum is 2
print(output.sum())
import torch.nn as nn
# softmax exploration

m = nn.Softmax(dim=1)
input = torch.randn(2, 2)
print(input)

# do the softmax on tensor
output = m(input)
print(output)

# show that sum of tensor element on horisontal(1) axis is 1
print(output.sum(1))

#total sum is 2
print(output.sum())
import torch.nn as nn
# softmax on flatten 2D tensor

m = nn.Softmax(dim=0)

# flatten
input = torch.randn(2, 2).view(-1,1)
print(input.shape)
print(input)

# do the softmax on tensor
output = m(input)
print(output)

# show that sum of tensor element on horisontal(1) axis is 1
print(output.sum(0))

#total sum is 2
print(output.sum())
import numpy as np
print(input[0][0])
print(input[0][1])
print(input[1][0])
print(input[1][1])
print("...")
print(np.exp(input[0][0]))
print(np.exp(input[0][1]))
print(np.exp(input[1][0]))
print(np.exp(input[1][1]))
print("...")

# should be the same value as output[0][0] from above
print(np.exp(input[0][0]) / (np.exp(input[0][0]) + np.exp(input[1][0])) )
# implement softmax in numpy on array
import numpy as np
z = np.arange(5) #[0 1 2 3 4]
print(z)
softmax = lambda z:np.exp(z)/np.sum(np.exp(z)) #[0.01165623 0.03168492 0.08612854 0.23412166 0.63640865]
print(softmax(z))

# more gradual
ze = np.exp(z)
print(ze)
zesum = np.sum(ze)
print(zesum)
smx = ze/zesum
print(smx)


import torch
output = torch.randn(10, 3)
print(output)
prob = torch.softmax(output, dim=1)
print(prob)

classes = torch.LongTensor(10, 1).random_(0, 3)
class_prob = torch.gather(prob, 1, classes)
print(class_prob)
import torch
import torch.nn as nn

# torch.nn.CrossEntropyLoss(weight=None,size_average=True)
# This criterion combines 
# LogSoftMax and NLLLoss in one single class.
# It is useful when training a classification problem with n classes. 

# If provided, the optional argument weights should be a 1D Tensor
# assigning weight to each of the classes. 
# This is particularly useful when you have an unbalanced training set.

# The input is expected to contain scores for each class.input 
# has to be a 2D Tensor of size batch x n.

criterion = nn.CrossEntropyLoss(ignore_index=-1)

target = torch.tensor([-1, 1])
print("target:", target)

output = torch.randn(2, 10, requires_grad=True)
print("output:", output)

loss = criterion(output, target)

loss.backward()
print("loss:", loss)
print("grad:", output.grad)



# segments

a = torch.randn(3, 2)
print(a.shape)
print(a)


res = a.unsqueeze(1) - a
print(res.shape)
print(res)


res =  a.unsqueeze(0) - a
print(res.shape)
print(res)

# CEL for albino

import torch 
import torch.nn as nn

batch_size = 3
a = torch.LongTensor(batch_size).random_(5)
b = torch.randn(batch_size, 5)

criterion = nn.CrossEntropyLoss()
loss = criterion(b, a)
print(loss)
# onces into zeros

a = torch.zeros(5, 5)
a[2:, 2:] = torch.ones(3, 3)
print(a)

a = torch.zeros(5, 5)
a[2:, :-2] = torch.ones(3, 3)
print(a)

# 
# substract values on sequential index

x = torch.tensor([1, 2, 4, 7, 0])
x_diff = x[1:] - x[:-1]

print(x[1:])
print(x[:-1])
print(x_diff)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15374367172928324, 'fraction_numerical': 0.04103383959499068, 'mean_word_length': 4.8564742589703584, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '257295', 'license': 'MIT', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1456, 'n_words': 409, 'path': 'PyTorch_teorija.ipynb', 'repo_name': 'dejanbatanjac/pytorch-learning-101', 'size': '3753'}