# Using SameDiff for aCosineSimVertex

**URL:** <https://community.konduit.ai/t/using-samediff-for-acosinesimvertex/2039>\
**Category:** SameDiff\
**Created:** [October 4, 2022, 5:52pm UTC](https://community.konduit.ai/t/using-samediff-for-acosinesimvertex/2039 "2022-10-04T17:52:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdavis95](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/mdavis95/32/46_2.png) [@mdavis95](https://community.konduit.ai/u/mdavis95)\
**Post date:** [October 4, 2022, 5:52pm UTC](https://community.konduit.ai/t/using-samediff-for-acosinesimvertex/2039/1 "2022-10-04T17:52:59Z")

</div>

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

```auto
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.

```auto
.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?

---

<div class="post-metadata">

**Author:** ![mdavis95](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/mdavis95/32/46_2.png) [@mdavis95](https://community.konduit.ai/u/mdavis95)\
**Post date:** [October 5, 2022, 12:09am UTC](https://community.konduit.ai/t/using-samediff-for-acosinesimvertex/2039/2 "2022-10-05T00:09:52Z")

</div>

removing

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

```

and chaining on a

```auto
new ReshapeVertex(1000, 1)

```

works but then I have to hard code the batch size

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [October 5, 2022, 3:08am UTC](https://community.konduit.ai/t/using-samediff-for-acosinesimvertex/2039/3 "2022-10-05T03:08:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mdavis95](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/mdavis95/32/46_2.png) [@mdavis95](https://community.konduit.ai/u/mdavis95)\
**Post date:** [October 6, 2022, 7:07pm UTC](https://community.konduit.ai/t/using-samediff-for-acosinesimvertex/2039/4 "2022-10-06T19:07:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mdavis95](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/mdavis95/32/46_2.png) [@mdavis95](https://community.konduit.ai/u/mdavis95)\
**Post date:** [October 7, 2022, 2:33pm UTC](https://community.konduit.ai/t/using-samediff-for-acosinesimvertex/2039/5 "2022-10-07T14:33:03Z")

</div>

This worked perfected, thanks for the tip!

```auto
.addVertex("ContentCosine", new CosineVertex(), "Content1", "Content2") //
.addVertex("ContentCosineReshaped", new ReshapeVertex(-1, 1), "ContentCosine") //

```

```auto
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);
	}
}

```
