ai.djl.training.loss.SimpleCompositeLoss Maven / Gradle / Ivy
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.training.loss;
import ai.djl.ndarray.NDList;
import ai.djl.util.Pair;
import java.util.ArrayList;
/**
* {@code SimpleCompositeLoss} is an implementation of the {@link Loss} abstract class that can
* combine different {@link Loss} functions by adding the individual losses together.
*
* For cases where the losses use only a single index of the labels and/or predictions, use the
* {@link IndexLoss}.
*
*
For an example of using this loss, see the
* captcha training example.
*/
public class SimpleCompositeLoss extends AbstractCompositeLoss {
/**
* Creates a new empty instance of {@code CompositeLoss} that can combine the given {@link Loss}
* components.
*/
public SimpleCompositeLoss() {
this("CompositeLoss");
}
/**
* Creates a new empty instance of {@code CompositeLoss} that can combine the given {@link Loss}
* components.
*
* @param name the display name of the loss
*/
public SimpleCompositeLoss(String name) {
super(name);
components = new ArrayList<>();
}
/**
* Adds a Loss that applies to all labels and predictions to this composite loss.
*
* @param loss the loss to add
* @return this composite loss
*/
public SimpleCompositeLoss addLoss(Loss loss) {
components.add(loss);
return this;
}
/** {@inheritDoc} */
@Override
protected Pair inputForComponent(
int componentIndex, NDList labels, NDList predictions) {
return new Pair<>(labels, predictions);
}
}