`y = ln(exp(x) + 1)` is applied to the tensor elementwise.
| 2957 | |
| 2958 | |
| 2959 | class SoftPlus(Operator): |
| 2960 | """ |
| 2961 | `y = ln(exp(x) + 1)` is applied to the tensor elementwise. |
| 2962 | """ |
| 2963 | |
| 2964 | def __init__(self): |
| 2965 | super(SoftPlus, self).__init__() |
| 2966 | |
| 2967 | def forward(self, x): |
| 2968 | """ |
| 2969 | Return `ln(exp(x) + 1)`, where x is CTensor. |
| 2970 | """ |
| 2971 | #f(x) = ln(exp(x) + 1) |
| 2972 | if training: |
| 2973 | self.input = x |
| 2974 | x1 = singa.AddFloat(singa.Exp(x), 1.0) |
| 2975 | y = singa.Log(x1) |
| 2976 | return y |
| 2977 | |
| 2978 | def backward(self, dy): |
| 2979 | """ |
| 2980 | Args: |
| 2981 | dy (CTensor): the gradient tensor from upper operations |
| 2982 | Returns: |
| 2983 | CTensor, the gradient over input |
| 2984 | """ |
| 2985 | dx = singa.Exp(singa.MultFloat(self.input, -1.0)) |
| 2986 | dx = singa.PowFloat(singa.AddFloat(dx, 1.0), -1.0) |
| 2987 | dx = singa.__mul__(dy, dx) |
| 2988 | return dx |
| 2989 | |
| 2990 | |
| 2991 | def softplus(x): |