Skip to content

Commit 882c873

Browse files
committed
Use logistic function in StatsFuns
1 parent 0d16973 commit 882c873

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

Manifest.toml

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
[[Base64]]
44
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
55

6+
[[CompilerSupportLibraries_jll]]
7+
deps = ["Libdl", "Pkg"]
8+
git-tree-sha1 = "7c4f882c41faa72118841185afc58a2eb00ef612"
9+
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
10+
version = "0.3.3+0"
11+
612
[[Dates]]
713
deps = ["Printf"]
814
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
915

10-
[[Distributed]]
11-
deps = ["Random", "Serialization", "Sockets"]
12-
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
13-
1416
[[InteractiveUtils]]
1517
deps = ["Markdown"]
1618
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1719

1820
[[LibGit2]]
21+
deps = ["Printf"]
1922
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
2023

2124
[[Libdl]]
@@ -38,8 +41,14 @@ git-tree-sha1 = "c3d1a616362645754b18e12dbba96ec311b0867f"
3841
uuid = "a6bfbf70-4841-5cb9-aa18-3a8ad3c413ee"
3942
version = "2018.6.22+0"
4043

44+
[[OpenSpecFun_jll]]
45+
deps = ["CompilerSupportLibraries_jll", "Libdl", "Pkg"]
46+
git-tree-sha1 = "d51c416559217d974a1113522d5919235ae67a87"
47+
uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
48+
version = "0.5.3+3"
49+
4150
[[Pkg]]
42-
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Test", "UUIDs"]
51+
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
4352
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
4453

4554
[[Printf]]
@@ -60,6 +69,18 @@ git-tree-sha1 = "d37400976e98018ee840e0ca4f9d20baa231dc6b"
6069
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
6170
version = "1.0.1"
6271

72+
[[Rmath]]
73+
deps = ["Random", "Rmath_jll"]
74+
git-tree-sha1 = "86c5647b565873641538d8f812c04e4c9dbeb370"
75+
uuid = "79098fc4-a85e-5d69-aa6a-4863f24498fa"
76+
version = "0.6.1"
77+
78+
[[Rmath_jll]]
79+
deps = ["Libdl", "Pkg"]
80+
git-tree-sha1 = "1660f8fefbf5ab9c67560513131d4e933012fc4b"
81+
uuid = "f50d1b31-88e8-58de-be2c-1cc44531875f"
82+
version = "0.2.2+0"
83+
6384
[[SHA]]
6485
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
6586

@@ -73,13 +94,21 @@ uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
7394
deps = ["LinearAlgebra", "Random"]
7495
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
7596

97+
[[SpecialFunctions]]
98+
deps = ["OpenSpecFun_jll"]
99+
git-tree-sha1 = "d8d8b8a9f4119829410ecd706da4cc8594a1e020"
100+
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
101+
version = "0.10.3"
102+
76103
[[Statistics]]
77104
deps = ["LinearAlgebra", "SparseArrays"]
78105
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
79106

80-
[[Test]]
81-
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
82-
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
107+
[[StatsFuns]]
108+
deps = ["Rmath", "SpecialFunctions"]
109+
git-tree-sha1 = "04a5a8e6ab87966b43f247920eab053fd5fdc925"
110+
uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
111+
version = "0.9.5"
83112

84113
[[UUIDs]]
85114
deps = ["Random", "SHA"]

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ NNPACK_jll = "a6bfbf70-4841-5cb9-aa18-3a8ad3c413ee"
99
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1010
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1111
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
12+
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
1213

1314
[compat]
1415
Requires = "0.5, 1.0"
16+
StatsFuns = "0.9"
1517
julia = "1.3"
1618

1719
[extras]

src/NNlib.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module NNlib
22
using Pkg
33
using Requires
44
using NNPACK_jll
5+
using StatsFuns: logistic, softplus
56

67
# Include APIs
78
include("dim_helpers.jl")

src/activation.jl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ export σ, sigmoid, hardσ, hardsigmoid, hardtanh, relu, leakyrelu, relu6, rrelu
1212
Classic [sigmoid](https://en.wikipedia.org/wiki/Sigmoid_function) activation
1313
function.
1414
"""
15-
function σ(x::Real)
16-
t = exp(-abs(x))
17-
ifelse(x 0, inv(one(t) + t), t / (one(t) + t))
18-
end
15+
const σ = logistic
1916
const sigmoid = σ
2017

2118
"""
@@ -181,15 +178,6 @@ See [Quadratic Polynomials Learn Better Image Features](http://www.iro.umontreal
181178
"""
182179
softsign(x::Real) = x / (one(x) + abs(x))
183180

184-
185-
"""
186-
softplus(x) = log(exp(x) + 1)
187-
188-
See [Deep Sparse Rectifier Neural Networks](http://proceedings.mlr.press/v15/glorot11a/glorot11a.pdf).
189-
"""
190-
softplus(x::Real) = ifelse(x > 0, x + log1p(exp(-x)), log1p(exp(x)))
191-
192-
193181
"""
194182
logcosh(x)
195183
@@ -222,7 +210,7 @@ See [Softshrink Activation Function](https://www.gabormelli.com/RKB/Softshrink_A
222210
softshrink(x::Real, λ = oftype(x/1, 0.5)) = min(max(zero(x), x - λ), x + λ)
223211

224212
# Provide an informative error message if activation functions are called with an array
225-
for f in (:σ, :hardσ, :logσ, :hardtanh, :relu, :leakyrelu, :relu6, :rrelu, :elu, :gelu, :swish, :lisht, :selu, :celu, :trelu, :softsign, :softplus, :logcosh, :mish, :tanhshrink, :softshrink)
213+
for f in (:hardσ, :logσ, :hardtanh, :relu, :leakyrelu, :relu6, :rrelu, :elu, :gelu, :swish, :lisht, :selu, :celu, :trelu, :softsign, :logcosh, :mish, :tanhshrink, :softshrink)
226214
@eval $(f)(x::AbstractArray, args...) =
227215
error("Use broadcasting (`", $(string(f)), ".(x)`) to apply activation functions to arrays.")
228216
end

test/activation.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ end
108108
@testset "Array input" begin
109109
x = rand(5)
110110
for a in ACTIVATION_FUNCTIONS
111-
@test_throws ErrorException a(x)
111+
if a != σ && a != softplus
112+
@test_throws ErrorException a(x)
113+
end
112114
end
113115
end
114116

0 commit comments

Comments
 (0)