<jupyter_start><jupyter_text>In this notebook we will try to train a simple pytorch model on the Iris dataset<jupyter_code>import torch
import sklearn
import numpy as np
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

import torch.nn as nn
import torch.nn.functional as F

#loading iris dataset and extracting variables
iris = load_iris()
X = iris['data']
y = iris['target']
names = iris['target_names']
feature_names = iris['feature_names']

#no of targets
print(names)<jupyter_output>['setosa' 'versicolor' 'virginica']<jupyter_text>Model<jupyter_code>class Model(nn.Module):
    
    #defining model structure
    def __init__(self,input_dims):
        super(Model,self).__init__()
        self.layer1 = nn.Linear(input_dims,100)
        self.layer2 = nn.Linear(100,50)
        self.layer3 = nn.Linear(50,3)
        
    #define forward computation with activation functions
    def forward(self,x):
        x = F.relu(self.layer1(x))
        x = F.relu(self.layer2(x))
        x = F.softmax(self.layer3(x))
        
        return x
    
#instantiating our model and defining optimizer and loss fn
model = Model(4)
optimizer = torch.optim.Adagrad(model.parameters(),lr = 0.01)
loss_fn = nn.CrossEntropyLoss()
print(model)<jupyter_output>Model(
  (layer1): Linear(in_features=4, out_features=100, bias=True)
  (layer2): Linear(in_features=100, out_features=50, bias=True)
  (layer3): Linear(in_features=50, out_features=3, bias=True)
)<jupyter_text>Preparing our data<jupyter_code>scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

X_scaled = X_scaled.astype(np.float32)

X_train,X_test,y_train,y_test = train_test_split(X_scaled,y,test_size=0.2,random_state=2)

# we need to convert out dataset variables to tensors to utilize gpu computation

X_train = torch.from_numpy(X_train)
X_test = torch.from_numpy(X_test)
y_train = torch.from_numpy(y_train)
y_test = torch.from_numpy(y_test)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>EPOCHS = 100

for epoch in range(EPOCHS):
    
    y_pred = model(X_train)
    loss = loss_fn(y_pred,y_train)
    
    if epoch % 10 == 0:
        print ("Epoch #",epoch)
        print(loss.item())
        
    #Zero the gradients
    optimizer.zero_grad()
    loss.backward()#calculate gradients
    optimizer.step()#update weights<jupyter_output>Epoch # 0
1.0889053344726562
Epoch # 10
0.709537923336029
Epoch # 20
0.6378505229949951
Epoch # 30
0.6061201691627502
Epoch # 40
0.5926541090011597
Epoch # 50
0.5853510499000549
Epoch # 60
0.5807108283042908
Epoch # 70
0.5774818062782288
Epoch # 80
0.5751286745071411
Epoch # 90
0.573313295841217<jupyter_text>Predictions<jupyter_code>with torch.no_grad():
    y_test_pred = model(X_test)
accuracy_score(np.argmax(y_test_pred,axis=1),y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10407394727832935, 'fraction_numerical': 0.08045190003423486, 'mean_word_length': 4.891129032258065, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '645957', 'license': 'MIT', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1091, 'n_words': 216, 'path': 'Training_a_simple_model_with_pytorch.ipynb', 'repo_name': 'abhinav153/Deep_Learning_code', 'size': '2921'}