-
Hi, I have a kind of esoteric question ;) Do you know if it is possible to somehow parse a string, encoding a mathematical expression, into a differentiable torch expression somehow like Botorch related use case would be for example the case of a frontend in which a user can type a mathematical expression which is then used as callable in a Any thoughts on this? Best, Johannes |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hmm so if the string was just some pytorch code à la 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 And then of course you could get arbitrarily complicated with the parsing if you wanted to. In a real world application with a GUI you'd probably want to have a corpus of possible methods and then use typeahead for selecting them in the interface.... |
Beta Was this translation helpful? Give feedback.
-
I just came across this package https://github.com/patrick-kidger/sympytorch which takes sympy expressions and translates them to differentiable torch modules. Looks very helpful for defining complex nonlinear constraints and deterministic models via a frontend ;) |
Beta Was this translation helpful? Give feedback.
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 viadef 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 atorch.
prefix in front of everything....And then of course you could get arbitrarily complicated with the parsing if you wante…