-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.jl
148 lines (100 loc) · 4.38 KB
/
train.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
using Flux.Data: DataLoader
using Flux: train!, flatten, crossentropy
using Flux
using Optim, FluxOptTools
using Random
using LineSearches
using ParametricMachinesDemos
using Optim: optimize, only_fg!, minimizer
function train_classification(x_train::AbstractArray, y_train, x_test::AbstractArray, y_test,
machine_type, dimensions, loss; embedder = identity, timeblock = 1, pad = 1, opt = "Adam", learning_rate = 0.01, line_search = BackTracking(),
n_epochs=100, device=cpu)
Random.seed!(3)
if machine_type == DenseMachine
machine = machine_type(dimensions, sigmoid);
model = Flux.Chain(embedder, machine, Dense(sum(dimensions), 2), softmax) |> device
end
if machine_type == RecurMachine
machine = machine_type(dimensions, sigmoid; pad=pad, timeblock=timeblock)
model = Flux.Chain(machine, Conv((1,), sum(dimensions) => 2), flatten, Dense(192,2), softmax) |> device
end
params = Flux.params(model)
if opt == "Adam"
return train_ADAM(x_train, y_train, x_test, y_test, loss, learning_rate, params, model, n_epochs)
end
if opt == "LBFGS"
return train_LBFGS(x_train, y_train, x_test, y_test, model, line_search, params, n_epochs)
end
if opt == "ConjugateGradient"
return train_ConjGrad(x_train, y_train, x_test, y_test, model, line_search, params, n_epochs)
end
@error "This optimizer has not been implemented yet."
end
function train_ADAM(x_train::AbstractArray, y_train,
x_test::AbstractArray, y_test, loss,
learning_rate, params, model, n_epochs)
Random.seed!(28)
train_data = DataLoader((x_train, y_train); batchsize = 32, shuffle = true);
opt = ADAM(learning_rate);
@info "Starting training."
loss_on_train = Float64[]
acc_train = Float64[]
acc_test = Float64[]
best_params = Float32[]
for epoch in 1:n_epochs
for (x,y) in train_data
# Train
# train!(loss, params, train_data, opt)
gs = gradient(params) do
loss(model, x, y)
end
Flux.Optimise.update!(opt, params, gs)
end
# Saving losses and accuracies for visualization
push!(loss_on_train, loss(model, x_train, y_train))
push!(acc_train, accuracy(y_train, model(x_train)))
push!(acc_test, accuracy(y_test, model(x_test)))
@show loss(model, x_train, y_train)
# Saving the best parameters
if epoch > 1
if is_best(loss_on_train[epoch-1], loss_on_train[epoch])
best_params = params
end
end
end
if isempty(best_params)
best_params = params
end
Flux.loadparams!(model, best_params);
return best_params, model, loss_on_train, acc_train, acc_test
end
function train_LBFGS(x_train::AbstractArray, y_train::Flux.OneHotArray,
x_test::AbstractArray, y_test::Flux.OneHotArray,
model, line_search, params, n_epochs)
@info "Starting training."
loss() = crossentropy(model(x_train), y_train);
_, _, fg!, p0 = optfuns(loss, params)
res = Optim.optimize(Optim.only_fg!(fg!), p0, LBFGS(linesearch = line_search), Optim.Options(iterations=n_epochs, store_trace=true))
best_params = res.minimizer
copy!(params, best_params)
Flux.loadparams!(model, params)
acc_train = accuracy(y_train, model(x_train))
acc_test = accuracy(y_test, model(x_test))
loss_on_train = crossentropy(model(x_train), y_train)
return params, model, loss_on_train, acc_train, acc_test
end
function train_ConjGrad(x_train::AbstractArray, y_train::Flux.OneHotArray,
x_test::AbstractArray, y_test::Flux.OneHotArray,
model, line_search, params, n_epochs)
@info "Starting training."
loss() = crossentropy(model(x_train), y_train);
lossfun, gradfun, fg!, p0 = optfuns(loss, params)
res = Optim.optimize(Optim.only_fg!(fg!), p0, ConjugateGradient(linesearch = line_search), Optim.Options(iterations=n_epochs, store_trace=true))
best_params = res.minimizer
copy!(params, best_params)
Flux.loadparams!(model, params)
acc_train = accuracy(y_train, model(x_train))
acc_test = accuracy(y_test, model(x_test))
loss_on_train = crossentropy(y_train, model(x_train))
return params, model, loss_on_train, acc_train, acc_test
end