-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from adtzlr/add-dual2real
Add `Tensor.dual2real(like=None)`
- Loading branch information
Showing
4 changed files
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
cosh, | ||
diagonal, | ||
dot, | ||
dual2real, | ||
einsum, | ||
exp, | ||
hstack, | ||
|
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
Δ, | ||
Δδ, | ||
broadcast_to, | ||
dual2real, | ||
einsum, | ||
f, | ||
matmul, | ||
|
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,36 @@ | ||
import numpy as np | ||
|
||
import tensortrax as tr | ||
import tensortrax.math as tm | ||
|
||
|
||
def test_dual2real(): | ||
np.random.seed(34563) | ||
x = (np.random.rand(3, 3) - 0.5) / 10 + np.eye(3) | ||
|
||
# init a Tensor with `hessian=True` | ||
F = tr.Tensor(x) | ||
F.init(hessian=True) | ||
|
||
# perform some math operations | ||
C = F.T() @ F | ||
J = tm.linalg.det(F) | ||
W = tm.trace(J**(-2 / 3) * C) - 3 | ||
eta = 1 - 1 / 3 * tm.tanh(W / 8) | ||
|
||
# set old dual data as new real values (i.e. obtain the gradient) | ||
P = W.dual2real(like=F) | ||
tm.dual2real(W, like=F) | ||
|
||
# perform some more math with a derived Tensor involved | ||
Q = eta * P | ||
|
||
# take the gradient | ||
A = tr.δ(Q) | ||
|
||
assert P.shape == (3, 3) | ||
assert Q.shape == (3, 3) | ||
assert A.shape == (3, 3, 3, 3) | ||
|
||
if __name__ == "__main__": | ||
test_dual2real() |