Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Momentum policy #5

Merged
merged 3 commits into from
Nov 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cifar10/cifar10.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ lr_policy = LRPolicy.Staged(
(5000, LRPolicy.Fixed(0.00001)),
)
solver_params = SolverParameters(max_iter=70000,
regu_coef=0.004, momentum=0.9, lr_policy=lr_policy)
regu_coef=0.004, mom_policy=MomPolicy.Fixed(0.9), lr_policy=lr_policy)
solver = SGD(solver_params)

# report training progress every 200 iterations
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist/mnist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ init(sys)
common_layers = [conv_layer, pool_layer, conv2_layer, pool2_layer, fc1_layer, fc2_layer]
net = Net("MNIST-train", sys, [data_layer, common_layers..., loss_layer])

params = SolverParameters(max_iter=10000, regu_coef=0.0005, momentum=0.9,
params = SolverParameters(max_iter=10000, regu_coef=0.0005, mom_policy=MomPolicy.Fixed(0.9),
lr_policy=LRPolicy.Inv(0.01, 0.0001, 0.75))
solver = SGD(params)

Expand Down
4 changes: 2 additions & 2 deletions src/cuda/solvers/sgd.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function update_parameters(net::Net{CuDNNBackend}, solver::SGD, learning_rate, state, param_blob, hist_blob, gradient, data_type)
function update_parameters(net::Net{CuDNNBackend}, solver::SGD, learning_rate, momentum, state, param_blob, hist_blob, gradient, data_type)
# hist_blob = net.sys.momentum * hist_blob
CuBLAS.scal(net.sys.backend.cublas_ctx, length(hist_blob), convert(data_type, solver.params.momentum),
CuBLAS.scal(net.sys.backend.cublas_ctx, length(hist_blob), convert(data_type, momentum),
hist_blob.ptr, 1)
# hist_blob = learning_rate * gradient + hist_blob
CuBLAS.axpy(net.sys.backend.cublas_ctx, length(hist_blob), convert(data_type, learning_rate),
Expand Down
38 changes: 36 additions & 2 deletions src/solvers.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export SolverParameters
export SGD

export LearningRatePolicy, LRPolicy, get_learning_rate
export LearningRatePolicy, LRPolicy, get_learning_rate, MomentumPolicy, MomPolicy, get_momentum

export add_coffee_break, solve

Expand Down Expand Up @@ -75,9 +75,43 @@ function get_learning_rate(policy::LRPolicy.Staged, state::SolverState)
return get_learning_rate(policy.stages[policy.curr_stage][2], state)
end


############################################################
# Momentum policy
############################################################
abstract MomentumPolicy
module MomPolicy
using ..Mocha.MomentumPolicy
type Fixed <: MomentumPolicy
base_mom :: FloatingPoint
end

# min(base_mom * gamma ^ (floor(iter / stepsize)), max_mom)
type Step <: MomentumPolicy
base_mom :: FloatingPoint
gamma :: FloatingPoint
stepsize :: Int
max_mom :: FloatingPoint
end

type Linear <: MomentumPolicy
base_mom :: FloatingPoint
gamma :: FloatingPoint
stepsize :: Int
max_mom :: FloatingPoint
end

end # module MomPolicy

get_momentum(policy::MomPolicy.Fixed, state::SolverState) = policy.base_mom
get_momentum(policy::MomPolicy.Step, state::SolverState) =
min(policy.base_mom * policy.gamma ^ (floor(state.iter / policy.stepsize)), policy.max_mom)
get_momentum(policy::MomPolicy.Linear, state::SolverState) =
min(policy.base_mom + floor(state.iter / policy.stepsize) * policy.gamma, policy.max_mom)

@defstruct SolverParameters Any (
lr_policy :: LearningRatePolicy = LRPolicy.Fixed(0.01),
(momentum :: FloatingPoint = 0.9, 0 <= momentum < 1),
mom_policy :: MomentumPolicy = MomPolicy.Fixed(0.),
(max_iter :: Int = 0, max_iter > 0),
(regu_coef :: FloatingPoint = 0.0005, regu_coef >= 0),
)
Expand Down
7 changes: 4 additions & 3 deletions src/solvers/sgd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function solve(sgd::SGD, net::Net)

obj_val = forward_backward(net, sgd.params.regu_coef)
learning_rate = get_learning_rate(sgd.params.lr_policy, solver_state)
momentum = get_momentum(sgd.params.mom_policy, solver_state)

# update parameters
for i = 1:length(param_states)
Expand All @@ -36,7 +37,7 @@ function solve(sgd::SGD, net::Net)
gradient = state.parameters[j].gradient
data_type = eltype(hist_blob)

update_parameters(net, sgd, state.parameters[j].learning_rate * learning_rate,
update_parameters(net, sgd, state.parameters[j].learning_rate * learning_rate, momentum,
state, state.parameters[j].blob, hist_blob, gradient, data_type)
end
end
Expand All @@ -53,9 +54,9 @@ function solve(sgd::SGD, net::Net)
map(x -> map(destroy, x), param_history)
end

function update_parameters(net::Net{CPUBackend}, solver::SGD, learning_rate, state, param_blob, hist_blob, gradient, data_type)
function update_parameters(net::Net{CPUBackend}, solver::SGD, learning_rate, momentum, state, param_blob, hist_blob, gradient, data_type)
# hist_blob = momentum * hist_blob
BLAS.scal!(length(hist_blob), convert(data_type, solver.params.momentum), hist_blob.data, 1)
BLAS.scal!(length(hist_blob), convert(data_type, momentum), hist_blob.data, 1)
# hist_blob = learning_rate * gradient + hist_blob
BLAS.axpy!(length(hist_blob), convert(data_type, learning_rate), gradient.data, 1, hist_blob.data, 1)

Expand Down