machine learning - Keras: weighted binary crossentropy ...
stackoverflow.com › questions › 46009619Sep 02, 2017 · import tensorflow as tf import tensorflow.keras.backend as K import numpy as np # weighted loss functions def weighted_binary_cross_entropy(weights: dict, from_logits: bool = False): ''' Return a function for calculating weighted binary cross entropy It should be used for multi-hot encoded labels # Example y_true = tf.convert_to_tensor([1, 0, 0, 0, 0, 0], dtype=tf.int64) y_pred = tf.convert_to_tensor([0.6, 0.1, 0.1, 0.9, 0.1, 0.], dtype=tf.float32) weights = { 0: 1., 1: 2.
Keras: weighted binary crossentropy | Newbedev
newbedev.com › keras-weighted-binary-crossentropyKeras: weighted binary crossentropy. You can use the sklearn module to automatically calculate the weights for each class like this: # Import import numpy as np from sklearn.utils import class_weight # Example model model = Sequential () model.add (Dense (32, activation='relu', input_dim=100)) model.add (Dense (1, activation='sigmoid')) # Use binary crossentropy loss model.compile (optimizer='rmsprop', loss='binary_crossentropy', metrics= ['accuracy']) # Calculate the weights for each class ...