<jupyter_start><jupyter_text>Layers : Fundamental blocks of Neural Network<jupyter_code>import torch
from torch.nn import Linear, ReLU
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
from torchvision import models
from torchvision.datasets import ImageFolder
linear_layer = Linear(in_features=5,out_features=3,bias=True)
inp = Variable(torch.randn(1,5))
linear_layer(inp)
linear_layer.weight
linear_layer.bias
linear_layer = Linear(5,3)
linear_layer_2 = Linear(3,2)
linear_layer_2(linear_layer(inp))
myLayer = Linear(in_features=10,out_features=5,bias=True)
inp = Variable(torch.randn(1,10))
myLayer = Linear(in_features=10,out_features=5,bias=True) 
myLayer(inp)
myLayer.weight
myLayer.bias<jupyter_output><empty_output><jupyter_text>PyTorch Non-linear Activations<jupyter_code>sample_data = Variable(torch.Tensor([[1,2,-1,-1]])) 
myRelu = ReLU()
myRelu(sample_data)
import torch.nn as nn
import torch.nn.functional as F
sample_data = Variable(torch.Tensor([[1,2,-1,-1]])) 
f = F.relu(sample_data) # Much simpler.
f<jupyter_output><empty_output><jupyter_text>Neural Network<jupyter_code>class MyFirstNetwork(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(MyFirstNetwork,self).__init__() 
        self.layer1 = nn.Linear(input_size,hidden_size) 
        self.layer2 = nn.Linear(hidden_size,output_size)
    def __forward__(self,input): 
        out = self.layer1(input) 
        out = nn.ReLU(out)
        out = self.layer2(out) 
        return out
my_network = MyFirstNetwork(input_size = 3, hidden_size = 2, output_size = 1)<jupyter_output><empty_output><jupyter_text>Loss<jupyter_code>loss = nn.MSELoss()
input = Variable(torch.randn(3, 5), requires_grad=True) 
target = Variable(torch.randn(3, 5))
output = loss(input, target)
output.backward()
def cross_entropy(true_label, prediction):
    if true_label == 1:
        return -log(prediction)
    else:
        return -log(1 - prediction)
loss = nn.CrossEntropyLoss()
input = Variable(torch.randn(3, 5), requires_grad=True) 
target = Variable(torch.LongTensor(3).random_(5)) 
output = loss(input, target)
output.backward()<jupyter_output><empty_output><jupyter_text>Optimizer<jupyter_code># for demo
optimizer = torch.optim.SGD(my_network.parameters(), lr = 0.01)
for input, target in torch.utils.data.Dataset:
    optimizer.zero_grad()
    output = model(input)
    loss = loss_fn(output, target)
    loss.backward()
    optimizer.step()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12314480545527477, 'fraction_numerical': 0.0188527878058564, 'mean_word_length': 5.814207650273224, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '54466', 'license': 'MIT', 'n_tokens_mistral': 930, 'n_tokens_neox': 896, 'n_words': 158, 'path': 'Chapter03/Chapter03a.ipynb', 'repo_name': 'PacktPublishing/Deep-Learning-with-PyTorch-1.x', 'size': '2493'}