Search code examples
numpytensorflowneural-networklinear-regressionpredict

Simple neural network for a linear regression problem (one input, one output) behaves the exact opposite way it should, why? (TensorFlow)


I try to solve a simple linear regression problem with a neural network (preferably in TensorFlow) where I have the data of the population (which is decreasing continuously) for 17 years.

When I make predictions there are 2 problems:

  1. The predicted population is far away from the last one (the last 'loss' is 642388.5000).
  2. The population is increasing as the years pass when it should clearly decrease.

What can I do to reduce the error? And why the model predicts that the population is increasing over time when it's clearly not?

The code I wrote:

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.keras.models import Model

X = np.arange(2002, 2019)
y = np.linspace(21730496, 19473970, 17).astype(float)

input1 = Input(shape=(1,))
l1 = Dense(10, activation='relu')(input1)
l2 = Dense(50, activation='relu')(l1)
l3 = Dense(50, activation='relu')(l2)
out = Dense(1)(l3)

model = Model(inputs=input1, outputs=[out])
model.compile(
    optimizer='adam',
    loss=['mae']
)

history = model.fit(X, y, epochs=500, batch_size=10)

model.predict([2019., 2020., 2021.])

Solution

  • I had to rescale the input and the output data too to be in the [0,1] interval. Here is the modified code, which works exactly as I expected:

    import numpy as np
    import matplotlib.pyplot as plt
    from tensorflow.python.keras.layers import Input, Dense
    from tensorflow.python.keras.models import Model
    from sklearn.preprocessing import MinMaxScaler
    
    X = np.arange(2002, 2019)
    y = np.linspace(21730496, 19473970, 17).astype(float)
    
    X_scalar = MinMaxScaler()
    X_scalar.fit(X.reshape(-1, 1))
    X_scaled = X_scalar.transform(X.reshape(-1,1))
    
    y_scalar = MinMaxScaler()
    y_scalar.fit(y.reshape(-1, 1))
    y_scaled = y_scalar.transform(y.reshape(-1,1))
    
    input1 = Input(shape=(1,))
    l1 = Dense(10, activation='relu')(input1)
    l2 = Dense(50, activation='relu')(l1)
    l3 = Dense(50, activation='relu')(l2)
    out = Dense(1)(l3)
    
    model = Model(inputs=input1, outputs=[out])
    model.compile(
        optimizer='adam',
        loss=['mae']
    )
    
    history = model.fit(X_scaled, y_scaled, epochs=500, batch_size=10)
    
    X_test = np.array([2019., 2020., 2021.])
    X_test_scaled = X_scalar.transform(X_test.reshape(-1,1))
    
    print(y_scalar.inverse_transform(model.predict(X_test_scaled)))