# Take positive part of weights in loss function

**URL:** <https://discourse.julialang.org/t/take-positive-part-of-weights-in-loss-function/100950>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [June 28, 2023, 9:05pm UTC](https://discourse.julialang.org/t/take-positive-part-of-weights-in-loss-function/100950 "2023-06-28T21:05:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![clairem](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@clairem](https://discourse.julialang.org/u/clairem)\
**Post date:** [June 28, 2023, 9:05pm UTC](https://discourse.julialang.org/t/take-positive-part-of-weights-in-loss-function/100950/1 "2023-06-28T21:05:40Z")

</div>

Hi! I’m trying to constrain my weights matrix to contain only positive values.

I was confused by answers to similar questions, and I thought it might make sense to implement my loss function such that, when it takes my weights in, it evaluates only their positive part.

Could someone explain (to a complete flux beginner) how to constrain a weights matrix to contain only non-negative elements, or write up a quick loss function that works with the positive part of weights?

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [June 28, 2023, 9:37pm UTC](https://discourse.julialang.org/t/take-positive-part-of-weights-in-loss-function/100950/2 "2023-06-28T21:37:17Z")

</div>

> [@clairem](#):
>
> Hi! I’m trying to constrain my weights matrix to contain only positive values.

`filter!(>(0), x)`

---

<div class="post-metadata">

**Author:** ![clairem](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@clairem](https://discourse.julialang.org/u/clairem)\
**Post date:** [June 28, 2023, 9:42pm UTC](https://discourse.julialang.org/t/take-positive-part-of-weights-in-loss-function/100950/3 "2023-06-28T21:42:10Z")

</div>

Where in the code should something like that go? In the loss function? When I try to put it in my loss function, I’m getting an error (the weights vector is a “NamedTuple{(:weight,), Tuple{Matrix{Float32}}}” if that helps).

I have been able to take the positive part of the weights only in my loss function, using

```julia
 theta = max.(ComponentVector{Float64}(theta), 0) 

```

where theta is the weights vector, but the final weights end up being negative (because this doesn’t actually change the weights, it just makes it so that we only consider their positive parts in the loss function). Is there a way to constrain the weights themselves so that they cannot be made negative?

---

<div class="post-metadata">

**Author:** ![clairem](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@clairem](https://discourse.julialang.org/u/clairem)\
**Post date:** [June 29, 2023, 5:18pm UTC](https://discourse.julialang.org/t/take-positive-part-of-weights-in-loss-function/100950/4 "2023-06-29T17:18:07Z")

</div>

I resolved this question by making the weights the exponentiated versions of the values I solve for – thus, the weights must always be positive.
