# Negative coefficient of determination

I am new to deep learning and Pytorch, trying to develop a model to predict house price. Below are code excerpts. Why am I getting negative R2 value?

Loss is not decreasing after 1550 epochs (epochs =10000).

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
from torcheval.metrics import R2Score
from torch.nn import functional as F

class housePricePrediction(nn.Module):
def init(self):
super(housePricePrediction,self).init()
#self.linear = torch.nn.Linear(13,1)
self.linear1 = torch.nn.Linear(13,20)
self.linear2 = torch.nn.Linear(20,30)
self.linear3 = torch.nn.Linear(30,20)
self.linear4 = torch.nn.Linear(20,1)

def forward(self,x):

# x = self.linear(x)

``````x= F.leaky_relu(self.linear1(x))
x= F.leaky_relu(self.linear2(x))
x= F.leaky_relu(self.linear3(x))
x= self.linear4(x)
return x
``````

criterion =nn.MSELoss()
epochNum = 10000
learningRate = 0.001

finalLoss =[]
model.train()
for epoch in range(epochNum):
epoch = epoch + 1
predicted = model(X_train_tensor)
loss = criterion(predicted,y_train_tensor.float())
finalLoss.append(loss)
loss.backward()
optimizer.step()
if(epoch%30==1):
print(“Epoch{},Loss{}”.format(epoch,round(loss.item(),2)))

Epoch1,Loss539.16
Epoch31,Loss505.4
Epoch61,Loss356.7
Epoch91,Loss97.97
Epoch121,Loss85.39
Epoch151,Loss83.51
Epoch181,Loss82.28
Epoch211,Loss81.39
Epoch241,Loss80.75
Epoch271,Loss80.29
Epoch301,Loss79.95
Epoch331,Loss79.7
Epoch361,Loss79.5
Epoch391,Loss79.35
Epoch421,Loss79.22
Epoch451,Loss79.11
Epoch481,Loss79.02
Epoch511,Loss78.94
Epoch541,Loss78.87
Epoch571,Loss78.81
Epoch601,Loss78.76
Epoch631,Loss78.7
Epoch661,Loss78.66
Epoch691,Loss78.61
Epoch721,Loss78.56
Epoch751,Loss78.52
Epoch781,Loss78.48
Epoch811,Loss78.44
Epoch841,Loss78.39
Epoch871,Loss78.35
Epoch901,Loss78.28
Epoch931,Loss78.16
Epoch961,Loss77.98
Epoch991,Loss77.78
Epoch1021,Loss77.61
Epoch1051,Loss77.48
Epoch1081,Loss77.38
Epoch1111,Loss77.31
Epoch1141,Loss77.24
Epoch1171,Loss77.19
Epoch1201,Loss77.15
Epoch1231,Loss77.11
Epoch1261,Loss77.08
Epoch1291,Loss77.05
Epoch1321,Loss77.03
Epoch1351,Loss77.01
Epoch1381,Loss77.0
Epoch1411,Loss76.99
Epoch1441,Loss76.98
Epoch1471,Loss76.97
Epoch1501,Loss76.97
Epoch1531,Loss76.97
Epoch1561,Loss76.96
Epoch1591,Loss76.96
Epoch1621,Loss76.96
Epoch1651,Loss76.96
Epoch1681,Loss76.96
Epoch1711,Loss76.96
…………………………….
Epoch9781,Loss76.96
Epoch9811,Loss76.96
Epoch9841,Loss76.96
Epoch9871,Loss76.96
Epoch9901,Loss76.96
Epoch9931,Loss76.96
Epoch9961,Loss76.96
Epoch9991,Loss76.96

model.eval()