All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.tensor.functions.Argmax Maven / Gradle / Ivy

Go to download

Library for use in Java components of Vespa. Shared code which do not fit anywhere else.

There is a newer version: 8.409.18
Show newest version
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.functions;

import com.yahoo.tensor.evaluation.Name;

import java.util.List;
import java.util.Objects;

/**
 * @author bratseth
 */
public class Argmax extends CompositeTensorFunction {

    private final TensorFunction argument;
    private final List dimensions;

    public Argmax(TensorFunction argument) {
        this(argument, List.of());
    }

    public Argmax(TensorFunction argument, String dimension) {
        this(argument, List.of(dimension));
    }

    public Argmax(TensorFunction argument, List dimensions) {
        Objects.requireNonNull(dimensions, "The dimensions cannot be null");
        this.argument = argument;
        this.dimensions = List.copyOf(dimensions);
    }

    @Override
    public List> arguments() { return List.of(argument); }

    @Override
    public TensorFunction withArguments(List> arguments) {
        if (arguments.size() != 1)
            throw new IllegalArgumentException("Argmax must have 1 argument, got " + arguments.size());
        return new Argmax<>(arguments.get(0), dimensions);
    }

    @Override
    public PrimitiveTensorFunction toPrimitive() {
        TensorFunction primitiveArgument = argument.toPrimitive();
        TensorFunction reduce = new Reduce<>(primitiveArgument, Reduce.Aggregator.max, dimensions);
        return new Join<>(primitiveArgument, reduce, ScalarFunctions.equal());
    }

    @Override
    public String toString(ToStringContext context) {
        return "argmax(" + argument.toString(context) + Reduce.commaSeparatedNames(dimensions, context) + ")";
    }

    @Override
    public int hashCode() { return Objects.hash("argmax", argument, dimensions); }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy