Search code examples
python-3.xcvxpy

Broadcasting in CVXPY


I'm trying to train a linear model with bias using CVXPY. Suppose input and target are given. Suppose loss is a cvxpy function, convex in its 1st argument. I have the following code:

import cvxpy as cvx
n_data = 100
d_in = 10
d_out = 10
beta = cvx.Variable(d_in, d_out)
bias = cvx.Variable(d_out)

input = np.random.rand(n_data, d_in)
...
objective = cvx.Minimize(loss(input @ beta + bias, target))
problem = cvx.Problem(objective)

problem.solve()

I get a broadcasting error due to input @ beta + bias : Cannot broadcast dimensions (100, 10) (10,)


Solution

  • Writing the outer product of bias with the vector of ones and defining bias = cvx.Variable((d_out, 1)) does the trick. Use:

    input @ beta + np.ones((n_data, 1)) @ bias