Solved – Keras Numpy Error: Why does accuracy go to zero

machine learningneural networksnumpytensorflowtheano

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
numpy.random.seed(seed)


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

Best Answer

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.