Skip to content

Instantly share code, notes, and snippets.

@scott-gray
Created April 29, 2016 17:38
Show Gist options
  • Save scott-gray/20f511c8a192346b73eedf298a767eb9 to your computer and use it in GitHub Desktop.
Save scott-gray/20f511c8a192346b73eedf298a767eb9 to your computer and use it in GitHub Desktop.
import numpy as np
def ceil_div(x, y):
return -(-x // y)
def out_dim(S, X, padding, strides):
return ceil_div(X - S + 1 + 2*padding, strides)
def fconv_slice(q, S, X, padding, strides):
qs = q * strides - padding
x1 = None
for s in range(S):
x = qs + s
if x1 is None and x >= 0:
x1 = x
f1 = s
if x < X:
x2 = x
f2 = s
return slice(f1, f2+1), slice(x1, x2+1)
def fprop_direct(I, F, O, padding, strides):
C, H, W, N = I.shape
C, R, S, K = F.shape
K, P, Q, N = O.shape
qSlice = [ fconv_slice(q, S, W, padding[0], strides[0]) for q in range(Q) ]
for p in range(P):
sliceR, sliceY = fconv_slice(p, R, H, padding[1], strides[1])
for q in range(Q):
sliceS, sliceX = qSlice[q]
slicedF = F[:,sliceR,sliceS,:].reshape((-1, K))
slicedI = I[:,sliceY,sliceX,:].reshape((-1, N))
O[:,p,q,:] = np.dot( slicedF.T, slicedI )
N = 1
C, K = 1, 1
H, W = 4, 4
R, S = 3, 3
strides = 1, 1
padding = 1, 1
P = out_dim(R, H, padding[0], strides[0])
Q = out_dim(S, W, padding[1], strides[1])
dimI = (C,H,W,N)
dimF = (C,R,S,K)
dimO = (K,P,Q,N)
I = np.ones(dimI)
F = np.ones(dimF)
O = np.ones(dimO)
fprop_direct(I, F, O, padding, strides)
print O[0,:,:,0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment