Negative coefficient of determination

username: MLGuru
cooked: <p>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?</p>
<p>Loss is not decreasing after 1550 epochs (epochs =10000).</p>
<p>import numpy as np<br>
from sklearn.model_selection import train_test_split<br>
from sklearn.preprocessing import MinMaxScaler<br>
import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
import matplotlib.pyplot as plt<br>
from torcheval.metrics import R2Score<br>
from torch.nn import functional as F</p>
<p>…<br>
…</p>
<p>class housePricePrediction(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(housePricePrediction,self).<strong>init</strong>()<br>
<span class="hashtag-raw">#self</span>.linear = torch.nn.Linear(13,1)<br>
self.linear1 = torch.nn.Linear(13,20)<br>
self.linear2 = torch.nn.Linear(20,30)<br>
self.linear3 = torch.nn.Linear(30,20)<br>
self.linear4 = torch.nn.Linear(20,1)<br>
…<br>
…<br>
def forward(self,x):</p>
<h1><a name="x-selflinearx-1" class="anchor" href="#x-selflinearx-1"></a>x = self.linear(x)</h1>
<pre><code>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
</code></pre>
<p>criterion =nn.MSELoss()<br>
optimizer = torch.optim.Adam(model.parameters())<br>
epochNum = 10000<br>
learningRate = 0.001</p>
<p>…<br>
…</p>
<p>finalLoss =<span class="chcklst-box fa fa-square-o fa-fw"></span><br>
model.train()<br>
for epoch in range(epochNum):<br>
epoch = epoch + 1<br>
optimizer.zero_grad()<br>
predicted = model(X_train_tensor)<br>
loss = criterion(predicted,y_train_tensor.float())<br>
finalLoss.append(loss)<br>
loss.backward()<br>
optimizer.step()<br>
if(epoch%30==1):<br>
print(“Epoch{},Loss{}”.format(epoch,round(loss.item(),2)))</p>
<p>…<br>
…</p>
<p>Epoch1,Loss539.16<br>
Epoch31,Loss505.4<br>
Epoch61,Loss356.7<br>
Epoch91,Loss97.97<br>
Epoch121,Loss85.39<br>
Epoch151,Loss83.51<br>
Epoch181,Loss82.28<br>
Epoch211,Loss81.39<br>
Epoch241,Loss80.75<br>
Epoch271,Loss80.29<br>
Epoch301,Loss79.95<br>
Epoch331,Loss79.7<br>
Epoch361,Loss79.5<br>
Epoch391,Loss79.35<br>
Epoch421,Loss79.22<br>
Epoch451,Loss79.11<br>
Epoch481,Loss79.02<br>
Epoch511,Loss78.94<br>
Epoch541,Loss78.87<br>
Epoch571,Loss78.81<br>
Epoch601,Loss78.76<br>
Epoch631,Loss78.7<br>
Epoch661,Loss78.66<br>
Epoch691,Loss78.61<br>
Epoch721,Loss78.56<br>
Epoch751,Loss78.52<br>
Epoch781,Loss78.48<br>
Epoch811,Loss78.44<br>
Epoch841,Loss78.39<br>
Epoch871,Loss78.35<br>
Epoch901,Loss78.28<br>
Epoch931,Loss78.16<br>
Epoch961,Loss77.98<br>
Epoch991,Loss77.78<br>
Epoch1021,Loss77.61<br>
Epoch1051,Loss77.48<br>
Epoch1081,Loss77.38<br>
Epoch1111,Loss77.31<br>
Epoch1141,Loss77.24<br>
Epoch1171,Loss77.19<br>
Epoch1201,Loss77.15<br>
Epoch1231,Loss77.11<br>
Epoch1261,Loss77.08<br>
Epoch1291,Loss77.05<br>
Epoch1321,Loss77.03<br>
Epoch1351,Loss77.01<br>
Epoch1381,Loss77.0<br>
Epoch1411,Loss76.99<br>
Epoch1441,Loss76.98<br>
Epoch1471,Loss76.97<br>
Epoch1501,Loss76.97<br>
Epoch1531,Loss76.97<br>
Epoch1561,Loss76.96<br>
Epoch1591,Loss76.96<br>
Epoch1621,Loss76.96<br>
Epoch1651,Loss76.96<br>
Epoch1681,Loss76.96<br>
Epoch1711,Loss76.96<br>
…………………………….<br>
Epoch9781,Loss76.96<br>
Epoch9811,Loss76.96<br>
Epoch9841,Loss76.96<br>
Epoch9871,Loss76.96<br>
Epoch9901,Loss76.96<br>
Epoch9931,Loss76.96<br>
Epoch9961,Loss76.96<br>
Epoch9991,Loss76.96</p>
<p>…<br>
…</p>
<p>model.eval()<br>
with torch.no_grad():<br>
y_test_predict = model(X_test_tensor)</p>
<p>metric =R2Score()<br>
metric.update(y_test_predict.squeeze(),y_test_tensor.squeeze())<br>
metric.compute()</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>tensor(-0.0864)</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>

username: MLGuru
cooked: <p>issue was resolved after using torch.squeeze (loss = criterion(torch.squeeze(predicted.float()),y_train_tensor.float())  )</p>