Non Linear Regression Example with Keras and Tensorflow Backend

New! Google Colab version of this code is available in this link. No need to install any software to run code. Download these files (training.csv and validation.csv) and upload when Colab requests for it.

Github Repository of Colab Code is available in this link.

For post on Keras Nonlinear Regression – Guass3 function click on this link


This post is about using Keras to do non linear fitting. The simplicity of Keras made it possible to quickly try out some neural network model without deep knowledge of Tensorflow.

The data for fitting was generated using a non linear continuous function. It has five inputs and one output. Both the training set and validation set have around 500 data points.

Y = SIN(A) x EXP(B) + COS(C x C) + POWER(D,5) – TANH(E)

I realized that adding too many hidden layers worsened the fit. Looks like for continuous functions, one hidden layer with sufficient number of nodes and good choice of activation function is sufficient. I chose hyperbolic tangent (tanh) for activation function and adam for optimizer. The results were pretty good but required some good number of iterations.

I plan to compare this with other regression algorithms available in Azure Machine Learning.

Complete code available on Github –

from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
import numpy
%matplotlib inline

#Red data from csv file for training and validation data
TrainingSet = numpy.genfromtxt("training.csv", delimiter=",", skip_header=True)
ValidationSet = numpy.genfromtxt("validation.csv", delimiter=",", skip_header=True)

# split into input (X) and output (Y) variables
X1 = TrainingSet[:,0:5]
Y1 = TrainingSet[:,5]

X2 = ValidationSet[:,0:5]
Y2 = ValidationSet[:,5]

# create model
model = Sequential()
model.add(Dense(20, activation="tanh", input_dim=5, kernel_initializer="uniform"))
model.add(Dense(1, activation="linear", kernel_initializer="uniform"))

# Compile model
model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])

# Fit the model, Y1, epochs=100, batch_size=10,  verbose=2)

# Calculate predictions
PredTestSet = model.predict(X1)
PredValSet = model.predict(X2)

# Save predictions
numpy.savetxt("trainresults.csv", PredTestSet, delimiter=",")
numpy.savetxt("valresults.csv", PredValSet, delimiter=",")

#Plot actual vs predition for training set
TestResults = numpy.genfromtxt("trainresults.csv", delimiter=",")
plt.title('Training Set')

#Compute R-Square value for training set
TestR2Value = r2_score(Y1,TestResults)
print("Training Set R-Square=", TestR2Value)

#Plot actual vs predition for validation set
ValResults = numpy.genfromtxt("valresults.csv", delimiter=",")
plt.title('Validation Set')

#Compute R-Square value for validation set
ValR2Value = r2_score(Y2,ValResults)
print("Validation Set R-Square=",ValR2Value)
Python Code



The results were pretty good



8 thoughts on “Non Linear Regression Example with Keras and Tensorflow Backend

  • Pingback:Keras Nonlinear Regression - Guass3 - Shankar Ananth Asokan

  • March 21, 2018 at 10:22 pm

    Can you use R^2 incase of non linear regression?Isn’t that not advised?

  • January 24, 2018 at 8:12 pm

    this looks pretty linear… why do you call it Non Linear Regression?
    (Not sure what you are plotting… labels would be nice)

    • May 5, 2018 at 12:59 pm

      Hi Niklas,
      The function is itself Non Linear. The fit is indeed confusing (Predicted Vs Actual). I have labelled them now. Thanks for your feedback.

      — Shankar

    • May 14, 2018 at 12:19 am

      I’ll also include some standard non linear functions (Guass3, Fourier Transform..) as the level is non linear is low in the above example.

      – Shankar

  • April 5, 2017 at 2:46 am

    I think there’s a bug in your code. Your inputs are actually:
    X1 = TrainingSet[:,0:4]
    X1 = TrainingSet[:,0:5]
    Since you’re including the outputs in the fit of the outputs, I think it’s just setting the weights on the inputs to zero, causing artificially high r^2 values.

    • May 5, 2018 at 12:21 pm

      Hi Adam,
      Thanks for your comments. I checked the data, fit parameters and it is correct. The r^2 value looks perfect as too many iterations are executed. If the iterations are limited to 100 for example the r^2 value is 0.87.

      The r^2 is not for the fit itself. It is for the fit between the actual data and predicted, and indirect indication of accuracy of fit.

      – Shankar


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.