0
$\begingroup$

My neural network is not giving the expected output after training in Python. Is there any error in the code? Is there any way to reduce the mean squared error (MSE)?

I tried to train (Run the program) the network repeatedly but it is not learning, instead it is giving the same MSE and output.

Here is the Data I used:

https://drive.google.com/open?id=1GLm87-5E_6YhUIPZ_CtQLV9F9wcGaTj2

Here is my code:

#load and evaluate a saved model
from numpy import loadtxt
from tensorflow.keras.models import load_model

# load model
model = load_model('ANNnew.h5')
# summarize model.
model.summary()
#Model starts
import numpy as np
import pandas as pd 
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.models import Sequential
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# Importing the dataset
X = pd.read_excel(r"C:\filelocation\Data.xlsx","Sheet1").values
y = pd.read_excel(r"C:\filelocation\Data.xlsx","Sheet2").values

# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.08, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Initialising the ANN
model = Sequential()

# Adding the input layer and the first hidden layer
model.add(Dense(32, activation = 'tanh', input_dim = 4))

# Adding the second hidden layer
model.add(Dense(units = 18, activation = 'tanh'))

# Adding the third hidden layer
model.add(Dense(units = 32, activation = 'tanh'))

#model.add(Dense(1))
model.add(Dense(units = 1))

# Compiling the ANN
model.compile(optimizer = 'adam', loss = 'mean_squared_error')

# Fitting the ANN to the Training set
model.fit(X_train, y_train, batch_size = 100, epochs = 1000)

y_pred = model.predict(X_test)
for i in range(5):
    print('%s => %d (expected %s)' % (X[i].tolist(), y_pred[i], y[i].tolist()))


plt.plot(y_test, color = 'red', label = 'Test data')
plt.plot(y_pred, color = 'blue', label = 'Predicted data')
plt.title('Prediction')
plt.legend()
plt.show()

# save model and architecture to single file
model.save("ANNnew.h5")
print("Saved model to disk")

Output

Output

$\endgroup$
  • $\begingroup$ It seems to me your case could fall as ordinal regression. Have you tried training it as classification model? It might sound weird but people did it and it worked sometime. $\endgroup$ – Yohanes Alfredo Nov 22 at 17:33
  • $\begingroup$ @YohanesAlfredo I did not tried training it as classification model.. My I know how to do that? please elaborate. $\endgroup$ – VASIH Nov 22 at 17:41
  • 1
    $\begingroup$ I mean it is simple. you just modify it to output probabilities of each classes, and use crossentropy loss instead $\endgroup$ – Yohanes Alfredo Nov 22 at 18:03
0
$\begingroup$

It looks like your loss is improving (the loss is decreasing every epoch), so I wouldn't say the network isn't training. Try looking at a training plot (which would have epoch on the x-axis and loss on the y-axis). See if there are any jumps or strange patterns in the training. If you don't see a roughly decreasing training loss, then you have a problem.

It's also possible that this model is just completely overfitting your data, leading to poor prediction on the test set. How many inputs do you have? Why are you using three hidden layers? Try reducing the size of your model and see if your prediction improves.

$\endgroup$
  • $\begingroup$ Thanks for the answer. to your information, I have 4 inputs. and 1 output. $\endgroup$ – VASIH Nov 22 at 16:28
  • $\begingroup$ Ah, please let me clarify: what is the size of your training data? $\endgroup$ – StatsSorceress Nov 22 at 17:27
  • $\begingroup$ The size of my training data is 56 $\endgroup$ – VASIH Nov 22 at 17:32
  • 1
    $\begingroup$ I think you might consider using a model with fewer parameters. $\endgroup$ – StatsSorceress Nov 22 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.