# Per-sample weights or label fractions

**URL:** <https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706>\
**Category:** DL4J\
**Created:** [July 9, 2020, 2:42pm UTC](https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706 "2020-07-09T14:42:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cacophany53](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@cacophany53](https://community.konduit.ai/u/cacophany53)\
**Post date:** [July 9, 2020, 2:42pm UTC](https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706/1 "2020-07-09T14:42:07Z")

</div>

Hi there.

Just wondering if per-sample weighting is currently possible? For our project we need to apply fractional weights to each sample.

Alternatively can DL4J handle having a fraction specified for label values for each sample? This example would indicate that it can: [deeplearning4j-examples/ImageDrawer.java at master · eclipse/deeplearning4j-examples · GitHub](https://github.com/eclipse/deeplearning4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/modeling/feedforward/regression/ImageDrawer.java)

However, the Constructor for DataSet specifies that labels must be binarized: [https://deeplearning4j.org/api/latest/org/nd4j/linalg/dataset/DataSet.html#DataSet-org.nd4j.linalg.api.ndarray.INDArray-org.nd4j.linalg.api.ndarray.INDArray-org.nd4j.linalg.api.ndarray.INDArray-org.nd4j.linalg.api.ndarray.INDArray-](https://deeplearning4j.org/api/latest/org/nd4j/linalg/dataset/DataSet.html#DataSet-org.nd4j.linalg.api.ndarray.INDArray-org.nd4j.linalg.api.ndarray.INDArray-org.nd4j.linalg.api.ndarray.INDArray-org.nd4j.linalg.api.ndarray.INDArray-)

I’m confused as this seems to be contradictory.

Any advice is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![arthur-dai-618](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arthur-dai-618/32/656_2.png) [@arthur-dai-618](https://community.konduit.ai/u/arthur-dai-618)\
**Post date:** [October 21, 2021, 8:46am UTC](https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706/2 "2021-10-21T08:46:24Z")

</div>

> [@cacophany53](#):
>
> fractional

I’m facing the same problem, did you solve it?

---

<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 21, 2021, 9:01am UTC](https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706/3 "2021-10-21T09:01:49Z")

</div>

@arthur-dai-618 depending on whether you’re using samediff or dl4j yes both are possible. Can you clarify which API you’re using?

---

<div class="post-metadata">

**Author:** ![arthur-dai-618](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arthur-dai-618/32/656_2.png) [@arthur-dai-618](https://community.konduit.ai/u/arthur-dai-618)\
**Post date:** [October 21, 2021, 9:42am UTC](https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706/4 "2021-10-21T09:42:16Z")

</div>

I’m using dl4j, how to do it?

---

<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 21, 2021, 10:28am UTC](https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706/5 "2021-10-21T10:28:27Z")

</div>

@arthur-dai-618 here’s weighted labels:

> <https://github.com/eclipse/deeplearning4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/features/classimbalance/WeightedLossFunctionExample.java#L73>

For per example weights, you’d have to implement custom loss:

> <https://github.com/eclipse/deeplearning4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/advanced/features/customizingdl4j/lossfunctions/CustomLossUsageEx.java>

You could also look in to different sampling techniques as well.

---

<div class="post-metadata">

**Author:** ![arthur-dai-618](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/arthur-dai-618/32/656_2.png) [@arthur-dai-618](https://community.konduit.ai/u/arthur-dai-618)\
**Post date:** [October 21, 2021, 12:18pm UTC](https://community.konduit.ai/t/per-sample-weights-or-label-fractions/706/6 "2021-10-21T12:18:21Z")

</div>

That helps me a lot, thank you very much!
