python - Why does a binary Keras CNN always predict 1? -
i want build binary classifier using keras cnn. have 6000 rows of input data looks this:
>> print(x_train[0]) [[[-1.06405307 -1.06685851 -1.05989663 -1.06273152] [-1.06295958 -1.06655996 -1.05969803 -1.06382503] [-1.06415248 -1.06735609 -1.05999593 -1.06302975] [-1.06295958 -1.06755513 -1.05949944 -1.06362621] [-1.06355603 -1.06636092 -1.05959873 -1.06173742] [-1.0619655 -1.06655996 -1.06039312 -1.06412326] [-1.06415248 -1.06725658 -1.05940014 -1.06322857] [-1.06345662 -1.06377347 -1.05890365 -1.06034568] [-1.06027557 -1.06019084 -1.05592469 -1.05537518] [-1.05550398 -1.06038988 -1.05225064 -1.05676692]]] >>> print(y_train[0]) [1] and i've build cnn way:
model = sequential() model.add(convolution1d(input_shape = (10, 4), nb_filter=16, filter_length=4, border_mode='same')) model.add(batchnormalization()) model.add(leakyrelu()) model.add(dropout(0.2)) model.add(convolution1d(nb_filter=8, filter_length=4, border_mode='same')) model.add(batchnormalization()) model.add(leakyrelu()) model.add(dropout(0.2)) model.add(flatten()) model.add(dense(64)) model.add(batchnormalization()) model.add(leakyrelu()) model.add(dense(1)) model.add(activation('softmax')) reduce_lr = reducelronplateau(monitor='val_acc', factor=0.9, patience=30, min_lr=0.000001, verbose=0) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) history = model.fit(x_train, y_train, nb_epoch = 100, batch_size = 128, verbose=0, validation_data=(x_test, y_test), callbacks=[reduce_lr], shuffle=true) y_pred = model.predict(x_test) but returns following:
>> print(confusion_matrix(y_test, y_pred)) [[ 0 362] [ 0 608]] why predictions ones? why cnn perform bad? here loss , acc charts: 
it predicts 1 because of output in network. have dense layer 1 neuron, softmax activation. softmax normalizes sum of exponential of each output. since there 1 output, possible output 1.0.
for binary classifier can either use sigmoid activation "binary_crossentropy" loss, or put 2 output units @ last layer, keep using softmax , change loss categorical_crossentropy.
Comments
Post a Comment