Reduce rank and set values according to 3rd dimension

[
[[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]
]

@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.

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 .

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.

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.

@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.

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.

@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.

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]
]

@SidneyLann is this for weighted loss? If not why not make that a placeholder?

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

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

@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?

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.