Deep learning could predict within the trained data range but couldn't predict outside the range

Viewed 16

I wrote a deep learning code with PyTorch to predict the y=x^2 function

I've trained the model in the [0,5] range.

I've tried different activation functions, 1 to 5 hidden layers, 2 to 50 neurons for each hidden layer, 200 to 15000 epochs, 50 and 100 batch sizes, different values for learning rate, and 200 to 2000 training samples.

But, in every experiment that I did, the code could predict the function in the trained range but became a horizontal line outside the trained range.

Fig

What is the wrong with my code?

import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from torch.utils.data import Dataset
from torch.utils.data import DataLoader

x = np.linspace(0, 5, num=2000)
x = torch.tensor(x).unsqueeze(1)

y = x**2

class Dataset(Dataset):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def __len__(self):
        return len(self.x)
    
    def __getitem__(self,index):
        return self.x[index], self.y[index]

dataset = Dataset(x, y)

train_loader = DataLoader(dataset = dataset, 
                         batch_size = 50,
                         shuffle = True)

class Model(nn.Module):
    def __init__(self, input_features, hidden_layers, output_features):
        super().__init__()
        self.fc1 = nn.Linear(input_features, hidden_layers)
        self.fc2 = nn.Linear(hidden_layers, hidden_layers)
        self.fc3 = nn.Linear(hidden_layers, hidden_layers)
        self.fc4 = nn.Linear(hidden_layers, hidden_layers)
        self.fc5 = nn.Linear(hidden_layers, output_features)
        self.relu = nn.ReLU()
        self.tanh = nn.Tanh()
        
    def forward(self, x):
        out = self.fc1(x)
        out = self.tanh(out)
        out = self.fc2(out)
        out = self.tanh(out)
        out = self.fc3(out)
        out = self.tanh(out)
        out = self.fc4(out)
        out = self.tanh(out)
        out = self.fc5(out)
        return out

model = Model(1, 50, 1)
criterian = nn.L1Loss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.005)

epochs = 15000
for epoch in range(epochs):
    for inputs, labels in train_loader:
        inputs = inputs.float()
        labels = labels.float()

        outputs = model(inputs)
        loss = criterian(outputs, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        print(epoch, loss)

x_pred = np.linspace(-10, 10)
x_pred = torch.tensor(x_pred).unsqueeze(1).float()
y_pred = model(x_pred)
plt.plot(x_pred.detach().numpy(), y_pred.detach().numpy());
0 Answers
Related