Keras: loss function parameters problem

Created on 13 Dec 2019  路  12Comments  路  Source: keras-team/keras

model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
while using codes above, I can't set parameters of categorical_crossentropy. how can i assign a value to label_smoothing so that i can use label_smoothing to improve my model performance?

support

Most helpful comment

Let me try to give a more complete example @pengpaiSH @Kantshun :

def cc_loss_w_smoothing(y_true, y_pred):
    label_smoothing = 0.1
    return keras.losses.categorical_crossentropy(y_true, y_pred, label_smoothing=label_smoothing )

model.compile(
    loss=cc_loss_w_smoothing,
    optimizer="adam",
    metrics=["accuracy"]
)

All 12 comments

Instead of setting the loss to loss="categorical_crossentropy", you can set the loss function like this: loss=keras.losses.categorical_crossentropy(label_smoothing=somevalue)

You can read more here.

I tried, not work. @am1tyadav
image

You can use partial from functools

from functools import partial
loss_func = partial(categorical_crossentropy, label_smoothing=somevalue)

This should work.

TypeError: binary_crossentropy() got an unexpected keyword argument 'label_smoothing'

Let me try to give a more complete example @pengpaiSH @Kantshun :

def cc_loss_w_smoothing(y_true, y_pred):
    label_smoothing = 0.1
    return keras.losses.categorical_crossentropy(y_true, y_pred, label_smoothing=label_smoothing )

model.compile(
    loss=cc_loss_w_smoothing,
    optimizer="adam",
    metrics=["accuracy"]
)
def cc_loss_w_smoothing(y_true, y_pred):
    label_smoothing = 0.1
    return keras.losses.categorical_crossentropy(y_true, y_pred, label_smoothing=label_smoothing )

Thank you !!! I tried exactly as you suggested. It worked perfectly after I updated Keras.

@am1tyadav your method works quite well!! thank you very much.

@am1tyadav your method can be used to train a model, but the model can't be loaded later.
image

@am1tyadav i successfully trained my model with your method, then i save it. error raise when i loaded it.

That's correct @Kantshun . When using a custom loss function, you will not be able to use the keras.models.load_model function to load a full model file. Instead, you will have to save and load weights of the model. I'll extend my example from above assuming you have already trained a model:

model.save_weights("filename")

# Now we will load the weights after creating a new model instance
model = keras.models.Sequential([
  # Your model architecture etc.
])

model.load_weights("filename")

@am1tyadav your method seems not work for my case. the architecture of my network looks like picture below:
image
my model is not a sequentail model.
keras is not as simple as i expected.

@Kantshun You don't have to use Sequential class, you can use the Model class

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amityaffliction picture amityaffliction  路  3Comments

somewacko picture somewacko  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments

LuCeHe picture LuCeHe  路  3Comments