com.yahoo.tensor.functions.L2Normalize Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vespajlib Show documentation
Show all versions of vespajlib Show documentation
Library for use in Java components of Vespa. Shared code which do
not fit anywhere else.
// 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 L2Normalize extends CompositeTensorFunction {
private final TensorFunction argument;
private final String dimension;
public L2Normalize(TensorFunction argument, String dimension) {
this.argument = argument;
this.dimension = dimension;
}
@Override
public List> arguments() { return List.of(argument); }
@Override
public TensorFunction withArguments(List> arguments) {
if ( arguments.size() != 1)
throw new IllegalArgumentException("L2Normalize must have 1 argument, got " + arguments.size());
return new L2Normalize<>(arguments.get(0), dimension);
}
@Override
public PrimitiveTensorFunction toPrimitive() {
TensorFunction primitiveArgument = argument.toPrimitive();
return new Join<>(primitiveArgument,
new Map<>(new Reduce<>(new Map<>(primitiveArgument, ScalarFunctions.square()),
Reduce.Aggregator.sum,
dimension),
ScalarFunctions.sqrt()),
ScalarFunctions.divide());
}
@Override
public String toString(ToStringContext context) {
return "l2_normalize(" + argument.toString(context) + ", " + context.resolveBinding(dimension) + ")";
}
@Override
public int hashCode() { return Objects.hash("l2_normalize", argument, dimension); }
}