keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.12k stars 19.49k forks source link

Cannot access accuracy in results with keras 3 #20529

Open tanwarsh opened 1 day ago

tanwarsh commented 1 day ago

Hi, I am new to Keras and TensorFlow. I am using keras==3.6.0 and tensorflow==2.18.0. I created a sequential model and added layers to it. Here is a pseudocode of what I did:

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import Dense
from keras.layers import Flatten
from keras import metrics

def build_model(self,
                input_shape,
                num_classes,
                conv_kernel_size=(4, 4),
                conv_strides=(2, 2),
                conv1_channels_out=16,
                conv2_channels_out=32,
                final_dense_inputsize=100):
    model = Sequential()
    model.add(Conv2D(conv1_channels_out,
                     kernel_size=conv_kernel_size,
                     strides=conv_strides,
                     activation='relu',
                     input_shape=input_shape))
    model.add(Conv2D(conv2_channels_out,
                     kernel_size=conv_kernel_size,
                     strides=conv_strides,
                     activation='relu'))
    model.add(Flatten())
    model.add(Dense(final_dense_inputsize, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])

    return model

while evaluating the model, I am getting the results as shown below.

print(model.evaluate(self.data_loader.get_valid_loader(batch_size), verbose=1))
print(new_model.metrics_names)

Result:

157/157 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.0788 - loss: 2.3034
[2.3036301136016846, 0.07720000296831131]
['loss', 'compile_metrics']

Expected Result:

157/157 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.0788 - loss: 2.3034
[2.3036301136016846, 0.07720000296831131]
['loss', 'accuracy']

Is my expectation correct, or do I need to access accuracy differently? Also, the results do not change even if I change the metrics while compiling the model. Any guidance would be appreciated.

eg.

model.compile(loss="categorical_crossentropy",
                      optimizer="adam",
                      metrics=[metrics.MeanSquaredError(name='my_mse'),
                               metrics.AUC(name='my_auc'),
                               metrics.BinaryAccuracy(),
                               metrics.Accuracy(),])
sachinprasadhs commented 23 hours ago

You can get the metrics name and it's value using model.get_metrics_result() Attaching the Gist here for reference with the usage.

If you train the model, you can also get these metrics details using something like

history = model.fit()
history.history # This will be dictionary with all the metrics details
tanwarsh commented 10 hours ago

Thanks @sachinprasadhs for the quick response. This was helpful. I have one more doubt. with keras 3.6.0 code below is not working related to optmizer and I was not able to find anything related to this in documentation.

model.optimizer.get_weights()
model.optimizer.weights

How can we achieve the same functionality?