# Convex.jl and Icnn

**URL:** https://discourse.julialang.org/t/convex-jl-and-icnn/80942
**Category:** Optimization (Mathematical)
**Created:** [May 12, 2022, 9:28am UTC](https://discourse.julialang.org/t/convex-jl-and-icnn/80942 "2022-05-12T09:28:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)
#### Post date: [May 12, 2022, 9:28am UTC](https://discourse.julialang.org/t/convex-jl-and-icnn/80942/1 "2022-05-12T09:28:00Z")

</div>

Dear all,

I would like to implement an Input Convex Neural Networks [ICNN](http://proceedings.mlr.press/v70/amos17b/amos17b.pdf) with the awesome convex.jl tool. The purpose to minimize a cost function. However, in order to get convexity with Icnn, weighting matrices are nonnegative and activation function is convex and nonnegative. So within convex, there is the `pos(x)` and sound like relu activation function.

I did the following evaluation:

```julia
using Flux
using Convex

W = NNlib.relu.(rand(3,3))
b = rand(3,1)

x = Variable(3,1)
y = Variable(3,1)

expr = Convex.pos(W * x[:,1] + b)

```

Then I tried :

```julia
y[:,1] == Convex.pos(W * x[:,1] + b)

```

And I’ve got the warning

```julia
┌ Warning: Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior.

```

I’m not a professional with DCP subject. However I do not understand why added the convex.jl constraint lead to “warming” as ‘y’ is a variable. Maybe a clever person can help me.

Warmest regards

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 12, 2022, 10:26am UTC](https://discourse.julialang.org/t/convex-jl-and-icnn/80942/2 "2022-05-12T10:26:32Z")

</div>

Hi!  
I don’t know anything about ICNNs but I think I see where your problem is.  
When doing convex optimization, you are allowed two types of constraints: affine equality constraints and convex inequality constraints. This ensures that the feasible region you define is a convex set.  
The function `f(x) -> pos(Wx+b)` is convex but not affine, so I guess that is why the equality constraint `y == f(x)` is not accepted by Convex.jl? Could you try with an inequality just to check?

---

<div class="post-metadata">

### Author: ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)
#### Post date: [May 12, 2022, 11:35am UTC](https://discourse.julialang.org/t/convex-jl-and-icnn/80942/3 "2022-05-12T11:35:27Z")

</div>

Thanks for answering.

Please find the inequality:

```julia
constraint = Constraint[y[:,1] >= expr ]
1-element Vector{Constraint}:
 >= constraint (convex)
├─ index (affine; real)
│ └─ 3-element real variable (id: 175…826)
└─ max (convex; positive)
   ├─ + (affine; real)
   │ ├─ * (affine; real)
   │ │ ├─ …
   │ │ └─ …
   │ └─ [0.34773; 0.713025; 0.600791;;]
   └─ 0

```

and

```julia
julia> constraint = Constraint[y[:,1] <= expr ]
1-element Vector{Constraint}:
┌ Warning: Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior.

```

So \<= provides a DCP compliant issue.

I also evaluate:

```julia
julia> constraint = Constraint[expr >= ones(3,1)]
1-element Vector{Constraint}:
┌ Warning: Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior.

```

and

```julia
constraint = Constraint[expr <= ones(3,1)]
1-element Vector{Constraint}:
 <= constraint (convex)
├─ max (convex; positive)
│ ├─ + (affine; real)
│ │ ├─ * (affine; real)
│ │ │ ├─ …
│ │ │ └─ …
│ │ └─ [0.34773; 0.713025; 0.600791;;]
│ └─ 0
└─ [1.0; 1.0; 1.0;;]

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [May 12, 2022, 1:57pm UTC](https://discourse.julialang.org/t/convex-jl-and-icnn/80942/4 "2022-05-12T13:57:42Z")

</div>

Yeah, just as @gdalle said, expressions of the form `convex_function_of_variables >= 0` are not allowed (and if zero isn’t on one side, we subtract terms over to the other so it is), because that region is not a convex set. Think about a disk: the equation for `(x,y)` to be in a circular disk of radius `r` is `x^2 + y^2 <= r^2`:

 ![Screen Shot 2022-05-12 at 15.56.31](https://global.discourse-cdn.com/julialang/original/3X/5/c/5cf22d27e10e1480c40b1f3a624fada4f96b3a36.png)

For points to be outside the disk, `x^2 + y^2 > r^2`:  
 ![Screen Shot 2022-05-12 at 15.56.33](https://global.discourse-cdn.com/julialang/original/3X/2/f/2f4552a82e6859684c555c7250ee7c2be999909f.png)

Clearly this shape is not convex.

See [The DCP ruleset — CVX Users' Guide](http://cvxr.com/cvx/doc/dcp.html) for all the DCP rules.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 12, 2022, 1:59pm UTC](https://discourse.julialang.org/t/convex-jl-and-icnn/80942/5 "2022-05-12T13:59:14Z")

</div>

Yeah, sorry I should have been clearer: you are allowed constraints of the form `f(x) <= 0` where `f` is convex. Not `f(x) >= 0`
