def _check_get_obj_equals_get_obj_jac(make_model_func): attrs = random.randint(1, 10) outs = random.randint(1, 10) inp_matrix, tar_matrix = datasets.get_random_regression(10, attrs, outs) model = make_model_func(attrs, outs) flat_weights = model._weight_matrix.ravel() assert model._get_obj(flat_weights, inp_matrix, tar_matrix) == model._get_obj_jac( flat_weights, inp_matrix, tar_matrix)[0]
def _check_jacobian(make_model_func): attrs = random.randint(1, 10) outs = random.randint(1, 10) model = make_model_func(attrs, random.randint(1, 10), outs) inp_matrix, tar_matrix = datasets.get_random_regression(10, attrs, outs) # Test jacobian of error function f = lambda xk: mlp._mlp_obj(model, inp_matrix, tar_matrix, xk) df = lambda xk: mlp._mlp_obj_jac(model, inp_matrix, tar_matrix, xk)[1] helpers.check_gradient( f, df, inputs=mlp._flatten(model._weight_matrices), f_shape='scalar')
def test_rbf_obj_and_obj_jac_match(): """obj and obj_jac functions should return the same obj value.""" attrs = random.randint(1, 10) outs = random.randint(1, 10) model = rbf.RBF(attrs, random.randint(1, 10), outs) dataset = datasets.get_random_regression(10, attrs, outs) # Don't use exactly the same parameters, to ensure obj functions are actually # using the given parameters parameters = random.uniform(-1.0, 1.0) * model._weight_matrix.ravel() assert helpers.approx_equal( model._get_obj(parameters, dataset[0], dataset[1]), model._get_obj_jac(parameters, dataset[0], dataset[1])[0])
def _check_jacobian(make_model_func): attrs = random.randint(1, 10) outs = random.randint(1, 10) model = make_model_func(attrs, outs) inp_matrix, tar_matrix = datasets.get_random_regression(10, attrs, outs) # Test jacobian of error function f = lambda xk: model._get_obj(xk, inp_matrix, tar_matrix) df = lambda xk: model._get_obj_jac(xk, inp_matrix, tar_matrix)[1] helpers.check_gradient(f, df, inputs=model._weight_matrix.ravel(), f_shape='scalar')
def _check_jacobian(make_model_func): attrs = random.randint(1, 10) outs = random.randint(1, 10) model = make_model_func(attrs, random.randint(1, 10), outs) inp_matrix, tar_matrix = datasets.get_random_regression(random.randint(1, 10), attrs, outs) # Test jacobian of error function f = lambda xk: model._get_obj(xk, inp_matrix, tar_matrix) df = lambda xk: model._get_obj_jac(xk, inp_matrix, tar_matrix)[1] helpers.check_gradient( f, df, f_arg_tensor=mlp._flatten(model._bias_vec, model._weight_matrices), f_shape='scalar')
def _check_obj_and_obj_jac_match(make_model_func, classification=False): """obj and obj_jac functions should return the same obj value.""" attrs = random.randint(1, 10) outs = random.randint(1, 10) model = make_model_func(attrs, random.randint(1, 10), outs) if classification: dataset = datasets.get_random_classification(10, attrs, outs) else: dataset = datasets.get_random_regression(10, attrs, outs) # Don't use exactly the same parameters, to ensure obj functions are actually # using the given parameters parameters = random.uniform(-1.0, 1.0) * mlp._flatten(model._weight_matrices) assert helpers.approx_equal( mlp._mlp_obj(model, dataset[0], dataset[1], parameters), mlp._mlp_obj_jac(model, dataset[0], dataset[1], parameters)[0])