Skip to content

Instantly share code, notes, and snippets.

@salihkaragoz
Last active April 29, 2018 15:06
Show Gist options
  • Save salihkaragoz/0cd7b4fb49b35ba4822976b6e6ceefab to your computer and use it in GitHub Desktop.
Save salihkaragoz/0cd7b4fb49b35ba4822976b6e6ceefab to your computer and use it in GitHub Desktop.
Cifar 10 arc
def five_convnet(X, model, y=None, reg=0.0):
W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2']
W3, b3, W4, b4 = model['W3'], model['b3'], model['W4'], model['b4']
W5, b5, W6, b6 = model['W5'], model['b5'], model['W6'], model['b6']
conv_filter_height, conv_filter_width = W1.shape[2:]
assert conv_filter_height == conv_filter_width, 'Conv filter must be square'
assert conv_filter_height % 2 == 1, 'Conv filter height must be odd'
assert conv_filter_width % 2 == 1, 'Conv filter width must be odd'
conv_param = {'stride': 1, 'pad': (conv_filter_height - 1) / 2}
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}
a1, cache1 = conv_relu_forward(X, W1, b1, conv_param)
a2, cache2 = conv_relu_pool_forward(a1, W2, b2, conv_param, pool_param)
a3, cache3 = conv_relu_pool_forward(a2, W3, b3, conv_param, pool_param)
a4, cache4 = conv_relu_pool_forward(a3, W4, b4, conv_param, pool_param)
a5, cache5 = affine_forward(a4, W5, b5)
scores, cache6 = affine_forward(a5, W6, b6)
if y is None:
return scores
data_loss, dscores = softmax_loss(scores, y)
da5, dW6, db6 = affine_backward(dscores, cache6)
da4, dW5, db5 = affine_backward(da5, cache5)
da3, dW4, db4 = conv_relu_pool_backward(da4, cache4)
da2, dW3, db3 = conv_relu_pool_backward(da3, cache3)
da1, dW2, db2 = conv_relu_pool_backward(da2, cache2)
dX, dW1, db1 = conv_relu_backward(da1, cache1)
dW1 += reg * W1
dW2 += reg * W2
dW3 += reg * W3
dW4 += reg * W4
dW5 += reg * W5
dW6 += reg * W6
reg_loss = 0.5 * reg * sum(np.sum(W * W) for W in [W1, W2, W3, W4, W5, W6])
loss = data_loss + reg_loss
grads = {'W1': dW1, 'b1': db1, 'W2': dW2, 'b2': db2,
'W3': dW3, 'b3': db3, 'W4': dW4, 'b4': db4,
'W5': dW5, 'b5': db5, 'W6': dW6, 'b6': db6}
return loss, grads
def init_five_convnet(weight_scale=1e-3, bias_scale=0):
model = {}
model['W1'] = weight_scale * np.random.randn(32, 3, 3, 3)
model['b1'] = bias_scale * np.random.randn(32)
model['W2'] = weight_scale * np.random.randn(64, 32, 3, 3)
model['b2'] = bias_scale * np.random.randn(64)
model['W3'] = weight_scale * np.random.randn(128, 64, 3, 3)
model['b3'] = bias_scale * np.random.randn(128)
model['W4'] = weight_scale * np.random.randn(128, 128, 3, 3)
model['b4'] = bias_scale * np.random.randn(128)
model['W5'] = weight_scale * np.random.randn(2048, 1024)
model['b5'] = bias_scale * np.random.randn(1024)
model['W6'] = weight_scale * np.random.randn(1024, 10)
model['b6'] = bias_scale * np.random.randn(10)
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment