@@ -69,7 +69,7 @@ extraChain(::Tuple{}, x) = ()
69
69
70
70
71
71
"""
72
- Dense(in, out, σ= identity; bias= true, init= glorot_uniform)
72
+ Dense(in, out, σ = identity; bias = true, init = glorot_uniform)
73
73
Dense(W::AbstractMatrix, [bias, σ])
74
74
75
75
Create a traditional `Dense` layer, whose forward pass is given by:
@@ -81,7 +81,7 @@ as an `in × N` matrix, or any array with `size(x,1) == in`.
81
81
The out `y` will be a vector of length `out`, or a batch with
82
82
`size(y) == (out, size(x)[2:end]...)`
83
83
84
- Keyword `bias= false` will switch off trainable bias for the layer.
84
+ Keyword `bias = false` will switch off trainable bias for the layer.
85
85
The initialisation of the weight matrix is `W = init(out, in)`, calling the function
86
86
given to keyword `init`, with default [`glorot_uniform`](@doc Flux.glorot_uniform).
87
87
The weight matrix and/or the bias vector (of length `out`) may also be provided explicitly.
@@ -109,45 +109,41 @@ julia> Flux.params(d1) # no trainable bias
109
109
Params([[1.0 1.0 … 1.0 1.0; 1.0 1.0 … 1.0 1.0]])
110
110
```
111
111
"""
112
- struct Dense{F, M <: AbstractMatrix , B }
113
- weight:: M
114
- bias:: B
112
+ struct Dense{F,S <: AbstractArray ,T }
113
+ weight:: S
114
+ bias:: T
115
115
σ:: F
116
- function Dense (W:: M , bias = true , σ:: F = identity) where {M<: AbstractMatrix , F}
117
- b = create_bias (W, bias, size (W,1 ))
118
- new {F,M,typeof(b)} (W, b, σ)
119
- end
120
116
end
121
117
122
- function Dense (in:: Integer , out:: Integer , σ = identity;
123
- initW = nothing , initb = nothing ,
124
- init = glorot_uniform, bias= true )
118
+ Dense (W, b) = Dense (W, b, identity)
125
119
126
- W = if initW != = nothing
127
- Base. depwarn (" keyword initW is deprecated, please use init (which similarly accepts a funtion like randn)" , :Dense )
128
- initW (out, in)
129
- else
130
- init (out, in)
120
+ Dense (W:: AbstractArray , b:: Bool = true , σ = identity) =
121
+ Dense (W, create_bias (W, b, size (W,1 )), σ)
122
+
123
+ function Dense (in:: Integer , out:: Integer , σ = identity; initW = nothing ,
124
+ init = glorot_uniform, initb = nothing , bias:: Bool = true )
125
+ if initW != = nothing
126
+ Base. depwarn (" initW is deprecated, please use the `init` keyword instead" , :Dense )
127
+ init = initW
131
128
end
132
129
133
- b = if bias === true && initb != = nothing
134
- Base. depwarn (" keyword initb is deprecated, please simply supply the bias vector, bias=initb(out) " , :Dense )
135
- initb (out)
130
+ if initb != = nothing
131
+ Base. depwarn (" initb is deprecated, please use the array based constructors instead " , :Dense )
132
+ initb = initb
136
133
else
137
- bias
134
+ initb = zeros
138
135
end
139
-
140
- return Dense (W, b, σ)
136
+ Dense (init (out, in), bias ? initb (out) : Zeros (), σ)
141
137
end
142
138
143
139
@functor Dense
144
140
145
141
function (a:: Dense )(x:: AbstractVecOrMat )
146
142
W, b, σ = a. weight, a. bias, a. σ
147
- return σ .(W* x .+ b)
143
+ σ .(W * x .+ b)
148
144
end
149
145
150
- (a:: Dense )(x:: AbstractArray ) =
146
+ (a:: Dense )(x) =
151
147
reshape (a (reshape (x, size (x,1 ), :)), :, size (x)[2 : end ]. .. )
152
148
153
149
function Base. show (io:: IO , l:: Dense )
0 commit comments