# Shape of weight

**URL:** <https://community.konduit.ai/t/shape-of-weight/1706>\
**Category:** SameDiff\
**Created:** [December 13, 2021, 7:48am UTC](https://community.konduit.ai/t/shape-of-weight/1706 "2021-12-13T07:48:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 13, 2021, 7:48am UTC](https://community.konduit.ai/t/shape-of-weight/1706/1 "2021-12-13T07:48:26Z")

</div>

sameDiff.loss.softmaxCrossEntropy(“loss”, label=[8,128,6], out=[8,128,6], weight=[?,?])

What’t the shape of the weight should be here?

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 13, 2021, 10:42am UTC](https://community.konduit.ai/t/shape-of-weight/1706/2 "2021-12-13T10:42:52Z")

</div>

output=[miniBatch, sentenceLength, positionCharLabels], What’t the shape of the weights should be then?

---

<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:** [December 13, 2021, 11:44am UTC](https://community.konduit.ai/t/shape-of-weight/1706/3 "2021-12-13T11:44:46Z")

</div>

@SidneyLann generally it’s 1 weight per class output. Weights are also broadcastable.

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 13, 2021, 12:18pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/4 "2021-12-13T12:18:13Z")

</div>

But the miniBatch is dynamic and the weights are static, how to weight the sentences?

---

<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:** [December 14, 2021, 1:16pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/5 "2021-12-14T13:16:42Z")

</div>

@SidneyLann so you want per example weights. We only support output oriented weights which you can find here:

> <https://github.com/eclipse/deeplearning4j/blob/d77597bae3253e13a3384a168fda789f68309c1a/libnd4j/include/ops/declarable/generic/loss/cosineDistance.cpp#L62>

You can approximate this with output based weighting + resampling which we do support so in that case it would be the same shape as the output.

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 15, 2021, 3:12am UTC](https://community.konduit.ai/t/shape-of-weight/1706/6 "2021-12-15T03:12:00Z")

</div>

weights = [128, 6] and loss = [8, 128] → not work  
weights = [6, 8] and loss = [8, 128] → not work  
weights = [1024] and loss = [1024] → work

So the weights must be scalar or rank ==1?

---

<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:** [December 15, 2021, 12:09pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/7 "2021-12-15T12:09:50Z")

</div>

@SidneyLann are you saying weights that don’t have the same shape as the output? No it doesn’t have to only be scalar. I gave you the source code there and you can see we support different shapes. It sounds like the labels are the wrong shape and the weights are mis calculated. Could you give me something end to end where you actually try what I suggested with the weights as a placeholder?

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 15, 2021, 3:00pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/8 "2021-12-15T15:00:24Z")

</div>

1. 

sameDiff.loss.softmaxCrossEntropy(“loss”, label=[8,128,6], out=[miniBatch, sentenceLength, positionCharLabels]=[8,128,6], weight=[128,6])  
===\>  
shapes of weights and loss arrays should be broadcastable, but got weights = [128, 6] and loss = [8, 128] instead!

1. 

sameDiff.loss.softmaxCrossEntropy(“loss”, label=[8,128,6], out=[8,128,6], weight=[6, 8])  
===\>  
shapes of weights and loss arrays should be broadcastable, but got weights = [6, 8] and loss = [8, 128] instead!

1. 

sameDiff.loss.softmaxCrossEntropy(“loss”, label=[1024,6], out=[1024,6], weight=[1024])  
===\> NO ERROR

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 17, 2021, 10:22am UTC](https://community.konduit.ai/t/shape-of-weight/1706/9 "2021-12-17T10:22:58Z")

</div>

Such then the weights must be set based on miniBatch, how to set it not based on the miniBatch? I want to set static weight, not dynamic.

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 17, 2021, 10:47pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/10 "2021-12-17T22:47:14Z")

</div>

Can weight fixed sentences, but can’t weight N sentences, where N is dynamic.

---

<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:** [December 17, 2021, 10:55pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/11 "2021-12-17T22:55:50Z")

</div>

@SidneyLann could you clarify? The 1 sentence replies/answers don’t really help me much. I told you a way of passing in dynamic weights and I don’t have any indication you actually tried it.  
You use weights as placeholders alongside inputs and labels just like you would for training.

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 25, 2021, 10:26pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/13 "2021-12-25T22:26:10Z")

</div>

sameDiff.loss.softmaxCrossEntropy(“loss”, label=[8,128,6], out=[8,128,6], weight=[8, 128])

This line can be run in SNAPSHOT now. Thanks.

But this should weight every char in a sentence(128), not weight the labels(6). Am I right?

---

<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:** [December 29, 2021, 12:14pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/14 "2021-12-29T12:14:09Z")

</div>

@SidneyLann if the labels are the characters then yes that should be fine.

---

<div class="post-metadata">

**Author:** ![SidneyLann](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sidneylann/32/367_2.png) [@SidneyLann](https://community.konduit.ai/u/SidneyLann)\
**Post date:** [December 29, 2021, 12:27pm UTC](https://community.konduit.ai/t/shape-of-weight/1706/15 "2021-12-29T12:27:24Z")

</div>

The labels are not the characters, a char has 6 possible labels, I want to weight the labels, below settings should work?  
label=[8,128,6], out=[8,128,6], weight=[8, 128]
