<jupyter_start><jupyter_text>Deep Learning with PyTorchAuthor: [<NAME>](http://teleported.in/)  2. Building a simple neural network<jupyter_code>import torch
import torch.nn as nn
import matplotlib.pyplot as plt

from torch.autograd import Variable

# Custom DataSet
from data import iris<jupyter_output><empty_output><jupyter_text>The Dataset and the challenge![iris](data/iris.jpg)The **Iris** flower, image source: [Wikimedia](https://en.wikipedia.org/wiki/Iris_(plant))| sepal_length_cm | sepal_width_cm | petal_length_cm | petal_width_cm | class           ||-----------------|----------------|-----------------|----------------|-----------------|| 5.1             | 3.5            | 1.4             | 0.2            | Iris-setosa     || 7.0             | 3.2            | 4.7             | 1.4            | Iris-versicolor || 6.3             | 3.3            | 6.0             | 2.5            | Iris-virginica  |* Total instances: 150 (we have separated 20% into validation set, rest into training set)* Download: [Data Source](https://archive.ics.uci.edu/ml/datasets/iris)  Let's do a head on the raw file<jupyter_code>!head data/iris.data.txt<jupyter_output><empty_output><jupyter_text>Create the Fully Connected Feed Forward Neural Network **Create the module**<jupyter_code>class IrisNet(nn.Module):
    
    def __init__(self, input_size, hidden1_size, hidden2_size, num_classes):
        
        super(IrisNet, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden1_size)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Linear(hidden1_size, hidden2_size)
        self.relu2 = nn.ReLU()
        self.fc3 = nn.Linear(hidden2_size, num_classes)  
    
    def forward(self, x):
        out = self.fc1(x)
        out = self.relu1(out)
        out = self.fc2(out)
        out = self.relu2(out)
        out = self.fc3(out)
        return out<jupyter_output><empty_output><jupyter_text>**Print the module**<jupyter_code>model = IrisNet(4, 100, 50, 3)
print(model)<jupyter_output><empty_output><jupyter_text>Create the DataLoader<jupyter_code>batch_size = 60
iris_data_file = 'data/iris.data.txt'
# Get the datasets
train_ds, test_ds = iris.get_datasets(iris_data_file)

# How many instances have we got?
print('# instances in training set: ', len(train_ds))
print('# instances in testing/validation set: ', len(test_ds))

# Create the dataloaders - for training and validation/testing
# We will be using the term validation and testing data interchangably
train_loader = torch.utils.data.DataLoader(dataset=train_ds, batch_size=batch_size, shuffle=True)
test_loader  = torch.utils.data.DataLoader(dataset=test_ds, batch_size=batch_size, shuffle=True)<jupyter_output><empty_output><jupyter_text>Instantiate the network, the loss function and the optimizer<jupyter_code># Our model
net = IrisNet(4, 100, 50, 3)

# Out loss function
criterion = nn.CrossEntropyLoss()

# Our optimizer
learning_rate = 0.001
optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate, nesterov=True, momentum=0.9, dampening=0)<jupyter_output><empty_output><jupyter_text>Train it!<jupyter_code>num_epochs = 500

train_loss = []
test_loss = []
train_accuracy = []
test_accuracy = []

for epoch in range(num_epochs):
    
    train_correct = 0
    train_total = 0
    
    for i, (items, classes) in enumerate(train_loader):
        
        # Convert torch tensor to Variable
        items = Variable(items)
        classes = Variable(classes)
        
        net.train()           # Put the network into training mode
        
        optimizer.zero_grad() # Clear off the gradients from any past operation
        outputs = net(items)  # Do the forward pass
        loss = criterion(outputs, classes) # Calculate the loss
        loss.backward()       # Calculate the gradients with help of back propagation
        optimizer.step()      # Ask the optimizer to adjust the parameters based on the gradients
        
        # Record the correct predictions for training data
        train_total += classes.size(0)    
        _, predicted = torch.max(outputs.data, 1)
        train_correct += (predicted == classes.data).sum()

        print ('Epoch %d/%d, Iteration %d/%d, Loss: %.4f' 
               %(epoch+1, num_epochs, i+1, len(train_ds)//batch_size, loss.data[0]))

    net.eval()                 # Put the network into evaluation mode
    
    # Book keeping
    # Record the loss
    train_loss.append(loss.data[0])

    # What was our train accuracy?
    train_accuracy.append((100 * train_correct / train_total))
    
    # How did we do on the test set (the unseen set)
    # Record the correct predictions for test data
    test_items = torch.FloatTensor(test_ds.data.values[:, 0:4])
    test_classes = torch.LongTensor(test_ds.data.values[:, 4])

    outputs = net(Variable(test_items))
    loss = criterion(outputs, Variable(test_classes))
    test_loss.append(loss.data[0])
    _, predicted = torch.max(outputs.data, 1)
    total = test_classes.size(0)
    correct = (predicted == test_classes).sum()
    test_accuracy.append((100 * correct / total))<jupyter_output><empty_output><jupyter_text>Plot loss vs iterations<jupyter_code>fig = plt.figure(figsize=(12, 8))
plt.plot(train_loss, label='train loss')
plt.plot(test_loss, label='test loss')
plt.title("Train and Test Loss")
plt.legend()
plt.show()
fig = plt.figure(figsize=(12, 8))
plt.plot(train_accuracy, label='train accuracy')
plt.plot(test_accuracy, label='test accuracy')
plt.title("Train and Test Accuracy")
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Savign the model to disk, and loading it back<jupyter_code>torch.save(net.state_dict(), "./2.model.pth")
net2 = IrisNet(4, 100, 50, 3)
net2.load_state_dict(torch.load("./2.model.pth"))
output = net2(Variable(torch.FloatTensor([[5.1, 3.5, 1.4, 0.2]])))
_, predicted_class = torch.max(output.data, 1)
print('Predicted class: ', predicted_class.numpy()[0])
print('Expected class: ', 0 )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1325180097168705, 'fraction_numerical': 0.020438934494890267, 'mean_word_length': 3.3135838150289016, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '552652', 'license': 'MIT', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1938, 'n_words': 453, 'path': '2.training.your.first.neural.network.ipynb', 'repo_name': 'Masum06/Deep-learning-with-PyTorch-video', 'size': '5973'}