Skip to content Skip to sidebar Skip to footer

Non-Identical Results From String Identifier And Actual Class Names For Activations, Loss Functions, And Metrics

I have the following keras model that is working fine: model = tf.keras.Sequential( [ #first convolution tf.keras.layers.Conv2D(16, (3,3), activation='relu',

Solution 1:

When we set String Identifier for accuracy as ['acc'] or ['accuracy'], the program will choose the relevant metrics for our problem, like whether it's binary or categorical type. But when we set the actual class name, we need to be a bit more specific. So, in your case, you need to change your metrics from

tf.keras.metrics.Accuracy()

to

tf.keras.metrics.BinaryAccuracy()

Read the content of each from .Accuracy(), and .BinaryAccuracy()


Here is one dummy example to reproduce the issue and solution for a complete reference.

# Generate dummy data
np.random.seed(10)
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test  = np.random.random((800, 20))
y_test  = np.random.randint(2, size=(800, 1))

# model
model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

# compile and run
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.fit(x_train, y_train,
          epochs=10, verbose=2,
          batch_size=128, validation_data=(x_test, y_test))

With String Identifier, it will run OK. But if you change metrics to .Accuracy(), it will give you zero scores for both the train and validation part. To solve it, you need to set .BinaryAccuracy(), then things will run as expected.


Post a Comment for "Non-Identical Results From String Identifier And Actual Class Names For Activations, Loss Functions, And Metrics"