Skip to content
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

Changed TheseusLayer.forward() to receive optimizer_kwargs as a single dict #45

Merged
merged 4 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated examples to use optimizer_kwargs dict in forward().
  • Loading branch information
luisenp committed Jan 21, 2022
commit c14ac1dd16c2d9fa5d0749ed46549556a1ac7bd2
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
rev: v0.910
hooks:
- id: mypy
additional_dependencies: [torch==1.9.0, tokenize-rt==3.2.0, types-PyYAML]
additional_dependencies: [torch==1.9.0, tokenize-rt==3.2.0, types-PyYAML, types-mock]
args: [--no-strict-optional, --ignore-missing-imports]
exclude: setup.py

Expand Down
56 changes: 34 additions & 22 deletions examples/backward_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ def quad_error_fn(optim_vars, aux_vars):
theseus_optim = th.TheseusLayer(optimizer)
updated_inputs, info = theseus_optim.forward(
theseus_inputs,
track_best_solution=True,
verbose=False,
backward_mode=th.BackwardMode.FULL,
optimizer_kwargs={
"track_best_solution": True,
"verbose": False,
"backward_mode": th.BackwardMode.FULL,
},
)

# The quadratic \hat y is now fit and we can also use Theseus
Expand All @@ -98,9 +100,11 @@ def quad_error_fn(optim_vars, aux_vars):
# forward again and changing the backward_mode flag.
updated_inputs, info = theseus_optim.forward(
theseus_inputs,
track_best_solution=True,
verbose=False,
backward_mode=th.BackwardMode.IMPLICIT,
optimizer_kwargs={
"track_best_solution": True,
"verbose": False,
"backward_mode": th.BackwardMode.IMPLICIT,
},
)

da_dx = torch.autograd.grad(updated_inputs["a"], data_x, retain_graph=True)[0].squeeze()
Expand All @@ -110,10 +114,12 @@ def quad_error_fn(optim_vars, aux_vars):
# We can also use truncated unrolling to compute the derivative:
updated_inputs, info = theseus_optim.forward(
theseus_inputs,
track_best_solution=True,
verbose=False,
backward_mode=th.BackwardMode.TRUNCATED,
backward_num_iterations=5,
optimizer_kwargs={
"track_best_solution": True,
"verbose": False,
"backward_mode": th.BackwardMode.TRUNCATED,
"backward_num_iterations": 5,
},
)

da_dx = torch.autograd.grad(updated_inputs["a"], data_x, retain_graph=True)[0].squeeze()
Expand All @@ -127,8 +133,8 @@ def fit_x(data_x_np):
theseus_inputs["x"] = (
torch.from_numpy(data_x_np).float().clone().requires_grad_().unsqueeze(0)
)
updated_inputs, info = theseus_optim.forward(
theseus_inputs, track_best_solution=True, verbose=False
updated_inputs, _ = theseus_optim.forward(
theseus_inputs, optimizer_kwargs={"track_best_solution": True, "verbose": False}
)
return updated_inputs["a"].item()

Expand All @@ -150,9 +156,11 @@ def fit_x(data_x_np):
start = time.time()
updated_inputs, info = theseus_optim.forward(
theseus_inputs,
track_best_solution=True,
verbose=False,
backward_mode=th.BackwardMode.FULL,
optimizer_kwargs={
"track_best_solution": True,
"verbose": False,
"backward_mode": th.BackwardMode.FULL,
},
)
times["fwd"].append(time.time() - start)

Expand All @@ -164,9 +172,11 @@ def fit_x(data_x_np):

updated_inputs, info = theseus_optim.forward(
theseus_inputs,
track_best_solution=True,
verbose=False,
backward_mode=th.BackwardMode.IMPLICIT,
optimizer_kwargs={
"track_best_solution": True,
"verbose": False,
"backward_mode": th.BackwardMode.IMPLICIT,
},
)
start = time.time()
da_dx = torch.autograd.grad(updated_inputs["a"], data_x, retain_graph=True)[
Expand All @@ -176,10 +186,12 @@ def fit_x(data_x_np):

updated_inputs, info = theseus_optim.forward(
theseus_inputs,
track_best_solution=True,
verbose=False,
backward_mode=th.BackwardMode.TRUNCATED,
backward_num_iterations=5,
optimizer_kwargs={
"track_best_solution": True,
"verbose": False,
"backward_mode": th.BackwardMode.TRUNCATED,
"backward_num_iterations": 5,
},
)
start = time.time()
da_dx = torch.autograd.grad(updated_inputs["a"], data_x, retain_graph=True)[
Expand Down
10 changes: 7 additions & 3 deletions examples/motion_planning_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ def run_learning_loop(cfg):

_, info = motion_planner.layer.forward(
planner_inputs,
track_best_solution=True,
verbose=cfg.verbose,
**cfg.optim_params.kwargs,
optimizer_kwargs={
**{
"track_best_solution": True,
"verbose": cfg.verbose,
},
**cfg.optim_params.kwargs,
},
)
if cfg.do_learning and cfg.include_imitation_loss:
solution_trajectory = motion_planner.get_trajectory()
Expand Down
8 changes: 5 additions & 3 deletions examples/state_estimation_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,12 @@ def cost_weights_model():
print("Initial error:", objective.error_squared_norm().mean().item())

for i in range(inner_loop_iters):
theseus_inputs, info = state_estimator.forward(
theseus_inputs, _ = state_estimator.forward(
theseus_inputs,
track_best_solution=True,
verbose=epoch % 10 == 0,
optimizer_kwargs={
"track_best_solution": True,
"verbose": epoch % 10 == 0,
},
)
theseus_inputs = run_model(
mode_,
Expand Down
4 changes: 3 additions & 1 deletion examples/tactile_pose_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ def run_learning_loop(cfg):
(sdf_tensor.data).repeat(batch_size, 1, 1).to(device)
)

theseus_inputs, _ = theseus_layer.forward(theseus_inputs, verbose=True)
theseus_inputs, _ = theseus_layer.forward(
theseus_inputs, optimizer_kwargs={"verbose": True}
)

obj_poses_opt = theg.get_tactile_poses_from_values(
batch_size=batch_size,
Expand Down
1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ nox==2020.8.22
pre-commit>=2.9.2
isort>=5.6.4
types-PyYAML==5.4.3
types-mock>=4.0.8