# 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 – https://github.com/shankarananth/Keras-Nonlinear-Regression

``````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

# 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()

# Compile model

# Fit the model
model.fit(X1, 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.plot(Y1,TestResults,'ro')
plt.title('Training Set')
plt.xlabel('Actual')
plt.ylabel('Predicted')

#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.plot(Y2,ValResults,'ro')
plt.title('Validation Set')
plt.xlabel('Actual')
plt.ylabel('Predicted')

#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”

• 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]
not
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.