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

New style relu #3175

Merged
merged 18 commits into from
Aug 24, 2017
Merged

New style relu #3175

merged 18 commits into from
Aug 24, 2017

Conversation

delta2323
Copy link
Member

This PR implements new-style version of F.relu.

This PR depends on #3096, and is a part of #3147.

Copy link
Member

@beam2d beam2d left a comment

Choose a reason for hiding this comment

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

Added some comments.

self.retain_outputs((0,))
self._use_cudnn = False
Copy link
Member

Choose a reason for hiding this comment

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

I think it's simpler to make _use_cudnn = False a class attribute and only sets explicitly on cuDNN path.
I mean:

class ReLU(function_node.FunctionNode):

    _use_cudnn = False

    ...

    def forward_gpu(self, x):
        if ...:
            self._use_cudnn = True
            ...
        ...

return ReLUGrad2().apply((y, gy[0]))


class Zero(function_node.FunctionNode):
Copy link
Member

Choose a reason for hiding this comment

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

Is it needed?

return Zero().apply(gy)


class Heaviside(function_node.FunctionNode):
Copy link
Member

Choose a reason for hiding this comment

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

The following code simplifies it.

def heaviside(x):
    return utils.force_array((x.data > 0).astype(x.dtype))

Copy link
Member

@beam2d beam2d left a comment

Choose a reason for hiding this comment

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

Thank you. I added some more comments.

def backward(self, indexes, gy):
ret = []
if 0 in indexes:
ret.append(None)
Copy link
Member

Choose a reason for hiding this comment

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

Could you remove the first argument from inputs and instead pass it as an argument of __init__? (It will simplifies the backprop, which is good for performance)

def backward(self, indexes, gy):
ret = []
if 0 in indexes:
ret.append(None)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

if 0 in indexes:
ret.append(None)
if 1 in indexes:
ret.append(None)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -12,48 +12,103 @@
_mode = cudnn.cudnn.CUDNN_ACTIVATION_RELU


class ReLU(function.Function):
class ReLU(function_node.FunctionNode):

"""Rectified Linear Unit."""
# TODO(beam2d): Implement in-place version.
Copy link
Member

Choose a reason for hiding this comment

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

It is not directly releated to this PR, but I found this TODO comment is obsolete. Can you remove it?

@delta2323
Copy link
Member Author

delta2323 commented Aug 18, 2017

As #3096 is merged to the master branch, I rebased the PR.

@delta2323
Copy link
Member Author

Thank you for your comments. I updated the PR. Note that although I wrote the docstrings of ReLUGrad2 and ReLUGrad3, I do not intend to make them official APIs.

Copy link
Member

@beam2d beam2d left a comment

Choose a reason for hiding this comment

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

I added some more comments.

Note: I noted that "you do not need to check if indexes is empty", but I found that Variable.backward() does not check it correctly. I'll fix this point in another PR, so it's ok to proceed with removing the check.

return gx,

def backward(self, indexes, gy):
if 0 in indexes:
Copy link
Member

Choose a reason for hiding this comment

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

You do not need to check this; indexes is always non-empty (otherwise backward is not called).

Copy link
Member Author

Choose a reason for hiding this comment

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

OK. I have removed it.

return cudnn.activation_backward(a, b, inputs[0], _mode),

def backward(self, indexes, gy):
if 0 in indexes:
Copy link
Member

Choose a reason for hiding this comment

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

Remove the check (see the above comment)



def _heaviside(x):
return utils.force_array((x > 0).astype(x.dtype))
Copy link
Member

Choose a reason for hiding this comment

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

Is this force_array needed?

def backward(self, indexes, gy):
if 0 in indexes:
xp = cuda.get_array_module(gy[0])
b = xp.asarray(self.b)
Copy link
Member

Choose a reason for hiding this comment

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

Is this asarray needed?

@delta2323
Copy link
Member Author

Thank you. Updated.

@beam2d beam2d mentioned this pull request Aug 21, 2017
27 tasks
@niboshi niboshi added the cat:feature Implementation that introduces new interfaces. label Aug 22, 2017
@beam2d
Copy link
Member

beam2d commented Aug 23, 2017

Please resolve the flake8 errors.

@beam2d
Copy link
Member

beam2d commented Aug 23, 2017

Jenkins, test this please

Copy link
Member

@beam2d beam2d left a comment

Choose a reason for hiding this comment

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

One more comment

"""

def __init__(self, b):
super(ReLUGrad2).__init__()
Copy link
Member

Choose a reason for hiding this comment

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

, self is missing (or remove this line; it is allowed to not call super init in FunctionNode)

@beam2d
Copy link
Member

beam2d commented Aug 24, 2017

LGTM

@beam2d beam2d merged commit c8c9aec into chainer:master Aug 24, 2017
@beam2d
Copy link
Member

beam2d commented Aug 24, 2017

Thank you!

@beam2d beam2d added this to the v3.0.0rc1 milestone Aug 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat:feature Implementation that introduces new interfaces.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants