-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
basic.jl
271 lines (228 loc) · 9.39 KB
/
basic.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using Test, Random
import Flux: activations
@testset "basic" begin
@testset "helpers" begin
@testset "activations" begin
dummy_model = Chain(x->x.^2, x->x .- 3, x -> tan.(x))
x = randn(10)
@test activations(dummy_model, x)[1] == x.^2
@test activations(dummy_model, x)[2] == (x.^2 .- 3)
@test activations(dummy_model, x)[3] == tan.(x.^2 .- 3)
@test activations(Chain(), x) == ()
@test activations(Chain(identity, x->:foo), x)[2] == :foo # results include `Any` type
end
end
@testset "Chain" begin
@test_nowarn Chain(Dense(10, 5, σ), Dense(5, 2))(randn(10))
@test_throws DimensionMismatch Chain(Dense(10, 5, σ),Dense(2, 1))(randn(10))
# numeric test should be put into testset of corresponding layer
@test_nowarn Chain(first = Dense(10, 5, σ), second = Dense(5, 2))(randn(10))
m = Chain(first = Dense(10, 5, σ), second = Dense(5, 2))
@test m[:first] == m[1]
@test m[1:2] == m
@test_throws ArgumentError Chain(layers = Dense(10, 10), two = identity) # reserved name
end
@testset "Activations" begin
c = Chain(Dense(3,5,relu), Dense(5,1,relu))
X = Float32.([1.0; 1.0; 1.0])
@test_nowarn gradient(()->Flux.activations(c, X)[2][1], params(c))
c2 = Chain(enc = c[1], dec = c[2])
@test Flux.activations(c, X) == Flux.activations(c2, X)
@test_nowarn gradient(()->Flux.activations(c2, X)[2][1], params(c2))
end
@testset "Dense" begin
@testset "constructors" begin
@test size(Dense(10, 100).weight) == (100, 10)
@test size(Dense(10, 100).bias) == (100,)
@test Dense(rand(100,10), rand(100)).σ == identity
@test Dense(rand(100,10)).σ == identity
@test Dense(rand(100,10), false).σ == identity
@test Dense(rand(100,10), false, tanh).σ == tanh
@test Dense(rand(100,10), rand(100)).σ == identity
@test Dense(rand(Float16, 100,10), true).bias isa Vector{Float16} # creates matching type
@test_skip Dense(rand(Float16, 100,10), rand(100)).bias isa Vector{Float16} # converts to match
@test Dense(3,4; init=Base.randn, bias=true).bias isa Vector{Float64}
@test_skip Dense(3,4; init=Base.randn, bias=[1,2,3,4]).bias isa Vector{Float64}
@test_throws MethodError Dense(10, 10.5)
@test_throws MethodError Dense(10, 10.5, tanh)
@test_throws DimensionMismatch Dense(3,4; bias=rand(5))
@test_throws DimensionMismatch Dense(rand(4,3), rand(5))
@test_throws MethodError Dense(rand(5))
@test_throws MethodError Dense(rand(5), rand(5))
@test_throws MethodError Dense(rand(5), rand(5), tanh)
end
@testset "dimensions" begin
@test length(Dense(10, 5)(randn(10))) == 5
@test_throws DimensionMismatch Dense(10, 5)(randn(1))
@test_throws MethodError Dense(10, 5)(1) # avoid broadcasting
@test_throws MethodError Dense(10, 5).(randn(10)) # avoid broadcasting
@test size(Dense(10, 5)(randn(10))) == (5,)
@test size(Dense(10, 5)(randn(10,2))) == (5,2)
@test size(Dense(10, 5)(randn(10,2,3))) == (5,2,3)
@test size(Dense(10, 5)(randn(10,2,3,4))) == (5,2,3,4)
@test_throws DimensionMismatch Dense(10, 5)(randn(11,2,3))
end
@testset "zeros" begin
@test Dense(10, 1, identity, init = ones)(ones(10,1)) == 10*ones(1, 1)
@test Dense(10, 1, identity, init = ones)(ones(10,2)) == 10*ones(1, 2)
@test Dense(10, 2, identity, init = ones)(ones(10,1)) == 10*ones(2, 1)
@test Dense(10, 2, identity, init = ones)([ones(10,1) 2*ones(10,1)]) == [10 20; 10 20]
@test Dense(10, 2, identity, init = ones, bias = false)([ones(10,1) 2*ones(10,1)]) == [10 20; 10 20]
end
end
@testset "Diagonal" begin
@test length(Flux.Diagonal(10)(randn(10))) == 10
@test length(Flux.Diagonal(10)(1)) == 10
@test length(Flux.Diagonal(10)(randn(1))) == 10
@test_throws DimensionMismatch Flux.Diagonal(10)(randn(2))
@test Flux.Diagonal(2)([1 2]) == [1 2; 1 2]
@test Flux.Diagonal(2)([1,2]) == [1,2]
@test Flux.Diagonal(2)([1 2; 3 4]) == [1 2; 3 4]
@test Flux.Diagonal(2)(rand(2,3,4)) |> size == (2, 3, 4)
@test Flux.Diagonal(2,3)(rand(2,3,4)) |> size == (2, 3, 4)
@test Flux.Diagonal(2,3,4)(rand(2,3,4)) |> size == (2, 3, 4)
@test Flux.Diagonal(2,3)(rand(2,1,4)) |> size == (2, 3, 4)
end
@testset "Maxout" begin
# Note that the normal common usage of Maxout is as per the docstring
# These are abnormal constructors used for testing purposes
@testset "Constructor" begin
mo = Maxout(() -> identity, 4)
input = rand(40)
@test mo(input) == input
end
@testset "simple alternatives" begin
mo = Maxout((x -> x, x -> 2x, x -> 0.5x))
input = rand(40)
@test mo(input) == 2*input
end
@testset "complex alternatives" begin
mo = Maxout((x -> [0.5; 0.1]*x, x -> [0.2; 0.7]*x))
input = [3.0 2.0]
target = [0.5, 0.7].*input
@test mo(input) == target
end
@testset "params" begin
mo = Maxout(()->Dense(32, 64), 4)
ps = params(mo)
@test length(ps) == 8 #4 alts, each with weight and bias
end
end
@testset "SkipConnection" begin
@testset "zero sum" begin
input = randn(10, 10, 10, 10)
@test SkipConnection(x -> zeros(size(x)), (a,b) -> a + b)(input) == input
end
@testset "concat size" begin
input = randn(10, 2)
@test size(SkipConnection(Dense(10,10), (a,b) -> cat(a, b, dims = 2))(input)) == (10,4)
end
end
@testset "Bilinear" begin
@testset "SkipConnection recombinator" begin
d = Dense(10, 10)
b = Flux.Bilinear(10, 10, 5)
x = randn(Float32,10,9)
sc = SkipConnection(d, b)
@test size(sc(x)) == (5,9)
end
@testset "Two-streams zero sum" begin
x = zeros(Float32,10,9)
y = zeros(Float32,2,9)
b = Flux.Bilinear(10, 2, 3)
@test size(b(x,y)) == (3,9)
@test sum(abs2, b(x,y)) == 0f0
end
@testset "Inner interactions" begin
x = randn(Float32,11,7)
b = Flux.Bilinear(11, 11, 3)
@test size(b(x)) == (3,7)
@test_nowarn gs = gradient(() -> sum(abs2.(b(x))), params(b))
end
@testset "constructors" begin
b1 = Flux.Bilinear(randn(3,4,5))
@test b1.bias isa Vector{Float64}
@test b1.σ == identity
b2 = Flux.Bilinear(randn(3,4,5), false)
@test b2.bias == Flux.Zeros()
b3 = Flux.Bilinear(randn(Float16, 3,4,5), true, tanh)
@test b3.σ == tanh
@test b3.bias isa Vector{Float16}
@test size(b3(rand(4), rand(5))) == (3,)
b4 = Flux.Bilinear(3,3,7; bias=1:7, init=Flux.zeros32)
@test_skip b4.bias isa Vector{Float32}
@test_throws ArgumentError Flux.Bilinear(rand(3)) # expects a 3-array
@test_throws ArgumentError Flux.Bilinear(rand(3,4), false, tanh)
@test_throws DimensionMismatch Flux.Bilinear(rand(3,4,5), rand(6), tanh) # wrong length bias
end
end
@testset "Parallel" begin
@testset "zero sum" begin
input = randn(10, 10, 10, 10)
@test Parallel(+, x -> zeros(size(x)), identity)(input) == input
end
@testset "concat size" begin
input = randn(10, 2)
@test size(Parallel((a, b) -> cat(a, b; dims=2), Dense(10, 10), identity)(input)) == (10, 4)
@test size(Parallel(hcat, one = Dense(10, 10), two = identity)(input)) == (10, 4)
end
@testset "vararg input" begin
inputs = randn(10), randn(5), randn(4)
@test size(Parallel(+, Dense(10, 2), Dense(5, 2), Dense(4, 2))(inputs)) == (2,)
@test size(Parallel(+; a = Dense(10, 2), b = Dense(5, 2), c = Dense(4, 2))(inputs)) == (2,)
end
@testset "named access" begin
m = Parallel(hcat, one = Dense(10, 10), two = identity)
@test m[1] == m[:one]
@test_throws ArgumentError Parallel(hcat, layers = Dense(10, 10), two = identity) # reserved names
@test_throws ArgumentError Parallel(hcat, connection = Dense(10, 10), two = identity)
end
# Ref https://github.com/FluxML/Flux.jl/issues/1673
@testset "Input domain" begin
struct Input
x
end
struct L1
x
end
(l::L1)(x) = l.x * x
Flux.@functor L1
Base.:*(a::AbstractArray, b::Input) = a * b.x
par = Parallel(+, L1(rand(Float32, 3,3)), L1(rand(Float32, 3,3)))
ip = Input(rand(Float32, 3,3))
ip2 = Input(rand(Float32, 3,3))
@test par(ip) ≈ par.layers[1](ip.x) + par.layers[2](ip.x)
@test par(ip, ip2) ≈ par.layers[1](ip.x) + par.layers[2](ip2.x)
gs = gradient((par, x...) -> sum(par(x...)), par, ip, ip2)
gs_reg = gradient(par, ip, ip2) do par, x, y
sum(par.layers[1](x.x) + par.layers[2](y.x))
end
for (a,b) in zip(gs[1].layers, gs_reg[1].layers)
@test a.x ≈ b.x
end
@test gs[2].x ≈ gs_reg[2].x
@test gs[3].x ≈ gs_reg[3].x
end
end
@testset "Embedding" begin
vocab_size, embed_size = 10, 4
m = Flux.Embedding(vocab_size, embed_size)
@test size(m.weight) == (embed_size, vocab_size)
x = rand(1:vocab_size, 3)
y = m(x)
@test y isa Matrix{Float32}
@test y ≈ m.weight[:,x]
x2 = OneHotMatrix(x, vocab_size)
y2 = m(x2)
@test y2 isa Matrix{Float32}
@test y2 ≈ y
@test_throws DimensionMismatch m(OneHotMatrix(x, 1000))
x = rand(1:vocab_size, 3, 4)
y = m(x)
@test y isa Array{Float32, 3}
@test size(y) == (embed_size, 3, 4)
@test m(2) ≈ m.weight[:,2]
@test m(OneHotVector(3, vocab_size)) ≈ m.weight[:,3]
@test_throws DimensionMismatch m(OneHotVector(3, 1000))
end
end