Created
September 15, 2022 06:52
-
-
Save aryan-jadon/8dd5a7d718ffe61c5c160c00e64a1f19 to your computer and use it in GitHub Desktop.
regression.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__() | |
# We must define each of the layers. | |
self.fc1 = nn.Linear(in_count, 50) | |
self.fc2 = nn.Linear(50, 25) | |
self.fc3 = nn.Linear(25, 1) | |
def forward(self, x): | |
# In the forward pass, we must calculate all of the layers we | |
# previously defined. | |
x = F.relu(self.fc1(x)) | |
x = F.relu(self.fc2(x)) | |
return self.fc3(x) | |
# Read the MPG dataset. | |
df = pd.read_csv( | |
"https://data.heatonresearch.com/data/t81-558/auto-mpg.csv", | |
na_values=['NA', '?']) | |
cars = df['name'] | |
# Handle missing value | |
df['horsepower'] = df['horsepower'].fillna(df['horsepower'].median()) | |
# Pandas to Numpy | |
x = df[['cylinders', 'displacement', 'horsepower', 'weight', | |
'acceleration', 'year', 'origin']].values | |
y = df['mpg'].values # regression | |
# 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.float32) | |
x_test = torch.tensor(x_test,device=device,dtype=torch.float32) | |
y_test = torch.tensor(y_test,device=device,dtype=torch.float32) | |
# 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],1).to(device) | |
# Define the loss function for regression | |
loss_fn = nn.MSELoss() | |
# Define the optimizer | |
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).flatten() # | |
loss = loss_fn(y_batch_pred, y_batch) | |
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).flatten() | |
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