You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've implemented the forward pass of the max pooling, but I wasn't able to use the index to perform the backward pass, your version, uses the img_to_row tricks, but I want to implement this without too much indexing and looping. Any idea how you can do this using this implementation?
importnumpyasnpas_strided=as_strideddefpool2d(Z, K:tuple=(2,2)):
""" performs the windowing, and padding if needed"""KH, KW=K# Kernel Height & WidthN, C, ZH, ZW=Z.shape# Input: NCHW Batch, Channels, Height, WidthNs, Cs, Hs, Ws=Z.stridesEdgeH, EdgeW=ZH%KH, ZW%KW# How many pixels left on the edgeif (EdgeH!=0orEdgeW!=0): # If there are pixels left we need to padPadH, PadW=KH-EdgeH, KW-EdgeWPadTop, PadBottom=ceil(PadH/2), floor(PadH/2)
PadLeft, PadRight=ceil(PadW/2), floor(PadW/2)
Z=np.pad(Z, ((0,0),(0,0), (PadTop, PadBottom), (PadLeft, PadRight)))
N, C, ZH, ZW=Z.shape#NCHWNs, Cs, Hs, Ws=Z.stridesZstrided=np.lib.stride_tricks.as_strided(Z, shape=(N,C,ZH//KH, ZW//KW, KH, KW), strides=(Ns, Cs, Hs*KH, Ws*KW,Hs, Ws))
returnZstrided.reshape(N,C,ZH//KH, ZW//KW, KH*KW) # reshape to flatten pooling windows to be 1D-vectordefmaxpool2d(Z, K:tuple=(2,2)):
ZP=pool2d(Z, K)
MxP=np.max(ZP, axis=(-1))
Inx=np.argmax(ZP, axis=-1)
returnMxP, Inxdefunmaxpool2d(ZP, Indx, K:tuple=(2,2)):
ZN,ZC,ZH,ZW=ZP.shapeKH, KW=KZ=np.zeros((ZN,ZC,ZH*KH,ZW*KW))
_ZP=pool2d(Z, K)
# ... Where to go next
The text was updated successfully, but these errors were encountered:
I've implemented the forward pass of the max pooling, but I wasn't able to use the index to perform the backward pass, your version, uses the img_to_row tricks, but I want to implement this without too much indexing and looping. Any idea how you can do this using this implementation?
The text was updated successfully, but these errors were encountered: