Skip to content

Commit 28cd4a2

Browse files
author
Han Wang
committed
fix: address reviewer comments on tabulate_math
- Fix UnboundLocalError: use yy instead of zz for single-layer nets - Add linear/none (functype=0) to ACTIVATION_TO_FUNCTYPE and grad/grad_grad (identity: f'=1, f''=0) - All 8 activation derivatives verified against numerical differentiation
1 parent 1067ff0 commit 28cd4a2

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

deepmd/utils/tabulate_math.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"softplus": 5,
4040
"sigmoid": 6,
4141
"silu": 7,
42+
"linear": 0,
43+
"none": 0,
4244
}
4345

4446

@@ -47,7 +49,9 @@
4749

4850
def grad(xbar: np.ndarray, y: np.ndarray, functype: int) -> np.ndarray:
4951
"""First derivative of the activation function."""
50-
if functype == 1:
52+
if functype == 0:
53+
return np.ones_like(xbar)
54+
elif functype == 1:
5155
return 1 - y * y
5256
elif functype == 2:
5357
var = np.tanh(SQRT_2_PI * (xbar + GGELU * xbar**3))
@@ -75,7 +79,9 @@ def grad(xbar: np.ndarray, y: np.ndarray, functype: int) -> np.ndarray:
7579

7680
def grad_grad(xbar: np.ndarray, y: np.ndarray, functype: int) -> np.ndarray:
7781
"""Second derivative of the activation function."""
78-
if functype == 1:
82+
if functype == 0:
83+
return np.zeros_like(xbar)
84+
elif functype == 1:
7985
return -2 * y * (1 - y * y)
8086
elif functype == 2:
8187
var1 = np.tanh(SQRT_2_PI * (xbar + GGELU * xbar**3))
@@ -435,7 +441,7 @@ def _make_data(self, xx: np.ndarray, idx: int) -> Any:
435441
dy = dz
436442
yy = zz
437443

438-
vv = zz.astype(self.data_type)
444+
vv = yy.astype(self.data_type)
439445
dd = dy.astype(self.data_type)
440446
d2 = dy2.astype(self.data_type)
441447
return vv, dd, d2

0 commit comments

Comments
 (0)