Deep learning 关于cs231n赋值中的快速Conv层蕴涵
在著名的Cs231n赋值2中,在快速卷积层的含义中,他们使用跨步技巧获得im2col运算Deep learning 关于cs231n赋值中的快速Conv层蕴涵,deep-learning,Deep Learning,在著名的Cs231n赋值2中,在快速卷积层的含义中,他们使用跨步技巧获得im2col运算 def conv_forward_strides(x, w, b, conv_param): N, C, H, W = x.shape F, _, HH, WW = w.shape stride, pad = conv_param['stride'], conv_param['pad'] # Check dimensions #assert (W + 2 * pad
def conv_forward_strides(x, w, b, conv_param):
N, C, H, W = x.shape
F, _, HH, WW = w.shape
stride, pad = conv_param['stride'], conv_param['pad']
# Check dimensions
#assert (W + 2 * pad - WW) % stride == 0, 'width does not work'
#assert (H + 2 * pad - HH) % stride == 0, 'height does not work'
# Pad the input
p = pad
x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
# Figure out output dimensions
H += 2 * pad
W += 2 * pad
out_h = (H - HH) // stride + 1
out_w = (W - WW) // stride + 1
# Perform an im2col operation by picking clever strides
shape = (C, HH, WW, N, out_h, out_w)
strides = (H * W, W, 1, C * H * W, stride * W, stride)
strides = x.itemsize * np.array(strides)
x_stride = np.lib.stride_tricks.as_strided(x_padded,
shape=shape, strides=strides)
x_cols = np.ascontiguousarray(x_stride)
x_cols.shape = (C * HH * WW, N * out_h * out_w)
# Now all our convolutions are a big matrix multiply
res = w.reshape(F, -1).dot(x_cols) + b.reshape(-1, 1)
# Reshape the output
res.shape = (F, N, out_h, out_w)
out = res.transpose(1, 0, 2, 3)
# Be nice and return a contiguous array
# The old version of conv_forward_fast doesn't do this, so for a fair
# comparison we won't either
out = np.ascontiguousarray(out)
cache = (x, w, b, conv_param, x_cols)
return out, cache
现在,as_Striped()的形状和步幅是如何计算的?我很难想象它。
这个实现是否也有额外的内存问题?它似乎不像其他im2col实现,因为我想没有新的内存被创建。
谢谢。 这里首先要考虑的是:数据格式是NCHW.< 因此,如果您将所有输入数据放在一个一致的内存块中,并按1移动,那么您将在W(=列)中移动,因为W的优先级最低 现在移动一行需要多少偏移量?H(=行)是下一个优先级。一行由W列组成。因此,如果要跳到下一行,需要偏移量为W 现在,您要在C(=通道)中移动。要转到下一个频道,必须跳过最后一个频道的所有行(=H),每行有W列。所以一个通道有H*W元素 现在,如果要移动一个小批次(=N),则必须移动所有通道(=C)。一个通道包含H*W元素,因此一个小批次包含所有通道的C*H*W元素 你就这样大步前进