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

Pingback: Keras Nonlinear Regression - Guass3 - Shankar Ananth Asokan

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

Hi Jaydeep,

The R^2 value is for a fit between Predicted value and actual. It is only an indication for the quality of fit. It is not the R^2 for the Non Linear fit itself as R^2 is impossible for Non linear regression.

http://blog.minitab.com/blog/adventures-in-statistics-2/why-is-there-no-r-squared-for-nonlinear-regression

– Shankar

this looks pretty linear… why do you call it Non Linear Regression?

(Not sure what you are plotting… labels would be nice)

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

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

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.

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