Using exponential function with sum

965 views
Skip to first unread message

sophie...@gmail.com

unread,
Aug 22, 2018, 1:33:09 PM8/22/18
to Pyomo Forum
Hello, 

I am trying to optimize a neural network model with sigmoid activation function. The function has the following form: 
_______________________________________________________________________________

def obj_rule(m):
    h1 = 1/(1+np.exp(-(sum(h1_w[i]*model.x[i] for i in V_all)+h1_b)))
    h2 = 1/(1+np.exp(-(sum(h2_w[i]*model.x[i] for i in V_all)+h2_b)))
    output = out_w1*h1 + out_w2*h2 + clf.intercepts_[1]
    return output

where h1_w, h2_w, h1_b, and h2_b are just vectors or scalar constants. 
________________________________________________________________________________

I get the following error: 
_________________________________________________________________________

ERROR: Rule failed when generating expression for objective obj:
    AttributeError: '_SumExpression' object has no attribute 'exp'
ERROR: Constructing component 'obj' from data=None failed: AttributeError:
    '_SumExpression' object has no attribute 'exp'
____________________________________________________________________________

It seems like pyomo can't handle an expression with sum and exp function together. I can obtain an output without error if I just use python (i.e. instead of writing an expression in pyomo form, I tried to check by just plugging in a vector for model.x and it worked). Also, if I remove "np.exp", it works perfectly. I don't think this is an error caused by Python but related to how pyomo handles summations. Is there any way I can solve this problem? 

Thanks! 

Sophie  

Santiago Rodriguez

unread,
Aug 22, 2018, 2:01:25 PM8/22/18
to pyomo...@googlegroups.com
Hi Sophie,

The problem is that np.exp takes numbers as their arguments. However, Pyomo variables are more than numbers. To get an expression with an exponential of sum of variables use pyomo's exponential function instead of using numpy's

import pyomo.environ as aml

m = aml.ConcreteModel()

m.x = aml.Var()

m.c = aml.Constraint(expr=aml.exp(m.x+m.x)==1)




--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages