cvxpy / cvxpy

A Python-embedded modeling language for convex optimization problems.
https://www.cvxpy.org
Apache License 2.0
5.44k stars 1.07k forks source link

Can Variable be converted to Tensor #2285

Open fine4303 opened 12 months ago

fine4303 commented 12 months ago

Hello, I'm a newbie in Machine Learning. I constructe a neural network using torch and I want Variable serve as neural network inputs.

my code:

model = nn.gen_nn()
model.load_state_dict(torch.load('NN.pt'), strict=True)

p = cvxpy.Variable(n)
V = model(p)
objective = cvxpy.Maximize(V)
constraints = []
prob = cvxpy.Problem(objective, constraints)

the issue is that Variable cannot convert to Tensor. Is my idea feasible? Any help is great! thanks!

Transurgeon commented 12 months ago

Unfortunately this isn't supported at the moment. There is no Intermediate Representation between cvxpy and torch currently, but it is something that the developers are planning to add soon.

fine4303 commented 12 months ago

Unfortunately this isn't supported at the moment. There is no Intermediate Representation between cvxpy and torch currently, but it is something that the developers are planning to add soon.

Thank you very much for your answer! To the best of your knowledge, can you tell me what tools can solve the maximum/minimum value of a neural network?

Transurgeon commented 11 months ago

I am not too familiar with Neural Networks, sorry about that. I would look deeper into torch features, it is possible that something similar exists. With some little searching I found that the cost-functions of neural networks are generally not convex (see here). Maybe you could look into cvxpylayers?