Created
September 15, 2022 05:01
-
-
Save aryan-jadon/0aadb3b8a2ea32075bf26beb835ea303 to your computer and use it in GitHub Desktop.
NeturalNetwork.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
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 | |
import numpy as np | |
from torch.autograd import Variable | |
from sklearn import preprocessing | |
from torch.utils.data import DataLoader, TensorDataset | |
import tqdm | |
import time | |
# Define the PyTorch Neural Network | |
class Net(nn.Module): | |
def __init__(self, in_count, out_count): | |
super(Net, self).__init__() | |
self.fc1 = nn.Linear(in_count, 50) | |
self.fc2 = nn.Linear(50, 25) | |
self.fc3 = nn.Linear(25, out_count) | |
self.softmax = nn.Softmax(dim=1) | |
def forward(self, x): | |
x = F.relu(self.fc1(x)) | |
x = F.relu(self.fc2(x)) | |
return self.softmax(self.fc3(x)) | |
df = pd.read_csv( | |
"https://data.heatonresearch.com/data/t81-558/iris.csv", | |
na_values=['NA', '?']) | |
le = preprocessing.LabelEncoder() | |
x = df[['sepal_l', 'sepal_w', 'petal_l', 'petal_w']].values | |
y = le.fit_transform(df['species']) | |
species = le.classes_ | |
# Split into validation and training sets | |
x_train, x_test, y_train, y_test = train_test_split( | |
x, y, test_size=0.25, random_state=42) | |
# Numpy to Torch Tensor | |
x_train = torch.tensor(x_train, device=device, dtype=torch.float32) | |
y_train = torch.tensor(y_train, device=device, dtype=torch.long) | |
x_test = torch.tensor(x_test, device=device, dtype=torch.float32) | |
y_test = torch.tensor(y_test, device=device, dtype=torch.long) | |
# Create datasets | |
BATCH_SIZE = 16 | |
dataset_train = TensorDataset(x_train, y_train) | |
dataloader_train = DataLoader(dataset_train,\ | |
batch_size=BATCH_SIZE, shuffle=True) | |
dataset_test = TensorDataset(x_test, y_test) | |
dataloader_test = DataLoader(dataset_test,\ | |
batch_size=BATCH_SIZE, shuffle=True) | |
# Create model | |
model = Net(x.shape[1],len(species)).to(device) | |
loss_fn = nn.CrossEntropyLoss()# cross entropy loss | |
optimizer = torch.optim.Adam(model.parameters(), lr=0.01) | |
es = EarlyStopping() | |
epoch = 0 | |
done = False | |
while epoch<1000 and not done: | |
epoch += 1 | |
steps = list(enumerate(dataloader_train)) | |
pbar = tqdm.tqdm(steps) | |
model.train() | |
for i, (x_batch, y_batch) in pbar: | |
y_batch_pred = model(x_batch.to(device)) | |
loss = loss_fn(y_batch_pred, y_batch.to(device)) | |
optimizer.zero_grad() | |
loss.backward() | |
optimizer.step() | |
loss, current = loss.item(), (i + 1)* len(x_batch) | |
if i == len(steps)-1: | |
model.eval() | |
pred = model(x_test) | |
vloss = loss_fn(pred, y_test) | |
if es(model,vloss): done = True | |
pbar.set_description(f"Epoch: {epoch}, tloss: {loss}, vloss: {vloss:>7f}, EStop:[{es.status}]") | |
else: | |
pbar.set_description(f"Epoch: {epoch}, tloss {loss:}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment