# Regression Neural Network using tflearn

I have a script which I wrote using python and tflearn. I created a regression neural network model which takes in chemical analysis of wine as input and predicts a score out of 10.

The problem I have is that the prediction of my model is very bad. I'm also new to tflearn, I have only coded classification neural networks(twice). So, I'm a complete beginner in coding regression and in using tflearn.

Code:

import pandas as pd
import numpy as np
import tflearn
from tflearn.layers.core import input_data, fully_connected, dropout
from tflearn.layers.estimator import regression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

def preprocess():

data_source_red = 'F:\Gautam\...\winequality-red.csv'
data_source_white = 'F:\Gautam\...\winequality-white.csv'

data = pd.concat([data_red, data_white])
data = data.dropna(inplace=False)

x = data[data.columns[0:11]].values

y = data[data.columns[11]].values

sc = StandardScaler()
x = sc.fit_transform(x)

y = np.expand_dims(y, -1)

x = np.float32(x)
y = np.float32(y)

return (x, y)

x, y = preprocess()

train_x, test_x, train_y, test_y = train_test_split(x, y, test_size = 0.2)

network = input_data(shape=[None, 11], name='Input_layer')

network = fully_connected(network, 5, activation='relu', name='Hidden_layer_1')

network = fully_connected(network, 1, activation='linear', name='Output_layer')

network = regression(network, batch_size=64, optimizer='sgd', learning_rate=0.2, loss='mean_square', metric='R2')

model = tflearn.DNN(network)

model.fit(train_x, train_y, show_metric=True, run_id='wine_regression', validation_set=0.1, n_epoch=10)

result = model.evaluate(test_x, test_y)
print('Accuracy is %0.2f%%' % (result[0] * 100))

pred_y = model.predict(test_x)

plt.plot(test_y, color = 'red', label = 'Real data')
plt.plot(pred_y, color = 'blue', label = 'Predicted data')
plt.title('Prediction')
plt.legend()
plt.show()


Again, the prediction is very bad. Also, the loss and R2 values go hectic. Sometimes, the loss is low(6.45) and sometimes very high(23445.45). The same goes for R2 value too. Sometimes, R2 value goes above 1.0

Even if the loss is minimum(0.1) and R2 as 0.95, the graph shows that the actual value varies lot from the predicted values.

What mistake am I doing? Why is my prediction very bad? And why are the values R2 and loss too high sometimes and too low sometimes?

Am I missing something here? This is my first regression neural network, so I don't know much about this. I hope that my question is clear. Thanks.

• You need to try to be a little more focused on one of the problems you are having. To answer your question in full, someone would need to almost write a full article, and there even books dedicated to the topics. Here are some keywords for you to read about, which should start giving you ideas about debugging and solving your issues: batch normalisation, overfitting, learning rate scheduling, loss metrics, tensorboard'. – n1k31t4 May 7 '18 at 15:50
• Yes. I get it. I didn't ask anything specific. But for now, can you please explain why R2 value changes a lot. – Gautam J May 7 '18 at 15:55
• Your optimisation algorithm is not consistently converging and is doing so in a 'noisy' manner. Try playing with the learning rate and using batch normalisation. You could also reassess the input data and see if the 'signal' is really consistent, i.e. why do you expect a smooth improvement in R2 during training? Perhaps you could do some further preprocessing on the data before training a model. – n1k31t4 May 7 '18 at 16:42