-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(tutorials): update tutorials (#120)
Co-authored-by: Xuehai Pan <XuehaiPan@pku.edu.cn> Co-authored-by: Benjamin-eecs <benjaminliu.eecs@gmail.com>
- Loading branch information
1 parent
b155b15
commit a760c83
Showing
13 changed files
with
757 additions
and
1,959 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
Get Started with Jupyter Notebook | ||
================================= | ||
|
||
In this tutorial, we will use Google Colaboratory to show you the most basic usages of TorchOpt. | ||
In this tutorial, we will use Google Colab notebooks to show you the most basic usages of TorchOpt. | ||
|
||
- 1: `Functional Optimizer <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/1_Functional_Optimizer.ipynb>`_ | ||
- 2: `Visualization <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/2_Visualization.ipynb>`_ | ||
- 3: `Meta Optimizer <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/3_Meta_Optimizer.ipynb>`_ | ||
- 4: `Stop Gradient <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/4_Stop_Gradient.ipynb>`_ | ||
- 5: `Implicit Differentiation <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/5_Implicit_Differentiation.ipynb>`_ | ||
- 1: `Functional Optimizer <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/1_Functional_Optimizer.ipynb>`_ | ||
- 2: `Visualization <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/2_Visualization.ipynb>`_ | ||
- 3: `Meta-Optimizer <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/3_Meta_Optimizer.ipynb>`_ | ||
- 4: `Stop Gradient <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/4_Stop_Gradient.ipynb>`_ | ||
- 5: `Implicit Differentiation <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/5_Implicit_Differentiation.ipynb>`_ | ||
- 6: `Zero-order Differentiation <https://colab.research.google.com/github/metaopt/torchopt/blob/main/tutorials/6_Zero_Order_Differentiation>`_ |
This file contains 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,79 @@ | ||
# Copyright 2022 MetaOPT Team. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
import functorch | ||
import torch | ||
import torch.nn as nn | ||
import torch.types | ||
|
||
import helpers | ||
import torchopt | ||
|
||
|
||
BATCH_SIZE = 8 | ||
NUM_UPDATES = 5 | ||
|
||
|
||
class FcNet(nn.Module): | ||
def __init__(self, dim, out): | ||
super().__init__() | ||
self.fc = nn.Linear(in_features=dim, out_features=out, bias=True) | ||
nn.init.ones_(self.fc.weight) | ||
nn.init.zeros_(self.fc.bias) | ||
|
||
def forward(self, x): | ||
return self.fc(x) | ||
|
||
|
||
@helpers.parametrize( | ||
dtype=[torch.float64, torch.float32], | ||
lr=[1e-2, 1e-3], | ||
method=['naive', 'forward', 'antithetic'], | ||
sigma=[0.01, 0.1, 1], | ||
) | ||
def test_zero_order(dtype: torch.dtype, lr: float, method: str, sigma: float) -> None: | ||
helpers.seed_everything(42) | ||
input_size = 32 | ||
output_size = 1 | ||
batch_size = BATCH_SIZE | ||
coef = 0.1 | ||
num_iterations = NUM_UPDATES | ||
num_samples = 500 | ||
|
||
model = FcNet(input_size, output_size) | ||
|
||
fmodel, params = functorch.make_functional(model) | ||
x = torch.randn(batch_size, input_size) * coef | ||
y = torch.randn(input_size) * coef | ||
distribution = torch.distributions.Normal(loc=0, scale=1) | ||
|
||
@torchopt.diff.zero_order.zero_order( | ||
distribution=distribution, method=method, argnums=0, sigma=sigma, num_samples=num_samples | ||
) | ||
def forward_process(params, fn, x, y): | ||
y_pred = fn(params, x) | ||
loss = torch.mean((y - y_pred) ** 2) | ||
return loss | ||
|
||
optimizer = torchopt.adam(lr=lr) | ||
opt_state = optimizer.init(params) | ||
|
||
for i in range(num_iterations): | ||
opt_state = optimizer.init(params) # init optimizer | ||
loss = forward_process(params, fmodel, x, y) # compute loss | ||
|
||
grads = torch.autograd.grad(loss, params) # compute gradients | ||
updates, opt_state = optimizer.update(grads, opt_state) # get updates | ||
params = torchopt.apply_updates(params, updates) # update network parameters |
This file contains 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.