If Y_pred
is very far off from Y
, the Loss value will be very high. However, if both values are almost similar, the Loss value will be very low. Hence we need to keep a loss function which can penalize a model effectively while it is training on a dataset.
When a neural network is trying to predict a discrete value, we can consider it to be a classification model. This could be a network trying to predict what kind of animal is present in an image, or whether an email is a spam or not.
Questions tagged [loss-function]
1727 questions
15
votes
2 answers
Add class information to keras network
I am trying to figure out how I will use the label information of my dataset with Generative Adversarial Networks. I am trying to use the following implementation of conditional GANs that can be found here. My dataset contains two different image…

Jose Ramon
- 5,572
- 25
- 76
- 152
14
votes
2 answers
Minimise cost of reallocating individuals
I have individuals that belong to different categories, they are located in different
zones, these populations are expected to grow from the population value below
to the demand value.
population_and_demand_by_category_and_zone <- tibble::tribble(
…

moodymudskipper
- 46,417
- 11
- 121
- 167
14
votes
1 answer
Weighted mse custom loss function in keras
I'm working with time series data, outputting 60 predicted days ahead.
I'm currently using mean squared error as my loss function and the results are bad
I want to implement a weighted mean squared error such that the early outputs are much more…

Eldar M.
- 387
- 2
- 11
13
votes
1 answer
Tensorflow, what does from_logits = True or False mean in sparse_categorical_crossentropy of Tensorflow?
In Tensorflow 2.0,
there is a loss function called
tf.keras.losses.sparse_categorical_crossentropy(labels, targets, from_logits = False)
Can I ask you what are the differences between setting from_logits = True or False?
My guess was that when…

Won Jun Son
- 135
- 1
- 6
12
votes
2 answers
ValueError: Unknown loss function:focal_loss_fixed when loading model with my custom loss function
I designed my own loss function. However when trying to revert to the best model encountered during training with
model = load_model("lc_model.h5")
I got the following…

Revolucion for Monica
- 2,848
- 8
- 39
- 78
12
votes
2 answers
Keras load_model with custom objects doesn't work properly
Setting
As already mentioned in the title, I got a problem with my custom loss function, when trying to load the saved model. My loss looks as follows:
def weighted_cross_entropy(weights):
weights = K.variable(weights)
def loss(y_true,…

pafi
- 619
- 1
- 8
- 20
12
votes
1 answer
AttributeError: 'Tensor' has no attribute: 'backwards'
I'm posting this question for those who made the same error I did. I got this error when trying to compute my gradients:
criterion = torch.nn.CrossEntropyLoss()
loss = criterion(y_hat, y_truth)
loss.backwards()

Jacob Stern
- 3,758
- 3
- 32
- 54
11
votes
5 answers
Using weights in CrossEntropyLoss and BCELoss (PyTorch)
I am training a PyTorch model to perform binary classification. My minority class makes up about 10% of the data, so I want to use a weighted loss function. The docs for BCELoss and CrossEntropyLoss say that I can use a 'weight' for each…

clueless
- 211
- 2
- 3
- 7
11
votes
5 answers
Keras/Tensorflow: Combined Loss function for single output
I have only one output for my model, but I would like to combine two different loss functions:
def get_model():
# create the model here
model = Model(inputs=image, outputs=output)
alpha = 0.2
model.compile(loss=[mse, gse],
…

Saravanabalagi Ramachandran
- 8,551
- 11
- 53
- 102
11
votes
2 answers
How is the categorical_crossentropy implemented in keras?
I'm trying to apply the concept of distillation, basically to train a new smaller network to do the same as the original one but with less computation.
I have the softmax outputs for every sample instead of the logits.
My question is, how is the…

Eric
- 1,108
- 3
- 11
- 25
10
votes
1 answer
TensorFlow 2 custom loss: "No gradients provided for any variable" error
I have an image segmentation problem I have to solve in TensorFlow 2.
In particular I have a training set composed by aerial images paired with their respective masks. In a mask the terrain is colored in black and the buildings are colored in white.…

robertspierre
- 3,218
- 2
- 31
- 46
10
votes
2 answers
How does TensorFlow/Keras's class_weight parameter of the fit() function work?
I do semantic segmentation with TensorFlow 1.12 and Keras. I supply a vector of weights (size equal to the number of classes) to tf.keras.Model.fit() using its class_weight parameter. I was wondering how this works internally. I use a custom loss…
user4028648
10
votes
2 answers
How to test a custom loss function in keras?
I am training a CovNet with two outputs. My training samples look like this:
[0, value_a1], [0, value_a2], ...
and
[value_b1, 0], [value_b2, 0], ....
I want to generate my own loss function and mask pairs that contain the mask_value = 0. I have…

Soerendip
- 7,684
- 15
- 61
- 128
10
votes
4 answers
Variational Autoencoder gives same output image for every input mnist image when using KL divergence
When not using KL divergence term, the VAE reconstructs mnist images almost perfectly but fails to generate new ones properly when provided with random noise.
When using KL divergence term, the VAE gives the same weird output both when…

Cracin
- 503
- 1
- 8
- 19
10
votes
1 answer
Generalized dice loss for multi-class segmentation: keras implementation
I just implemented the generalised dice loss (multi-class version of dice loss) in keras, as described in ref :
(my targets are defined as: (batch_size, image_dim1, image_dim2, image_dim3, nb_of_classes))
def generalized_dice_loss_w(y_true, y_pred):…

Manu
- 186
- 3
- 11