com.yahoo.tensor.functions.XwPlusB Maven / Gradle / Ivy
// 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 XwPlusB extends CompositeTensorFunction {
private final TensorFunction x, w, b;
private final String dimension;
public XwPlusB(TensorFunction x, TensorFunction w, TensorFunction b, String dimension) {
this.x = x;
this.w = w;
this.b = b;
this.dimension = dimension;
}
@Override
public List> arguments() { return List.of(x, w, b); }
@Override
public TensorFunction withArguments(List> arguments) {
if ( arguments.size() != 3)
throw new IllegalArgumentException("XwPlusB must have 3 arguments, got " + arguments.size());
return new XwPlusB<>(arguments.get(0), arguments.get(1), arguments.get(2), dimension);
}
@Override
public PrimitiveTensorFunction toPrimitive() {
TensorFunction primitiveX = x.toPrimitive();
TensorFunction primitiveW = w.toPrimitive();
TensorFunction primitiveB = b.toPrimitive();
return new Join<>(new Reduce<>(new Join<>(primitiveX, primitiveW, ScalarFunctions.multiply()),
Reduce.Aggregator.sum,
dimension),
primitiveB,
ScalarFunctions.add());
}
@Override
public String toString(ToStringContext context) {
return "xw_plus_b(" + x.toString(context) + ", " +
w.toString(context) + ", " +
b.toString(context) + ", " +
context.resolveBinding(dimension) + ")";
}
@Override
public int hashCode() { return Objects.hash("xwplusb", x, w, b, dimension); }
}