I'm currently sitting on a problem, where i'm uncertain if there is not a much simpler solution.

I'm trying to train a DNN with a dataset for a classification task that should be cost sensitive. Classic literature on this kind of task use a cost weight that is constant for any kind of misclassification. My problem needs to use one of the dimensions of the input as the cost of misclassification for that single classification.

My solution would be to use TensorFlow and add the parameter i later need to a collection and then write a custom loss function where i grab the values from the collection for my cost sensitive loss.

So my question would be, does anybody know of any simpler solution, open source implementation etc.?


There are two types of classification costs: per class, and per instance.

In keras, for instance cost, we assign a cost to each training sample by feeding sample_weight to .fit. For example, if we have four training samples in rows 1, 2, 3, and 4, with misclassification costs 2.5, 1.5, 1.0, and 1.0, we feed sample_weight=[2.5, 1.5, 1.0, 1.0].

For class cost, if there are three classes 0, 1, and 2, with misclassification costs 1.0, 3.0, 1.0, we feed class_weight=[1.0, 3.0, 1.0].

Here is a step-by-step classification example in Keras.

You should feed your weights in this line:

model.fit(train_images, train_labels, epochs=5)
| improve this answer | |
  • 1
    $\begingroup$ Thank you for your answer! Sadly i need a class and instance specific weight. So a combination of both types. Sadly could also not find such a thing in pytorch but will try out how the keras training is going to help me out. $\endgroup$ – T.Tos Apr 5 '19 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.