Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With these changes I could speed up execution a lot. On my machine instead of over 2 minutes it now takes about 14 seconds (including saving the plot as SVG, 12 seconds without that).
The changes are:
Because the expression tree is always exactly the same (when not batching) it can be re-used and instead of re-creating the whole tree every time the new
refresh()
method recalculates all the values in-place. This method also setsgrad
to0.0
in every node, sozero_grad()
isn't needed in the optimization loop anymore.Note that for this improvement to work with batching you need a little bit more preparation. That is, you need to create
Xb
andyb
dummy lists of batch size containingValue
objects and use these to setup the calculations before the optimization loop. In theloss()
function you then need make the random index selection and update thevalue.data
attributes of all the objects inXb
andyb
before runningrefresh()
.I didn't include an example for that at the moment.I also added code to the demo that demonstrates batching.