本文主要是介绍CS231n作业笔记2.6:卷积层以及池化层的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
CS231n简介
详见 CS231n课程笔记1:Introduction。
本文都是作者自己的思考,正确性未经过验证,欢迎指教。
作业笔记
就是简单实现一下卷积层以及池化层,唯一的难点在于确定索引值,稍微注意一下就好。
1. 卷积层
1.1. 前向传播
使用np.pad做填充。
N, C, H, W = x.shapeF, C, HH, WW = w.shapestride = conv_param['stride']pad = conv_param['pad']H_new = 1 + (H + 2 * pad - HH) / strideW_new = 1 + (W + 2 * pad - WW) / stridex = np.pad(x,((0,0),(0,0),(pad,pad),(pad,pad)),'constant',constant_values=0)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 = cacheN, C, H, W = x.shapeF, C, HH, WW = w.shapestride = conv_param['stride']pad = conv_param['pad']H_new, W_new = dout.shape[-2:]dx, dw, db = None, None, Nonedx = np.zeros_like(x)dx = np.pad(dx,((0,0),(0,0),(pad,pad),(pad,pad)),'constant',constant_values=0)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.shapepool_height = pool_param['pool_height']pool_width = pool_param['pool_width']stride = pool_param['stride']H_new = 1 + (H - pool_height) / strideW_new = 1 + (W - pool_width) / strideout = 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.shapepool_height = pool_param['pool_height']pool_width = pool_param['pool_width']stride = pool_param['stride']H_new = 1 + (H - pool_height) / strideW_new = 1 + (W - pool_width) / stridedx = 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]
这篇关于CS231n作业笔记2.6:卷积层以及池化层的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!