Flux.jl/src/layers/basic.jl

46 lines
987 B
Julia
Raw Normal View History

2017-08-19 19:52:29 +00:00
# Chain
2017-06-05 15:08:23 +00:00
type Chain
2016-08-25 21:49:21 +00:00
layers::Vector{Any}
2017-03-17 16:34:51 +00:00
Chain(xs...) = new([xs...])
2016-08-25 21:49:21 +00:00
end
2017-03-17 16:34:51 +00:00
@forward Chain.layers Base.getindex, Base.first, Base.last, Base.endof, Base.push!
2017-03-07 14:37:37 +00:00
@forward Chain.layers Base.start, Base.next, Base.done
2016-08-25 21:49:21 +00:00
2017-08-22 16:13:03 +00:00
Optimise.children(c::Chain) = c.layers
2016-08-25 21:49:21 +00:00
(s::Chain)(x) = foldl((x, m) -> m(x), x, s.layers)
2017-06-12 11:39:34 +00:00
2017-08-19 19:04:21 +00:00
Compiler.graph(s::Chain) =
2016-10-25 22:10:35 +00:00
foldl((v, m) -> vertex(m, v), constant(inputnode(1)), s.layers)
2016-09-06 17:03:39 +00:00
2017-02-28 16:42:48 +00:00
Base.getindex(c::Chain, i::AbstractArray) = Chain(c.layers[i]...)
2017-08-19 19:52:29 +00:00
2017-08-21 16:20:09 +00:00
function Base.show(io::IO, c::Chain)
print(io, "Chain(")
join(io, c.layers, ", ")
print(io, ")")
end
2017-09-02 20:50:11 +00:00
# Dense
2017-08-19 19:52:29 +00:00
2017-09-02 20:50:11 +00:00
struct Dense{F,S,T}
2017-08-20 12:35:35 +00:00
σ::F
2017-08-19 19:52:29 +00:00
W::S
b::T
end
2017-09-02 20:50:11 +00:00
Dense(in::Integer, out::Integer, σ = identity; init = initn) =
2017-09-07 19:13:04 +00:00
Dense(σ, param(init(out, in)), param(init(out)))
2017-08-19 19:52:29 +00:00
2017-09-02 20:50:11 +00:00
Optimise.children(d::Dense) = (d.W, d.b)
2017-08-22 16:13:03 +00:00
2017-09-02 20:50:11 +00:00
(a::Dense)(x) = a.σ.(a.W*x .+ a.b)
2017-08-21 16:20:09 +00:00
2017-09-02 20:50:11 +00:00
function Base.show(io::IO, l::Dense)
print(io, "Dense(", size(l.W, 2), ", ", size(l.W, 1))
2017-08-21 16:20:09 +00:00
l.σ == identity || print(io, ", ", l.σ)
print(io, ")")
end