Skip to content

Instantly share code, notes, and snippets.

@Gnimuc
Last active September 27, 2017 11:40
Show Gist options
  • Save Gnimuc/0172c390d50641f7e07d4181434b17bf to your computer and use it in GitHub Desktop.
Save Gnimuc/0172c390d50641f7e07d4181434b17bf to your computer and use it in GitHub Desktop.
# this is actually cross-correlation
function toyconv(x::AbstractArray{T,3}, w::AbstractArray{T,4}, pad::Integer, _stride::Integer) where T<:Real
xSpatialDims = size(x, 1, 2)
kernelSpatialDims = size(w, 1, 2)
channelNum, kernelNum = size(w, 3, 4)
ySpatialDims = 1 .+ (xSpatialDims .+ 2pad .- kernelSpatialDims) .÷ _stride
δ = kernelSpatialDims[1] ÷ 2
padSpatialDims = xSpatialDims .+ 2pad
xOffset = OffsetArray(x, pad, pad, 0)
xPadded = PaddedView(zero(T), xOffset, (padSpatialDims..., channelNum))
y = zeros(T, ySpatialDims..., kernelNum)
for kn = 1:kernelNum, cn = 1:channelNum
for j = 1:ySpatialDims[2], i = 1:ySpatialDims[1]
iPad = _stride*(i-1) + pad - δ
jPad = _stride*(j-1) + pad - δ
for m = 1:kernelSpatialDims[2], n = 1:kernelSpatialDims[1]
y[i,j,kn] += xPadded[iPad+n,jPad+m,cn] * w[n,m,cn,kn]
end
end
end
y
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment