-
Notifications
You must be signed in to change notification settings - Fork 2
Optimizer precision #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Optimizer precision #135
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
core/src/test/scala/dimwit/optimizer/GradientOptimizerSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package dimwit.optimizer | ||
|
|
||
| import dimwit.* | ||
| import dimwit.Conversions.given | ||
| import dimwit.autodiff.FloatTree.* | ||
| import dimwit.autodiff.FloatTree.ops.* | ||
| import dimwit.autodiff.* | ||
|
|
||
| class GradientOptimizerSuite extends DimwitTest: | ||
|
|
||
| describe("GradientDescent"): | ||
| it("should converge towards the minimum of f(x) = (x+1)^2 at x = -1"): | ||
| val optimizer = GradientDescent(learningRate = 0.1) | ||
| val minX = optimizer.iterate(Tensor0(2.0f))(x => Grad(2 * (x + 1))).drop(1000).next() | ||
| minX.item shouldBe -1.0f +- 0.1f | ||
|
|
||
| describe("Adam"): | ||
| it("should converge towards the minimum of f(x) = (x+1)^2 at x = -1"): | ||
| val optimizer = Adam(learningRate = 0.1) | ||
| val minX = optimizer.iterate(Tensor0(2.0f))(x => Grad(2 * (x + 1))).drop(1000).next() | ||
| minX.item shouldBe -1.0f +- 0.1f | ||
|
|
||
| it("should compute the exact momentum and velocity updates (single step)"): | ||
| val optimizer = Adam(learningRate = 0.1, b1 = 0.9, b2 = 0.999) | ||
| val initParams = Tensor0(2.0f) | ||
| val initState = optimizer.init(initParams) | ||
|
|
||
| val grad = Grad(Tensor0(6.0f)) | ||
| val (nextParams, nextState) = optimizer.update(grad, initParams, initState) | ||
|
|
||
| nextParams.item shouldBe 1.9f +- 1e-5f | ||
| nextState.momentums.item shouldBe 0.6f +- 1e-5f | ||
| nextState.velocities.item shouldBe 0.036f +- 1e-5f | ||
| nextState.b1.item shouldBe 0.9f +- 1e-5f | ||
| nextState.b2.item shouldBe 0.999f +- 1e-5f | ||
|
|
||
| describe("AdamW"): | ||
| it("should converge towards the minimum of f(x) = (x+1)^2 at x = -1"): | ||
| val optimizer = AdamW(Adam(learningRate = 0.1), weightDecayFactor = 0.1) | ||
| val minX = optimizer.iterate(Tensor0(2.0f))(x => Grad(2 * (x + 1))).drop(1000).next() | ||
| minX.item shouldBe -1.0f +- 0.1f | ||
|
|
||
| it("should apply decoupled weight decay (single step)"): | ||
| val adam = Adam(learningRate = 0.1) | ||
| val adamW = AdamW(adam, weightDecayFactor = 0.1) | ||
|
|
||
| val initParams = Tensor0(2.0) | ||
| val grad = Grad(Tensor0(6.0)) | ||
| val (adamParams, _) = adam.update(grad, initParams, adam.init(initParams)) | ||
| val (adamWParams, _) = adamW.update(grad, initParams, adamW.init(initParams)) | ||
|
|
||
| adamWParams.item shouldBe (adamParams.item - 0.02) +- 1e-5 | ||
|
|
||
| describe("Lion"): | ||
| it("should converge towards the minimum of f(x) = (x+1)^2"): | ||
| val optimizer = Lion(learningRate = 0.1) | ||
| val minX = optimizer.iterate(Tensor0(2.0f))(x => Grad(2 * (x + 1))).drop(1000).next() | ||
| minX.item shouldBe -1.0f +- 0.1f | ||
|
|
||
| it("should compute the exact sign-based update and momentum (single step)"): | ||
| val optimizer = Lion(learningRate = 0.1, beta1 = 0.9, beta2 = 0.99) | ||
| val initParams = Tensor0(2.0) | ||
| val initMomentum = optimizer.init(initParams) | ||
|
|
||
| val grad = Grad(Tensor0(6.0)) | ||
| val (nextParams, nextMomentum) = optimizer.update(grad, initParams, initMomentum) | ||
|
|
||
| nextParams.item shouldBe 1.9d +- 1e-5d | ||
| nextMomentum.item shouldBe 0.06d +- 1e-5d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this not be confusing on single tensors, as they are both tensor trees and tensors and consequently will have +, +! and ++!. Another problem might be that ! usually indicates broadcasting and has here a different meaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mostly mirrors what we have in ValueOps for Tensors. While I think we can improve this overall, I propose moving it to a future PR and merging it as is.
Currently:
+: Add two tensors of the same shape
+!: Add two tensors, broadcast lower-rank one
++: Add two tensor trees of the same structure
++!: Add Tensor0/Double to a tensor tree by broadcasting it for each structure
So, in my mind, ++! does broadcasting (just limited to Tensor0, as it is the only structure broadcastable to all Tensor shapes in the tree). But you are probably right that it's suboptimal; I think, as is, we can write "t0 ++! 5" but not "t0 +! 5" with t0 being Tensor0... Also, ValueOps provides Float extension methods, and FloatTree provides Double extension methods... Let's address this in a future clean-up.