print('Testing conv') print('dx error: ', rel_error(dx_num, dx)) print('dw error: ', rel_error(dw_num, dw)) print('db error: ', rel_error(db_num, db)) # TODO: write script to check the backpropagation on the whole CNN is correct if check_cnn_back: model = CNN(image_size=(3, 8, 8), channels=3, conv_kernel=5, pool_kernel=2, hidden_units=10, n_classes=10) X = np.random.randn(2, 3, 8, 8) y = (np.random.rand(2) * 10).astype(np.int32) model.oracle(X, y) loss_fn = SoftmaxCE() for group in model.param_groups: for name, param in group.items(): xx = param["param"] def fx(v): tmp = param['param'] param['param'] = v f_x, _ = loss_fn(model.score(X), y) param['param'] = tmp return f_x d_num = eval_numerical_gradient(fx, xx, verbose=False)
if check_cnn_back: inputs = 2 input_dim = (3, 16, 16) hidden_units = 10 num_classes = 10 num_filters = 3 filter_size = 3 pool_size = 2 np.random.seed(231) X = np.random.randn(inputs, *input_dim) y = np.random.randint(num_classes, size=inputs) model = CNN(input_dim, num_filters, filter_size, pool_size, hidden_units, num_classes) loss, score = model.oracle(X, y) print('loss:', loss) a = ['w1', 'w2', 'w3'] b = ['b1', 'b2', 'b3'] for param_name in sorted(a): f = lambda _: model.oracle(X, y)[0] param_grad_num = eval_numerical_gradient( f, model.param_groups['w'][param_name]['param'], verbose=False, h=0.00001) e = rel_error(param_grad_num, model.param_groups['w'][param_name]['grad']) print('%s relative error: %e' % (param_name,