天天看點

CS231n作業筆記2.6:卷積層以及池化層的實作CS231n簡介作業筆記

CS231n簡介

詳見 CS231n課程筆記1:Introduction。

本文都是作者自己的思考,正确性未經過驗證,歡迎指教。

作業筆記

就是簡單實作一下卷積層以及池化層,唯一的難點在于确定索引值,稍微注意一下就好。

1. 卷積層

1.1. 前向傳播

使用np.pad做填充。

N, C, H, W = x.shape
  F, C, HH, WW = w.shape
  stride = conv_param['stride']
  pad = conv_param['pad']
  H_new =  + (H +  * pad - HH) / stride
  W_new =  + (W +  * pad - WW) / stride

  x = np.pad(x,((,),(,),(pad,pad),(pad,pad)),'constant',constant_values=)
  out = np.zeros([N,F,H_new,W_new])
  for n in xrange(N):
    for f in xrange(F):
        for hh in xrange(H_new):
            for ww in xrange(W_new):
                out[n,f,hh,ww] = np.sum(x[n,:,hh*stride:hh*stride + HH,ww*stride:ww*stride+WW]*w[f])+b[f]
           

1.2. 後向傳播

x, w, b, conv_param = cache
  N, C, H, W = x.shape
  F, C, HH, WW = w.shape
  stride = conv_param['stride']
  pad = conv_param['pad']
  H_new, W_new = dout.shape[-:]
  dx, dw, db = None, None, None

  dx = np.zeros_like(x)
  dx = np.pad(dx,((,),(,),(pad,pad),(pad,pad)),'constant',constant_values=)
  dw = np.zeros_like(w)
  db = np.zeros_like(b)

  for n in xrange(N):
        for f in xrange(F):
            for hh in xrange(H_new):
                for ww in xrange(W_new):
                    db[f] += dout[n,f,hh,ww]
                    dw[f] += dout[n,f,hh,ww]*x[n,:,hh*stride:hh*stride + HH,ww*stride:ww*stride+WW]
                    dx[n,:,hh*stride:hh*stride + HH,ww*stride:ww*stride+WW] += dout[n,f,hh,ww]*w[f]
  dx = dx[:,:,pad:H-pad,pad:W-pad]
           

2. 池化函數

2.1. 前向傳播

N, C, H, W = x.shape
  pool_height = pool_param['pool_height']
  pool_width = pool_param['pool_width']
  stride = pool_param['stride']

  H_new =  + (H - pool_height) / stride
  W_new =  + (W - pool_width) / stride

  out = np.zeros([N,C,H_new,W_new])
  for n in xrange(N):
        for c in xrange(C):
            for h in xrange(H_new):
                for w in xrange(W_new):
                    out[n,c,h,w] = np.max(x[n,c,h*stride:(h*stride+pool_height), w*stride:(w*stride+pool_width)])
           

2.2. 後向傳播

N, C, H, W = x.shape
  pool_height = pool_param['pool_height']
  pool_width = pool_param['pool_width']
  stride = pool_param['stride']

  H_new =  + (H - pool_height) / stride
  W_new =  + (W - pool_width) / stride

  dx = np.zeros_like(x)
  for n in xrange(N):
    for c in xrange(C):
        for h in xrange(H_new):
            for w in xrange(W_new):
                max_index = np.argmax(x[n,c,h*stride:(h*stride+pool_height), w*stride:(w*stride+pool_width)])
                dx[n,c,h*stride+max_index/pool_width,w*stride+max_index%pool_width] += dout[n,c,h,w]
           

繼續閱讀