001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.classification.sgd.objectives;
018
019import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance;
020import com.oracle.labs.mlrg.olcut.provenance.impl.ConfiguredObjectProvenanceImpl;
021import com.oracle.labs.mlrg.olcut.util.Pair;
022import org.tribuo.classification.sgd.LabelObjective;
023import org.tribuo.math.la.SGDVector;
024import org.tribuo.math.util.ExpNormalizer;
025import org.tribuo.math.util.VectorNormalizer;
026
027/**
028 * A multiclass version of the log loss.
029 * <p>
030 * Generates a probabilistic model, and uses an {@link ExpNormalizer}.
031 */
032public class LogMulticlass implements LabelObjective {
033
034    private final VectorNormalizer normalizer = new ExpNormalizer();
035
036    /**
037     * Constructs a multiclass log loss.
038     */
039    public LogMulticlass() {}
040
041    @Deprecated
042    @Override
043    public Pair<Double, SGDVector> valueAndGradient(int truth, SGDVector prediction) {
044        return lossAndGradient(truth, prediction);
045    }
046
047    /**
048     * Returns a {@link Pair} of {@link Double} and {@link SGDVector} representing the loss
049     * and per label gradients respectively.
050     * <p>
051     * The prediction vector is transformed to produce the per label gradient and returned.
052     * @param truth The true label id
053     * @param prediction The prediction for each label id
054     * @return A Pair of the score and per label gradient.
055     */
056    @Override
057    public Pair<Double,SGDVector> lossAndGradient(Integer truth, SGDVector prediction) {
058        prediction.normalize(normalizer);
059        double loss = Math.log(prediction.get(truth));
060        prediction.scaleInPlace(-1.0);
061        prediction.add(truth,1.0);
062        return new Pair<>(loss,prediction);
063    }
064
065    @Override
066    public VectorNormalizer getNormalizer() {
067        return new ExpNormalizer();
068    }
069
070    /**
071     * Returns true.
072     * @return True.
073     */
074    @Override
075    public boolean isProbabilistic() {
076        return true;
077    }
078
079    @Override
080    public String toString() {
081        return "LogMulticlass";
082    }
083
084    @Override
085    public ConfiguredObjectProvenance getProvenance() {
086        return new ConfiguredObjectProvenanceImpl(this,"LabelObjective");
087    }
088}