Using SameDiff for aCosineSimVertex

I am trying to replace a L2Vertex in an existing working network with a Cosine Similarity vertex using SameDiff (or any other way).

I keep getting errors like

Axis array [-539956880] contains values above array rank (rank=2)
org.nd4j.linalg.exception.ND4JIllegalStateException: Axis array [-539956880] contains values above array rank (rank=2)

with the code below.

.addVertex("CosineDiff", new SameDiffLambdaVertex() {
					@Override
					public SDVariable defineVertex(SameDiff sameDiff, VertexInputs inputs) {
						SDVariable input1 = inputs.getInput(0);
						SDVariable input2 = inputs.getInput(1);
						return sameDiff.math.cosineSimilarity(input1, input2, 1).reshape(input1.getShape()[0], 1);
					}
}, "Content1", "Content2")

Am I trying to approach this the wrong way?

removing

.reshape(input1.getShape()[0], 1);

and chaining on a

new ReshapeVertex(1000, 1)

works but then I have to hard code the batch size

@mdavis95 did you try specifying -1 for the shape that’s variable? That should work for filling out dynamic shapes.

I will give (-1,1) a try, thanks!

This worked perfected, thanks for the tip!

.addVertex("ContentCosine", new CosineVertex(), "Content1", "Content2") //
.addVertex("ContentCosineReshaped", new ReshapeVertex(-1, 1), "ContentCosine") //
public class CosineVertex extends SameDiffLambdaVertex {

	@Override
	public SDVariable defineVertex(SameDiff sameDiff, VertexInputs inputs) {
		SDVariable input1 = inputs.getInput(0);
		SDVariable input2 = inputs.getInput(1);
		return sameDiff.math.cosineSimilarity(input1, input2, 1);
	}
}