I am trying to follow this tutorial. Everything works when I copy and paste the data, but when I try to replace the dataset with a random numpy matrix, my accuracy goes to zero. It shouldn't go to zero because the Y matrix is a column in the X matrix, so in theory the training process should figure that out and the accuracy should go to 100%, but it doesn't.

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dropout, Dense
seed = 7

# load pima indians dataset
# dataset = numpy.loadtxt(path, delimiter=",")
dataset = numpy.random.rand(800, 9)

# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,  7]

# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))

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

# Fit the model
model.fit(X, Y, nb_epoch=10, batch_size=10, verbose=0)

# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

You're trying to compute the accuracy of a regression problem, i.e. the portion of examples on which the output is floating-point equal to the input. Unsurprisingly, this never happens. Pass metrics=['mean_squared_error'] instead.

  • $\begingroup$ I did this on purpose so that the X matrix and the Y matrix have a column in common. I actually made a mistake by not doing that in the example, which I adjusted now. So both the X and the Y have a column in common. I still get an accuracy of 0.00 $\endgroup$ – user1367204 Feb 1 '17 at 3:03
  • $\begingroup$ @user1367204 Okay, edited to address the next problem. $\endgroup$ – Dougal Feb 1 '17 at 3:14

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.