Skip to content

Commit 72a8d6e

Browse files
committed
added SoftPlus
1 parent f268d0e commit 72a8d6e

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

docs/src/operators.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ LBFGS
5454

5555
```@docs
5656
Sigmoid
57+
SoftPlus
5758
SoftMax
5859
```
5960

src/AbstractOperators.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ include("calculus/NonLinearCompose.jl")
6262
# Non-Linear operators
6363
include("nonlinearoperators/Sigmoid.jl")
6464
include("nonlinearoperators/SoftMax.jl")
65+
include("nonlinearoperators/SoftPlus.jl")
6566

6667
# Syntax
6768
include("syntax.jl")

src/nonlinearoperators/SoftPlus.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export SoftPlus
2+
3+
"""
4+
`SoftPlus([domainType=Float64::Type,] dim_in::Tuple)`
5+
6+
Creates the softplus non-linear operator with input dimensions `dim_in`.
7+
```math
8+
\\sigma(\\mathbf{x}) = \\log (1 + e^{x} )
9+
```
10+
11+
"""
12+
struct SoftPlus{T,N} <: NonLinearOperator
13+
dim::NTuple{N,Int}
14+
end
15+
16+
function SoftPlus(DomainType::Type, DomainDim::NTuple{N,Int}) where {N}
17+
SoftPlus{DomainType,N}(DomainDim)
18+
end
19+
20+
SoftPlus(DomainDim::NTuple{N,Int}) where {N} = SoftPlus{Float64,N}(DomainDim)
21+
22+
function A_mul_B!(y::AbstractArray{T,N}, L::SoftPlus{T,N}, x::AbstractArray{T,N}) where {T,N}
23+
y .= log.(1.+exp.(x))
24+
end
25+
26+
function Ac_mul_B!(y::AbstractArray{T,N},
27+
L::Jacobian{A},
28+
b::AbstractArray{T,N}) where {T,N, A <: SoftPlus{T,N}}
29+
y .= 1./(1.+exp.(-L.x)).*b
30+
end
31+
32+
fun_name(L::SoftPlus) = "σ"
33+
34+
size(L::SoftPlus) = (L.dim, L.dim)
35+
36+
domainType(L::SoftPlus{T,N}) where {T,N} = T
37+
codomainType(L::SoftPlus{T,N}) where {T,N} = T

test/test_nonlinear_operators.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ r = randn(n,m,l)
2828
op = SoftMax(Float64,(n,m,l))
2929

3030
y, grad = test_NLop(op,x,r,verb)
31+
32+
n = 10
33+
x = randn(n)
34+
r = randn(n)
35+
op = SoftPlus(Float64,(n,))
36+
37+
n,m,l = 4,5,6
38+
x = randn(n,m,l)
39+
r = randn(n,m,l)
40+
op = SoftPlus(Float64,(n,m,l))
41+
42+
y, grad = test_NLop(op,x,r,verb)

0 commit comments

Comments
 (0)