Closed notEvil closed 1 year ago
The resulting expression has a LeafCount above one million. It certainly takes some time to deserialize even if 50s seems a little long. In order to help you getting a better experience with the library, I need to understand the next step. What will you do with such a big Wolfram Language expression?
If you plan to use it in a subsequent evaluation, just store it in a variable and use it directly (don't forget the semicolon). This will prevent the entire expression to round trip. The second command becomes:
_ = 'result = D[Log[{}], {{{{Global`thetatheta1, Global`thetatheta2, Global`thetasigma1, Global`thetasigma2, Global`thetasigma3, Global`thetarho12, Global`thetarho13, Global`thetarho23g1}}, 2}}];'.format(_)
You can also store the result somewhere, in which case I recommend to Export
the intermediary variable above, directly from the kernel:
with w_evaluation.WolframLanguageSession(kernel=KERNEL_PATH) as session:
session.evaluate(_)
session.evaluate(wl.Export('/tmp/test.wxf', wl.Global.result, "WXF"))
It'll create a new file /tmp/test.wxf
with the resulting expression. It can be later loaded using Import["/tmp/test.wxf"]
.
When cached (CSE), the result is not that big. My initial goal was to get the derivatives (and use Mathematicas implicit simplification) as an alternative to my current approach (R Deriv). That is, I would transform the result into a different representation (for instance python code based on numpy) and use it as a drop-in replacement. Of course, if I would do the entire evaluation in Mathematica, the deserialization wouldn't be necessary.
Can you explain what you mean by:
When cached (CSE)
I'm not familiar with this acronym.
If I understand correctly there is a repeated pattern in the result that artificially inflates the leaf count of the result. If you plan to traverse the resulting python expression and turn it into something else, then why don't you do a Replace
of the repeated expression with something easy to identify and short (e.g. an unique symbol name like myRepeatedPattern
), which you can later expand to its value (in the python numpy representation).
Sry, CSE is common subexpression elimination and the repeated expressions are a result of the chain rule and only to a minor extent of the base function.
I looked at the profile more closely and couldn't find obviously inefficient parts. Therefore I will try to do CSE in Mathematica (like https://community.wolfram.com/groups/-/m/t/394421) before transmission.
Closing this issue, feel free to reopen it with if needed.
Consider the following
In the profile I see 50.4s in session.evaluate and 49.9s in binary_deserialize. I assume there is not much asynchronous execution, so timing should be correct. Is there any chance to speed things up? In this case, the result is composed of many similar sub expressions (due to chain rule) which could be cached in transmission.