I am trying the predict prices and I used PyTorch and Skorch to build a neural network for the predictions. I found this helpful article that shows how to do it for classification: https://towardsdatascience.com/pycaret-skorch-build-pytorch-neural-networks-using-minimal-code-57079e197f33#:~:text=Yes%2C%20you%20heard%20it%20right,which%20is%20what%20PyCaret%20expects!. I changed the code such that it now runs for regression. However, the performance measures all output zero. Can someone help me out?
This is the code I currently have:
`from skorch import NeuralNetRegressor
from skorch.helper import DataFrameTransformer
from skorch.callbacks import EarlyStopping
target = "Price"
exp = setup(data = train,
test_data = test,
target = target)
class RegressorModule(nn.Module):
def __init__(
self,
input_size= 13,
num_units=150,
num_units_d1=100,
num_units_d2=50,
nonlin=F.relu,
):
super(RegressorModule, self).__init__()
self.num_units = num_units
self.nonlin = nonlin
self.dense0 = nn.LazyLinear(input_size, num_units)
self.dense1 = nn.LazyLinear(num_units, num_units_d1)
self.dense2 = nn.LazyLinear(num_units_d1, num_units_d2)
self.output = nn.LazyLinear(num_units_d2, 1)
def forward(self, X, **kwargs):
X = self.nonlin(self.dense0(X))
X = self.nonlin(self.dense1(X))
X = self.nonlin(self.dense2(X))
X = self.output(X)
return X
def training_step(self, batch, **kwargs):
data, target = batch
predictions = self.forward(data)
loss = F.mse_loss(predictions, target)
rmse = torch.sqrt(loss)
self.log('train_rmse', rmse, on_step=True, on_epoch=True, prog_bar=True)
return loss
class MyNet(NeuralNetRegressor):
def fit(self, X, y):
if y.ndim == 1:
y = y.values.reshape(-1, 1)
return super().fit(X, y)
net_regr = MyNet(
module=RegressorModule(input_size=13),
max_epochs=20,
lr=0.1,
train_split=None,
)
nn_pipe = Pipeline(
[
("transform", DataFrameTransformer()),
("net", net_regr),
]
)
skorch_model = exp.create_model(nn_pipe)`
and this image shows the output.