Zero validation accuracy when training with model.fit

7,498 views
Skip to first unread message

Tomi P. Maila

unread,
Jan 27, 2016, 12:34:43 AM1/27/16
to Keras-users
Hi,

I'm relatively new to Keras and trying to train a neural net to classify a set of very noisy data. I'm expecting maybe correct classifications to be just slightly above random, which would be sufficient outcome. I've tested Keras for some other use cases and everything seemed to work as expected. However for this use case I get validation accuracy of zero as the output of the training. I couldn't find anywhere what would this actually mean? For example trying to train a classifier with categorical_crossentropy as loss function and rmsprop as optimizer that would classify into four classes I get an output along the lines of the following:

model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

model.fit(X_train, y_train, nb_epoch=3, batch_size=16, validation_split=0.1, show_accuracy=True, verbose=1)



Train on 19645 samples, validate on 2183 samples

Epoch 1/3

19645/19645 [==============================] - 4s - loss: 1.3710 - acc: 0.2767 - val_loss: 1.7293 - val_acc: 0.0000e+00

Epoch 2/3

19645/19645 [==============================] - 5s - loss: 1.3690 - acc: 0.2751 - val_loss: 1.6536 - val_acc: 0.0000e+00

Epoch 3/3

19645/19645 [==============================] - 5s - loss: 1.3685 - acc: 0.2771 - val_loss: 1.7274 - val_acc: 0.0000e+00


Any idea what's going on behind the scenes resulting in the 0.0000e+00 validation accuracy? Nothing appears to be NaN so I'd expect accuracy to be non-zero.


Tomi

Gowdham

unread,
Nov 10, 2016, 12:13:45 PM11/10/16
to Keras-users
Hi,

Were you able to find the issue?. I am facing the same issue and working on finding its root cause.
Any help is appreciated!

Thanks

yobo

unread,
Nov 12, 2016, 6:52:18 PM11/12/16
to Keras-users
Is your data shuffled? If not, it could it be possible that your validation set is taken from a chunk of your data where the targets are all of the same class.

Gowdhaman Sadhasivam

unread,
Nov 13, 2016, 4:19:28 PM11/13/16
to yobo, Keras-users
Yes, I shuffled the dataset before the training and also used Shuffle=True in model.fit function. I used unbalanced dataset that caused the zero validation accuracy due to chunks of same class as you mentioned. So I tried with balanced dataset and I do get zero validation accuracy on first epoch. Is this again due to the validation set of same class?

Thank you for the help.

On Sat, Nov 12, 2016 at 5:52 PM, yobo <aacl...@gmail.com> wrote:
Is your data shuffled? If not, it could it be possible that your validation set is taken from a chunk of your data where the targets are all of the same class.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/b287e9ac-fb45-40f5-ab2c-34639ccd2c57%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

jibly...@gmail.com

unread,
May 4, 2019, 9:50:45 AM5/4/19
to Keras-users
Im a little late to the reply but try experimenting with these two activation functions in your final dense layer: sigmoid and softmax. I believe softmax would suit your categorical_crossentropy needs but try both. I say this because your accuracy is stagnant. 

pruthirajj...@gmail.com

unread,
Aug 9, 2019, 12:23:19 PM8/9/19
to Keras-users
Hi,

Let me give you a brief idea about how i solved it.
I was reading the data from pandas (pd.read_csv ('')) and converting to array. it was shoing the same o/p.But after lot of try i changed it to numpy(np.loadtext()) and it started working fine .

Charles John

unread,
Jun 30, 2022, 4:36:42 PM6/30/22
to Keras-users
This was good! Thanks!

Ahmed Baahmed

unread,
Apr 9, 2024, 9:17:30 AMApr 9
to Keras-users
Hey!

I am grappling with the same issue as you. Specifically, I am focusing on anomaly detection in time series data (multivariate). Although I am relatively new to Keras, I'm attempting to train a neural network to classify a dataset, categorizing time series as 1 if it is an anomaly and 0 otherwise. I've previously used Keras for other tasks, and everything appeared to function correctly. However, in this particular case, I am encountering a validation accuracy of zero after training. I have been unable to find any explanation for this. For instance, when attempting to train a classifier with "categorical_crossentropy" as the loss function and "adam" as the optimizer to classify into four classes, the output resembles the following:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense

cnn = Sequential([
    Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=(length_subsequences, 1)),
    MaxPooling1D(pool_size=2),
   
    Conv1D(filters=256, kernel_size=3, activation='relu'),
    MaxPooling1D(pool_size=2),
    Flatten(),
    Dense(64, activation='relu'),
    Dense(5000, activation='softmax')  # Binary classification, so sigmoid activation
])

from tensorflow.keras.optimizers import Adam
# Part 1: Compiling the CNN
cnn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Reshape X_train to match the input shape expected by the model
X_train_reshaped = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
X_test_reshaped = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))
# Train the model
history = cnn.fit(X_train_reshaped, y_train, epochs=100, batch_size=32, validation_split=0.2)
Reply all
Reply to author
Forward
0 new messages