Skip to content
Open
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
16 changes: 9 additions & 7 deletions src/impl/pooling_direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ for name in (:max, :mean, :lpnorm)
elseif $(name == :mean)
m += x[input_kw, input_kh, input_kd, c, batch_idx]
elseif $(name == :lpnorm)
# y = (∑ᵢ xᵢ^p)^(1 / p), here to calculate ∑ᵢ xᵢ^p
m += x[input_kw, input_kh, input_kd, c, batch_idx]^p
# y = (∑ᵢ |xᵢ|^p)^(1 / p), here to calculate ∑ᵢ |xᵢ|^p
m += abs(x[input_kw, input_kh, input_kd, c, batch_idx])^p
else
error("Unimplemented codegen path")
end
end

# for lpnormpool, y = (∑ᵢ xᵢ^p)^(1 / p)
# for lpnormpool, y = (∑ᵢ |xᵢ|^p)^(1 / p)
m = $(name == :lpnorm) ? m^(T(1) / p) : m

y[w, h, d, c, batch_idx] = _alpha * m # + _beta * y[w, h, d, c, batch_idx]
Expand Down Expand Up @@ -151,7 +151,7 @@ for name in (:max, :mean, :lpnorm)
elseif $(name == :mean)
m += x[input_kw, input_kh, input_kd, c, batch_idx]
elseif $(name == :lpnorm)
m += x[input_kw, input_kh, input_kd, c, batch_idx]^p
m += abs(x[input_kw, input_kh, input_kd, c, batch_idx])^p
else
error("Unimplemented codegen path")
end
Expand Down Expand Up @@ -263,8 +263,9 @@ for name in (:max, :mean, :lpnorm)
# Either does meanpool :(
dx[input_kw, input_kh, input_kd, c, batch_idx] += dy_idx * _alpha
elseif $(name == :lpnorm)
# y = (∑ᵢ xᵢ^p)^(1 / p), ∂y/∂xᵢ = xᵢ^(p-1) × y^(1-p)
grad = x[input_kw, input_kh, input_kd, c, batch_idx]^(p-1) * y_idx^(1-p)
# y = (∑ᵢ |xᵢ|^p)^(1 / p), ∂y/∂xᵢ = |xᵢ|^(p-1) × y^(1-p) × sign(xᵢ)
xv = x[input_kw, input_kh, input_kd, c, batch_idx]
grad = abs(xv)^(p-1) * y_idx^(1-p) * sign(xv)
Copy link
Member

Choose a reason for hiding this comment

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

why the sign here?

Copy link
Author

Choose a reason for hiding this comment

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

That comes from the derivative of abs

dx[input_kw, input_kh, input_kd, c, batch_idx] += dy_idx * grad
else
error("Unimplemented codegen path")
Expand Down Expand Up @@ -327,7 +328,8 @@ for name in (:max, :mean, :lpnorm)
elseif $(name == :mean)
dx[input_kw, input_kh, input_kd, c, batch_idx] += dy_idx * _alpha #+ _beta * dx[x_idxs...]
elseif $(name == :lpnorm)
grad = x[input_kw, input_kh, input_kd, c, batch_idx]^(p-1) * y_idx^(1-p)
xv = x[input_kw, input_kh, input_kd, c, batch_idx]
grad = abs(xv)^(p-1) * y_idx^(1-p) * sign(xv)
dx[input_kw, input_kh, input_kd, c, batch_idx] += dy_idx * grad
else
error("Unimplemented codegen path")
Expand Down
Loading