Search code examples
pythonkeraskeras-layer

Keras: ValueError while doing predictions and accuracy is zero


I wrote the following model in Keras but while doing predictions, I am encountering ValueError (stated after the code). I looked at other questions on StackOverflow but only found this one but I wasn't able to understand what the answer is saying.

I am trying to build a network which takes X as input and has a hidden of 200 neurons and finally, a sigmodial neuron to predict the output class. The objective is binary classification.

The input to the model is (n x 400) as X and (n, 1) as Y. Both X and Y are numpy arrays. The code and the error message are as follows. Also, results of the neural network are : Loss = Infinite and Accuracy = 0.0 which means something from my side is clearly wrong.

def create_baseline():
    # create model
    model = Sequential()
    model.add(Dense(200, input_dim=400, kernel_initializer='normal', activation='relu'))
    model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

I train the model as follows:

estimator = KerasClassifier(build_fn=create_baseline, nb_epoch=5, batch_size=5, verbose=1)
estimator.fit(X, Y)

I try to predict using the following:

estimator.predict(X[0])

I am getting the following error:

ValueError: Error when checking : expected dense_34_input to have shape (None, 400) but got array with shape (400, 1)

Edit 1 : The training data X has 400 features (which are learnt from word2vec for the document). Y is a label which is only 0 and 1. I read the training file into a pandas dataframe and then use dataframe.values to get X and Y as numpy array.

Edit 2: Based on the answer below, I predicted from my model as estimator.predict(train_X_vals[0].reshape(1,len(train_X_vals[0]))) and I get the following output: array([[ nan]], dtype=float32)

Edit 3: I predict values for 1000 X's from the training set and I get all these as nan.

Edit 4: I have about 250K training samples. Upon using all these samples, I am getting loss = nan and all predictions as nan. But when I take a sample of 2000 samples, I start getting good values.

Edit 5: It seems my model can't process more than 2500 training samples. I tried increasing training size and on 3000 and more, everything becomes nan.


Solution

  • Regarding the error that you get- when you specify the input dimension, you specify the number of columns, which is 400 in your case. However, x[0] is just a vector, so instead of having dimensions of the form (None, 400), its dimension is (400, 1). For example, if you define

    x=np.random.rand(20,400)
    

    The result of x[0].shape is (400L,).

    So you need to reshape your x[0] accordingly, for example by doing x[0].reshape(1,len(x[0])).