Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 13 additions & 3 deletions deepmd/dpmodel/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def sigmoid_t(x): # noqa: ANN001, ANN201
return xp_sigmoid(x)


def softplus_t(x): # noqa: ANN001, ANN201
"""Numerically stable softplus."""
xp = array_api_compat.array_namespace(x)
positive = x > 0
exp_neg_abs = xp.exp(xp.where(positive, -x, x))
return xp.where(
positive,
x + xp.log(1 + exp_neg_abs),
xp.log(1 + exp_neg_abs),
)


class Identity(NativeOP):
def __init__(self) -> None:
super().__init__()
Expand Down Expand Up @@ -330,9 +342,7 @@ def fn(x): # noqa: ANN001, ANN202
elif activation_function == "softplus":

def fn(x): # noqa: ANN001, ANN202
xp = array_api_compat.array_namespace(x)
# generated by GitHub Copilot
return xp.log(1 + xp.exp(x))
return softplus_t(x)

return fn
elif activation_function == "sigmoid":
Expand Down
Loading
Loading