Skip to content

Parsing a string into a differentiable expression for a GenericDeterministicModel #1279

Answered by Balandat
jduerholt asked this question in Q&A
Discussion options

You must be logged in to vote

Hmm so if the string was just some pytorch code à la str = "torch.sum(x**2) / torch.min(x)" then that could be done easily via def obj(x): return eval(s, {}, {"x": x}). This usually not a great thing to do though...

If you wanted this to be fancier and just use math expressions instead of the exact torch function you'd have to have some way or mapping those strings to the torch function. It seems challenging to do this for all of them, but if you are just interested in basic ones in the torch. namespace maybe you can get away with parsing the string and putting a torch. prefix in front of everything....

And then of course you could get arbitrarily complicated with the parsing if you wante…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by saitcakmak
Comment options

You must be logged in to vote
5 replies
@Balandat
Comment options

@Balandat
Comment options

@jduerholt
Comment options

@Balandat
Comment options

@jduerholt
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1154 on June 29, 2022 22:30.