could not determine the shape of object type 'ScalarVar'

Viewed 27

I trained a model in PyTorch and exported the output with torch.jit.trace

Now, I wanted to do optimization on the trained function in Pyomo. But I get "could not determine the shape of object type 'ScalarVar'" error from torch.tensor(x) part of the code. How can I solve this problem?

One way would be turning x type to NumPy array and then converting it to torch tensor, but I couldn't find the way of doing it.

import pyomo.environ as pyo
import torch

loaded_trace = torch.jit.load("trace.pth")

model = pyo.ConcreteModel()
model.x = pyo.Var(bounds = (-10, 10))
x=model.x

model.obj = pyo.Objective(expr = loaded_trace(torch.tensor(x).unsqueeze(1).float()) , sense = minimize)

opt = SolverFactory('glpk')
opt.solve(model)

model.pprint()

x_value = pyo.value(x)

print(x_value)
0 Answers
Related