# Reduce rank and set values according to 3rd dimension

**URL:** <https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029>\
**Category:** SameDiff\
**Created:** [September 27, 2022, 12:32pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029 "2022-09-27T12:32:14Z")\
**Posts on this page:** 13\
**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:** [September 27, 2022, 12:32pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/1 "2022-09-27T12:32:14Z")

</div>

[  
[[1,0,0,0],[0,1,0,0],[0,0,1,0]],  
[[1,0,0,0],[0,1,0,0],[0,0,1,0]]  
]  
How to get below result from above SDVariable? Reduce 3rd dimension, and the value = 0.1 when 1 exists in the first or 2rd position, otherwise value = 1.  
[  
[0.1,0.1,1],  
[0.1,0.1,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:** [September 27, 2022, 12:34pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/2 "2022-09-27T12:34:23Z")

</div>

@SidneyLann mind posting some code to start from? I’ll take that and modify it. I can say for sure that SDVariable does have get(…) just like on the ndarrays. You might be able to use that as is.

---

<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:** [September 27, 2022, 12:45pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/3 "2022-09-27T12:45:11Z")

</div>

SDVariable input = sameDiff.placeHolder(“input”, DataType.FLOAT, -1, timeSteps, numFeatrues);  
SDVariable label = sameDiff.placeHolder(“label”, DataType.FLOAT, -1, timeSteps, numLabelClasses);  
SDVariable weight = label.argmax(“weight”, 2);  
…  
sameDiff.loss.softmaxCrossEntropy(“loss”, label, out, weight);

I want to generate weights dynamic according to label SDVariable .

---

<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:** [September 27, 2022, 12:49pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/4 "2022-09-27T12:49:03Z")

</div>

Do you need just a subset of the dynamic weights then? If so, I think var.get(SDIndex.(…)) methods will do pretty well for you.  
You can even do it with variables.

---

<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:** [September 27, 2022, 12:52pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/5 "2022-09-27T12:52:15Z")

</div>

I am not just get a subset, also to assign new values to it. and the new values according to the 3rd dimension. It may need to use nested for loops.

---

<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:** [September 27, 2022, 12:53pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/6 "2022-09-27T12:53:13Z")

</div>

@SidneyLann then you can use put(…) - thttps://github.com/deeplearning4j/deeplearning4j/blob/2668876811fbebf1fafc09f954a34dfcd8c95780/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/SDVariable.java#L1807

This actually does dynamic looping for you internally.

---

<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:** [September 27, 2022, 1:06pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/7 "2022-09-27T13:06:24Z")

</div>

It seems I can’t use it, because I actually need to generate a toPut, but it need pass a toPut. and my case need to assign by condition.

---

<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:** [September 27, 2022, 1:08pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/8 "2022-09-27T13:08:53Z")

</div>

@SidneyLann you can probably reuse the control flow. We have the boolean eval conditions etc all there. We also have where all in there.

Am I correct in understanding you need to update your weights where they meet a certain condition? Is that the idea? Current samediff should be able to do all that as is.

---

<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:** [September 27, 2022, 1:14pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/9 "2022-09-27T13:14:26Z")

</div>

SDVariable label = sameDiff.placeHolder(“label”, DataType.FLOAT, -1, timeSteps, numLabelClasses);  
SDVariable weight =…( label )

I need generate weight dynamic from label, the label has rank 3, and the weight has rank 2.

This is label  
[  
[[1,0,0,0],[0,1,0,0],[0,0,1,0]],  
[[1,0,0,0],[0,1,0,0],[0,0,1,0]]  
]

And this is weight  
[  
[0.1,0.1,1],  
[0.1,0.1,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:** [September 27, 2022, 1:33pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/10 "2022-09-27T13:33:34Z")

</div>

@SidneyLann is this for weighted loss? If not why not make that 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:** [September 27, 2022, 1:45pm UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/11 "2022-09-27T13:45:55Z")

</div>

sameDiff.loss.softmaxCrossEntropy(“loss”, label, lstm1, weight);  
This should be weighted loss?

Yes, can use placeholder, but it need more maintenance work.

---

<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:** [September 28, 2022, 5:44am UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/12 "2022-09-28T05:44:25Z")

</div>

@SidneyLann could you clarify what you mean by maintenance work? Is there something we could do on our side to make that easier for you?

---

<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:** [September 28, 2022, 8:49am UTC](https://community.konduit.ai/t/reduce-rank-and-set-values-according-to-3rd-dimension/2029/13 "2022-09-28T08:49:40Z")

</div>

The logic to generate the weights INDArray is almost the same as label array, so if the label logic is amended, the same thing need to do for weights.

Ok. I generate the weights and use placeholder and it work now.
