def estimate_parameters(ann, sample, target, epsilon=0.0001): original_parameters = extract_parameters(ann) ann = ml.ForwardFeedNetwork(ann.arch, ann.activation) updated_parameters = [] for index in range(len(original_parameters)): dp = np.zeros(len(original_parameters)) dp[index] += epsilon minus_p = original_parameters - dp ann = update_ann(ann, minus_p) minus_errors = ml.quadratic_error(ann.forward_feed(sample), target) minus_cost = np.mean(minus_errors) plus_p = original_parameters + dp ann = update_ann(ann, plus_p) plus_errors = ml.quadratic_error(ann.forward_feed(sample), target) plus_cost = np.mean(plus_errors) derivative = (plus_cost - minus_cost) / (2 * epsilon) updated_parameters.append(derivative) return np.asarray(updated_parameters)
def test_quadratic_error_matrix(self): output = np.asarray([ [1.0, 0.5, 0.25], [0.5, 1.0, 0.25], ]) target = np.asarray([ [0.1, 1.0, 0.25], [1.0, 0.1, 0.25], ]) expected_errors = [[0.405, 0.125, 0.0], [0.125, 0.405, 0.0]] assert np.all(ml.quadratic_error(output, target) == expected_errors)
def test_quadratic_error_vector(self): output = np.asarray([0.5, 1.0, 0.25]) target = np.asarray([1.0, 0.1, 0.25]) expected_errors = [0.125, 0.405, 0.0] assert np.all(ml.quadratic_error(output, target) == expected_errors)
def test_quadratic_error_scalar(self): assert ml.quadratic_error(1, 0.5) == 0.125