Skip to content

Instantly share code, notes, and snippets.

@nishnik
Created November 14, 2017 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishnik/f9041eff871d269eada6decb9454b5d2 to your computer and use it in GitHub Desktop.
Save nishnik/f9041eff871d269eada6decb9454b5d2 to your computer and use it in GitHub Desktop.
Basic implementation of maxpool2d in pytorch, based on it you can create kmaxpool
stride = 3
new_var = Variable(torch.zeros([x.shape[0], x.shape[1]//stride, x.shape[2]//stride]))
for dim1 in range(x.shape[0]):
tmp = Variable(torch.zeros([x.shape[1]//stride, x.shape[2]//stride, 1]))
for i in range(0, x.shape[1], stride):
for j in range(0, x.shape[2], stride):
tmp_max = x[dim1][i][j]
for k in range(stride):
for m in range(stride):
tmp_max = torch.max(tmp_max, x[dim1][i+k][j+m])
tmp[i//stride, j//stride] = tmp_max
new_var[dim1] = tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment