-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.jl
198 lines (156 loc) · 4.07 KB
/
test.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
include("./src/binary_tree.jl")
include("./src/helper.jl")
import Flux.Params
import Functors.functor
using CUDA
function test_params()
A = NNContainer(Conv((3, ), 1=>10; pad=1))
# layer = fmap(cu, A.layer)
# A = NNContainer(layer)
A = fmap(cu, A)
x = rand(Float32, (10, 1, 20)) |> cu
A.layer(x)
@show device(params(A))
B = conv_ensemble(1, 10)
# B = activation_ensemble()
B = fmap(cu, B)
p = params(B)
# @show p
for param in p.order.data
@show typeof(param), size(param)
end
@show device(p)
C = conv_ensemble(1, 10)
C = Chain_ensemble([B, C])
C = fmap(cu, C)
# C = gen_conv_ensemble(1, 10, 10, 10)
p = params(C)
# @show cp
for param in p.order.data
@show typeof(param), size(param)
end
@show device(p)
end
function test_random_graph()
x = rand(Float32, 10, 1, 20) #|> cu
g = random_graph(:in, :out, x; depth=15, inchannel=1, insize=10)
average_infer!(g)
@show size(g.data[:out])
end
function test_Cell()
out_channel = 10
inchannel = 1
insize = 10
en_left = gen_conv_ensemble(inchannel, out_channel, insize, insize)
en_right = gen_conv_ensemble(inchannel, out_channel, insize, insize)
op = sum_op()
out_id = :out
input_symbol = :in
cell = Cell(en_left, en_right, op, input_symbol, input_symbol, out_id)
cell = fmap(cu, cell)
p = params(cell)
for param in p.order.data
@show typeof(param), size(param)
end
@show device(p)
end
function test_Graph()
g = random_graph(:in, :out, x; depth=15, inchannel=1, insize=10)
g = fmap(cu, g)
p = params(g)
for param in p.order.data
@show typeof(param), size(param)
end
@show device(p)
end
using Flux.Optimise: update!
function test_train()
x = rand(Float32, 10, 1, 20) |> cu
g = random_graph(:in, :out, x; depth=15, inchannel=1, insize=10)
g = fmap(cu, g)
function loss(g)
average_infer!(g)
return sum(g.data[:out])
end
opt = Descent(0.01)
for i in 1:10
@show loss(g)
grads = gradient(() -> loss(g), params(g))
for p in params(g)
grad = grads[p]
if !(grad isa Nothing)
# @show size(grad)
update!(opt, p, grad)
end
end
end
end
function test_htrain()
x = rand(Float32, 10, 1, 20) |> cu
g = random_graph(:in, :out, x; depth=15, inchannel=1, insize=10)
# g = fmap(cu, g)
g = gpu(g)
function loss(g)
average_infer!(g)
return sum(g.data[:out])
end
# @show device(hparams(g))
# @show device(params(g))
# @show loss(g)
# error()
opt = Descent(0.1)
for i in 1:10
@show loss(g)
grads = gradient(() -> loss(g), hparams(g))
for p in hparams(g)
grad = grads[p]
if !(grad isa Nothing)
# @show size(grad)
update!(opt, p, grad)
end
end
end
end
test_htrain()
function test_zygote()
W = rand(Float32, (10, 10)) |> cu
function loss(x)
data = Dict()
for i in 1:5
x = W * x
x = x ./ sum(x)
data[gensym()] = x
end
# return sum(map(keys(data)) do k; return sum(data[k]); end)
# l = zeros(Float32, (10, )) |> cu
l = nothing
for k in keys(data)
li = data[k]
if l isa Nothing
l = li
else
l = deepcopy(l) .+ li
end
end
return sum(l)
end
@show loss(rand(Float32, (10, )) |> cu)
x = rand(Float32, (10, )) |> cu
grads = gradient(() -> loss(x), Params([W, ]))
end
# average_infer!(g)
# @show size(g.data[:out])
# p = params(A)
# @show fieldnames(typeof(p))
# # @show p.order
# # @show p.params
# fa = functor(A)
# @show fa
# # ps = Params([p, p])
# # @show typeof(ps)
# x = rand(Float32, (10, 1, 20))
# sum(A.layer(x))
# grads = gradient(() -> sum(A.layer(x)), p)
# @show fieldnames(typeof(grads))
# # @show grads.grads
# # en = conv_ensemble