-
-
Notifications
You must be signed in to change notification settings - Fork 122
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
Reflect mode parameter from NNlib #53
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,15 @@ padtuple(x::Tuple,p::Integer) = map(_->p, head(head(x))) | |
padtuple(x::Tuple,p::Tuple) = p | ||
padtuple(x::AbstractArray,p) = padtuple(size(x),p) | ||
|
||
function conv(x::A, w::A; pad = 0, stride = 1, dilation = 1) where A<:AbstractArray | ||
function conv(x::A, w::A; pad = 0, stride = 1, dilation = 1, flipkernel=true) where A<:AbstractArray | ||
pad_, stride_ = padtuple(x, pad), padtuple(x, stride) | ||
if flipkernel | ||
conv!(similar(x, cdims(size(x), dilation_dims(w, dilation), pad_, stride_)), | ||
x, w, pad = pad_, stride = stride_, dilation = dilation) | ||
x, w, pad = pad_, stride = stride_, dilation = dilation, flipkernel=flipkernel) | ||
else | ||
crosscor!(similar(x, cdims(size(x), dilation_dims(w, dilation), pad_, stride_)), | ||
x, w, pad = pad_, stride = stride_, dilation = dilation, flipkernel=flipkernel) | ||
end | ||
end | ||
|
||
∇conv_data(dy::A, x::A, w::A; pad = 0, stride = 1, dilation = 1) where A<:AbstractArray = | ||
|
@@ -39,9 +44,10 @@ end | |
# N-D dispatch | ||
|
||
function conv!(y::AbstractArray{T,3}, x::AbstractArray{T,3}, w::AbstractArray{T,3}; | ||
pad = 0, stride = 1, dilation = 1) where T | ||
pad = 0, stride = 1, dilation = 1, flipkernel=true) where T | ||
args = map(x -> reshape(x, size(x,1),1,size(x,2),size(x,3)), (y, x, w)) | ||
conv!(args..., pad = (pad...,0), stride = (stride...,1), dilation = (dilation...,1)) | ||
conv!(args..., pad = (pad...,0), stride = (stride...,1), dilation = (dilation...,1), | ||
flipkernel= flipkernel) | ||
return y | ||
end | ||
|
||
|
@@ -62,8 +68,12 @@ function ∇conv_data!(dx::AbstractArray{T,3}, dy::AbstractArray{T,3}, | |
end | ||
|
||
conv!(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}; | ||
pad = 0, stride = 1, dilation = 1) where T = | ||
conv2d!(y, x, w, padding = pad, stride = stride, dilation = dilation) | ||
pad = 0, stride = 1, dilation = 1, flipkernel=true) where T = | ||
conv2d!(y, x, w, padding = pad, stride = stride, dilation = dilation, flipkernel=flipkernel) | ||
|
||
crosscor!(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cross correlation should have |
||
pad = 0, stride = 1, dilation = 1, flipkernel=true) where T = | ||
conv!(y, x, w, pad = pad, stride = stride, dilation = dilation, flipkernel=flipkernel) | ||
|
||
∇conv_filter!(dw::AbstractArray{T,4}, dy::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}; | ||
pad = 0, stride = 1, dilation = 1) where T = | ||
|
@@ -74,8 +84,8 @@ conv!(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}; | |
conv2d_grad_x!(dx, x, w, dy, padding = pad, stride = stride, dilation = dilation) | ||
|
||
conv!(y::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5}; | ||
pad = 0, stride = 1, dilation = 1) where T = | ||
conv3d!(y, x, w, padding = pad, stride = stride, dilation = dilation) | ||
pad = 0, stride = 1, dilation = 1, flipkernel=true) where T = | ||
conv3d!(y, x, w, padding = pad, stride = stride, dilation = dilation, flipkernel=flipkernel) | ||
|
||
∇conv_filter!(dw::AbstractArray{T,5}, dy::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5}; | ||
pad = 0, stride = 1, dilation = 1) where T = | ||
|
@@ -91,14 +101,14 @@ function dcdims(x::NTuple{4,Int}, w::NTuple{4,Int}, pad, stride) | |
((x[1] + 2 * pad[1] - w[1])÷stride[1] + 1,(x[2] + 2 * pad[2] - w[2])÷stride[2] + 1,w[3]*w[4],x[4]) | ||
end | ||
|
||
function depthwiseconv(x::A, w::A; pad = 0, stride = 1) where A<:AbstractArray | ||
function depthwiseconv(x::A, w::A; pad = 0, stride = 1, flipkernel = false) where A<:AbstractArray | ||
pad_, stride_ = padtuple(x, pad), padtuple(x, stride) | ||
depthwiseconv!(similar(x, dcdims(size(x), size(w), pad_, stride_)), x, w, pad = pad_, stride = stride_) | ||
depthwiseconv!(similar(x, dcdims(size(x), size(w), pad_, stride_)), x, w, pad = pad_, stride = stride_, flipkernel = flipkernel) | ||
end | ||
|
||
depthwiseconv!(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}; | ||
pad = 0, stride = 1) where T = | ||
depthwiseconv2d!(y, x, w, padding = pad, stride = stride) | ||
pad = 0, stride = 1, flipkernel = true) where T = | ||
depthwiseconv2d!(y, x, w, padding = pad, stride = stride, flipkernel = flipkernel) | ||
|
||
∇depthwiseconv_data(dy::A, x::A, w::A; pad = 0, stride = 1) where A<:AbstractArray = | ||
∇depthwiseconv_data!(zeros(x), dy, x, w; pad = pad, stride = stride) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't make sense to do an
if/else
here; you passflipkernel
through so removing it would behave the same way.It would however be good to have an out-of-place
crosscor
function that callscrosscor!
.