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

Add Differentiable CEM solver #329

Merged
merged 35 commits into from
Mar 23, 2023
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
31c277d
first implementation of dcem solver
dishank-b Oct 13, 2022
245d80c
dcem optimizer working
dishank-b Oct 14, 2022
11962c6
minor changes in dcem, added tests
dishank-b Oct 17, 2022
d069ee6
online calculatino of n_batch
dishank-b Oct 18, 2022
24d2fa9
initializing LML layer
dishank-b Oct 20, 2022
5632588
dcem working backwards tutorial 2
dishank-b Oct 28, 2022
655ba19
better vectorization in solver
dishank-b Oct 31, 2022
63a04cf
vectoriztion in solve method for itr loop in optimizer class
dishank-b Oct 31, 2022
fd93941
forward pass working perfectly with current set of hyperparams with b…
dishank-b Nov 3, 2022
976fb08
dcem backward unit test passed for one setting
dishank-b Nov 3, 2022
7547504
DCEM backward unit test working, not tested with leo, insanely slow w…
dishank-b Nov 4, 2022
3a22e09
refactoring, removed DcemSolver in favour of solve method in DCEM opt…
dishank-b Nov 4, 2022
89c8b39
correcting circle ci errors
dishank-b Nov 7, 2022
9bf03c4
corrected lml url for requirements.txt
dishank-b Nov 7, 2022
a1064a2
corrected reuirements.txt for lml
dishank-b Nov 7, 2022
c21dc69
removing -e from requirements
dishank-b Nov 8, 2022
c809e94
changing setup.py to install lml
dishank-b Nov 8, 2022
2bbf2db
changing setup.py to add lml
dishank-b Nov 8, 2022
4f3788c
commented dcem_test
dishank-b Nov 8, 2022
0f69df6
unit test working with both gpu, cpu with even less 10-2 error thres …
dishank-b Nov 9, 2022
0592f6f
testing with lml_eps=10-4
dishank-b Nov 10, 2022
7d68639
Revert "testing with lml_eps=10-4"
dishank-b Nov 10, 2022
044e881
reverting the common.py file
dishank-b Nov 10, 2022
769d483
dcem working, name changed from DCem to DCEM
dishank-b Mar 6, 2023
126ee47
removed _all_solve function and chnaged _solve name to _CEM_step
dishank-b Mar 6, 2023
f2ccf4b
changed dcem objective to use error_metric and edit __init files
dishank-b Mar 6, 2023
74a2a5d
dcem working, added dcem tutorial
dishank-b Mar 6, 2023
679dc3b
add lml as third party
dishank-b Mar 6, 2023
fab68be
or black pre-commit hook
dishank-b Mar 6, 2023
f4b345a
removeing abs in loss function since model chnaged test_theseus layer
dishank-b Mar 7, 2023
97c94b6
changes in test_theseus to make it compatible with DCEM
dishank-b Mar 9, 2023
bc32139
minor changes:styling, nits, typehinting, etc.
dishank-b Mar 17, 2023
50ea767
reverted minor changes, corrected test_theseus_layer argument logic f…
dishank-b Mar 20, 2023
ee75e95
using scatter for indexes with temp=None in dcem
dishank-b Mar 21, 2023
947e026
final changes, removing half-complete changes before merge
dishank-b Mar 22, 2023
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
commented dcem_test
  • Loading branch information
dishank-b committed Mar 9, 2023
commit 4f3788c9869ab9e66ac29851038fbab97e389ec2
240 changes: 120 additions & 120 deletions tests/optimizer/nonlinear/test_dcem.py
dishank-b marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,120 +1,120 @@
import torch

import theseus as th
from theseus import DCem


def test_dcem_optim():
# Step 1: Construct optimization and auxiliary variables.
# Construct variables of the function: these the optimization variables of the cost functions.
x = th.Vector(1, name="x")
y = th.Vector(1, name="y")

# Construct auxiliary variables for the constants of the function.
a = th.Vector(tensor=torch.randn(1, 1), name="a")
b = th.Vector(tensor=torch.randn(1, 1), name="b")

# Step 2: Construct cost weights
# For w1, let's use a named variable
w1 = th.ScaleCostWeight(th.Variable(tensor=torch.ones(1, 1), name="w1_sqrt"))
w2 = th.ScaleCostWeight(2.0) # we provide 2, as sqrt of 4 for the (y-b)^2 term

# Step 3: Construct cost functions representing each error term
# First term
cf1 = th.Difference(x, a, w1, name="term_1")
# Second term
cf2 = th.Difference(y, b, w2, name="term_2")

objective = th.Objective()
objective.add(cf1)
objective.add(cf2)

# Step 5: Evaluate objective under current values
# Note this needs to be preceded by a call to `objective.update`
# Here we use the update function to set values of all variables
objective.update(
{
"a": torch.ones(1, 1),
"b": 2 * torch.ones(1, 1),
"x": 0.5 * torch.ones(1, 1),
"y": 3 * torch.ones(1, 1),
}
)

# Recall that our objective is (x - a)^2 + 4 (y - b)^2
# which is minimized at x = a and y = b
# Let's start by assigning random values to them
objective.update({"x": torch.randn(1, 1), "y": torch.randn(1, 1)})

optimizer = DCem(objective)

info = optimizer.optimize()

print(info)
assert torch.tensor(
[info.best_solution["x"] - 1.0 < 1e-3, info.best_solution["y"] - 2.0 < 1e-3]
).all()


def test_dcem_theseus_layer():
# Step 1: Construct optimization and auxiliary variables.
# Construct variables of the function: these the optimization variables of the cost functions.
x = th.Vector(1, name="x")
y = th.Vector(1, name="y")

# Construct auxiliary variables for the constants of the function.
a = th.Vector(tensor=torch.randn(1, 1), name="a")
b = th.Vector(tensor=torch.randn(1, 1), name="b")

# Step 2: Construct cost weights
# For w1, let's use a named variable
w1 = th.ScaleCostWeight(th.Variable(tensor=torch.ones(1, 1), name="w1_sqrt"))
w2 = th.ScaleCostWeight(2.0) # we provide 2, as sqrt of 4 for the (y-b)^2 term

# Step 3: Construct cost functions representing each error term
# First term
cf1 = th.Difference(x, a, w1, name="term_1")
# Second term
cf2 = th.Difference(y, b, w2, name="term_2")

objective = th.Objective()
objective.add(cf1)
objective.add(cf2)

# Step 5: Evaluate objective under current values
# Note this needs to be preceded by a call to `objective.update`
# Here we use the update function to set values of all variables
objective.update(
{
"a": torch.ones(1, 1),
"b": 2 * torch.ones(1, 1),
"x": 0.5 * torch.ones(1, 1),
"y": 3 * torch.ones(1, 1),
}
)

# Recall that our objective is (x - a)^2 + 4 (y - b)^2
# which is minimized at x = a and y = b
# Let's start by assigning random values to them
objective.update({"x": torch.randn(1, 1), "y": torch.randn(1, 1)})

optimizer = DCem(objective)

layer = th.TheseusLayer(optimizer)
values, info = layer.forward(
{
"x": torch.randn(1, 1),
"y": torch.randn(1, 1),
"a": torch.ones(1, 1),
"b": 2 * torch.ones(1, 1),
"w1_sqrt": torch.ones(1, 1),
}
)

print(info)
assert torch.tensor(
[info.best_solution["x"] - 1.0 < 1e-3, info.best_solution["y"] - 2.0 < 1e-3]
).all()


test_dcem_optim()
# import torch
mhmukadam marked this conversation as resolved.
Show resolved Hide resolved
dishank-b marked this conversation as resolved.
Show resolved Hide resolved

# import theseus as th
# from theseus import DCem


# def test_dcem_optim():
# # Step 1: Construct optimization and auxiliary variables.
# # Construct variables of the function: these the optimization variables of the cost functions.
# x = th.Vector(1, name="x")
# y = th.Vector(1, name="y")

# # Construct auxiliary variables for the constants of the function.
# a = th.Vector(tensor=torch.randn(1, 1), name="a")
# b = th.Vector(tensor=torch.randn(1, 1), name="b")

# # Step 2: Construct cost weights
# # For w1, let's use a named variable
# w1 = th.ScaleCostWeight(th.Variable(tensor=torch.ones(1, 1), name="w1_sqrt"))
# w2 = th.ScaleCostWeight(2.0) # we provide 2, as sqrt of 4 for the (y-b)^2 term

# # Step 3: Construct cost functions representing each error term
# # First term
# cf1 = th.Difference(x, a, w1, name="term_1")
# # Second term
# cf2 = th.Difference(y, b, w2, name="term_2")

# objective = th.Objective()
# objective.add(cf1)
# objective.add(cf2)

# # Step 5: Evaluate objective under current values
# # Note this needs to be preceded by a call to `objective.update`
# # Here we use the update function to set values of all variables
# objective.update(
# {
# "a": torch.ones(1, 1),
# "b": 2 * torch.ones(1, 1),
# "x": 0.5 * torch.ones(1, 1),
# "y": 3 * torch.ones(1, 1),
# }
# )

# # Recall that our objective is (x - a)^2 + 4 (y - b)^2
# # which is minimized at x = a and y = b
# # Let's start by assigning random values to them
# objective.update({"x": torch.randn(1, 1), "y": torch.randn(1, 1)})

# optimizer = DCem(objective)

# info = optimizer.optimize()

# print(info)
# assert torch.tensor(
# [info.best_solution["x"] - 1.0 < 1e-3, info.best_solution["y"] - 2.0 < 1e-3]
# ).all()


# def test_dcem_theseus_layer():
# # Step 1: Construct optimization and auxiliary variables.
# # Construct variables of the function: these the optimization variables of the cost functions.
# x = th.Vector(1, name="x")
# y = th.Vector(1, name="y")

# # Construct auxiliary variables for the constants of the function.
# a = th.Vector(tensor=torch.randn(1, 1), name="a")
# b = th.Vector(tensor=torch.randn(1, 1), name="b")

# # Step 2: Construct cost weights
# # For w1, let's use a named variable
# w1 = th.ScaleCostWeight(th.Variable(tensor=torch.ones(1, 1), name="w1_sqrt"))
# w2 = th.ScaleCostWeight(2.0) # we provide 2, as sqrt of 4 for the (y-b)^2 term

# # Step 3: Construct cost functions representing each error term
# # First term
# cf1 = th.Difference(x, a, w1, name="term_1")
# # Second term
# cf2 = th.Difference(y, b, w2, name="term_2")

# objective = th.Objective()
# objective.add(cf1)
# objective.add(cf2)

# # Step 5: Evaluate objective under current values
# # Note this needs to be preceded by a call to `objective.update`
# # Here we use the update function to set values of all variables
# objective.update(
# {
# "a": torch.ones(1, 1),
# "b": 2 * torch.ones(1, 1),
# "x": 0.5 * torch.ones(1, 1),
# "y": 3 * torch.ones(1, 1),
# }
# )

# # Recall that our objective is (x - a)^2 + 4 (y - b)^2
# # which is minimized at x = a and y = b
# # Let's start by assigning random values to them
# objective.update({"x": torch.randn(1, 1), "y": torch.randn(1, 1)})

# optimizer = DCem(objective)

# layer = th.TheseusLayer(optimizer)
# values, info = layer.forward(
# {
# "x": torch.randn(1, 1),
# "y": torch.randn(1, 1),
# "a": torch.ones(1, 1),
# "b": 2 * torch.ones(1, 1),
# "w1_sqrt": torch.ones(1, 1),
# }
# )

# print(info)
# assert torch.tensor(
# [info.best_solution["x"] - 1.0 < 1e-3, info.best_solution["y"] - 2.0 < 1e-3]
# ).all()


# test_dcem_optim()