I’m trying to make a model to predict prices using the Boston dataset from sklearn.datasets
# -*- coding: utf-8 -*-
from sklearn.datasets import load_boston
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(13, 64)
self.fc2 = nn.Linear(64, 32)
self.fc3 = nn.Linear(32, 16)
self.fc4 = nn.Linear(16,1)
def forward(self, x):
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
x = self.fc3(x)
x = F.relu(x)
return self.fc4(x)
net = Net()
class BostonData(Dataset):
__xs = np.array(0)
__ys = np.array(0)
def __init__(self, train = True):
df = load_boston()
index = int(len(df["data"]) * 0.7)
if train:
self.__xs = np.array(df["data"][0:index])
self.__ys = np.array(df["target"][0:index])
else:
self.__xs = np.array(df["data"][index:])
self.__ys = np.array(df["target"][index:])
self.__xs = (self.__xs - self.__xs.mean()) / self.__xs.std()
def __getitem__(self, index):
return self.__xs[index], self.__ys[index]
def __len__(self):
return len(self.__xs)
dset_train = BostonData(train = True)
dset_test = BostonData(train = False)
train_loader = DataLoader(dset_train, batch_size=10, shuffle=True)
test_loader = DataLoader(dset_train, batch_size=10, shuffle=True)
After training the model, the strange behavior that I’m talking is on the following part. I want to measure an error average between the true values and the predictions using the test set as follows
res = 0
with torch.no_grad():
for testbatch in test_loader:
X,y = testbatch
output = net(X.float())
for i,elem in enumerate(output):
res = res + np.abs(y[i] - elem)
print(res/len(dset_train))
This gives me no error whatsoever, but I was curious about if the model was oferfitting, so I changed the test_loader
for the train_loader
res = 0
with torch.no_grad():
for testbatch in train_loader:
X,y = testbatch
output = net(X.float())
for i,elem in enumerate(output):
res = res + np.abs(y[i] - elem)
print(res/len(dset_train))
Executing only these lines of code (I’m using Spyder as IDE) brings the same result for res/len(dset_train)
as doing it for test_loader
, which seems really strange. Is there any error that I didn’t notice?