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

Refine code(momentum_op, rmsprop_op ) #6000

Closed
wants to merge 2 commits into from

Conversation

chengduoZH
Copy link
Contributor

@chengduoZH chengduoZH commented Nov 28, 2017

fix #6002

@@ -50,8 +50,7 @@ class MomentumOpKernel : public framework::OpKernel<T> {

v_out.device(place) = v * mu + g;
if (use_nesterov) {
p_out.device(place) = p - g * lr.broadcast(grad_dsize) +
v_out * mu * lr.broadcast(grad_dsize);
p_out.device(place) = p - (g - v_out * mu) * lr.broadcast(grad_dsize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether the framework::EigenScalar can be used for lr instead of broadcast ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried it, but the result is wrong.

@@ -50,8 +50,7 @@ class MomentumOpKernel : public framework::OpKernel<T> {

v_out.device(place) = v * mu + g;
if (use_nesterov) {
p_out.device(place) = p - g * lr.broadcast(grad_dsize) +
v_out * mu * lr.broadcast(grad_dsize);
p_out.device(place) = p - (g - v_out * mu) * lr.broadcast(grad_dsize);
Copy link
Contributor

@dzhwinter dzhwinter Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implement is almost same.
it's better to remove the v_out.device

Copy link
Contributor Author

@chengduoZH chengduoZH Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I think it's better to remove the v_out.device, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that v_out and v point to the same memory, this is to say the writer use inplace method in python interface. And it is necessary to update v_out in momentum_op, so v_out.device can't be removed.

@chengduoZH
Copy link
Contributor Author

I find that the implementation of momentum_op seems to be wrong.
Document description:

velocity = mu * velocity + gradient 
if (use\_nesterov):   
  param = param - gradient * learning\_rate + mu * velocity * learning\_rate 
else:   
  param = param - learning\_rate * velocity. 

Code implementation:

v_out.device(place) = v * mu + g;
if (use_nesterov) {
p_out.device(place) = p - g * lr.broadcast(grad_dsize) +
v_out * mu * lr.broadcast(grad_dsize);
} else {
p_out.device(place) = p - lr.broadcast(grad_dsize) * v_out;
}

@chengduoZH
Copy link
Contributor Author

The writer use inplace method in python interface, this is to say that v_out and v point to the same memory. I think this should be annotated in the code.

momentum_op = block.append_op(
type=self.type,
inputs={
"Param": param_and_grad[0],
"Grad": param_and_grad[1],
"Velocity": velocity_acc,
"LearningRate": self._create_param_lr(param_and_grad)
},
outputs={
"ParamOut": param_and_grad[0],
"VelocityOut": velocity_acc
},
attrs={"mu": self._momentum,
"use_nesterov": self._use_nesterov})

@chengduoZH chengduoZH closed this Dec 5, 2017
@chengduoZH chengduoZH deleted the refine_code branch January 29, 2018 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refine code (momentum_op, rmsprop_op)
3 participants