예제 #1
0
def get_opt_problem(nn_type):
    """ Gets parameters for the optimisation problem. """
    if nn_type == 'cnn':
        constraint_checker = CNNConstraintChecker(50, 1e8, 5, 5, 200, 1024, 8)
        init_points = get_initial_cnn_pool()
        func_caller = FunctionCaller(cnn_syn_func1, NNDomain(None, None))
    elif nn_type.startswith('mlp'):
        constraint_checker = MLPConstraintChecker(50, 1e8, 5, 5, 200, 1024, 8)
        init_points = get_initial_mlp_pool(CLASS_OR_REG)
        func_caller = FunctionCaller(mlp_syn_func1, NNDomain(None, None))
    else:
        raise ValueError('Unknown nn_type: %s.' % (nn_type))
    # Common stuff
    mutation_op = get_nn_modifier_from_args(constraint_checker,
                                            [0.5, 0.25, 0.125, 0.075, 0.05])
    init_vals = [func_caller.eval_single(nn)[0] for nn in init_points]
    return constraint_checker, func_caller, mutation_op, init_points, init_vals
예제 #2
0
def get_nn_opt_arguments():
  """ Returns arguments for NN Optimisation. """
  ret = Namespace()
  ret.cnn_constraint_checker = CNNConstraintChecker(50, 5, 1e8, 0, 5, 5, 200, 1024, 8)
  ret.mlp_constraint_checker = MLPConstraintChecker(50, 5, 1e8, 0, 5, 5, 200, 1024, 8)
  ret.cnn_mutation_op = get_nn_modifier_from_args(ret.cnn_constraint_checker,
                                                   [0.5, 0.25, 0.125, 0.075, 0.05])
  ret.mlp_mutation_op = get_nn_modifier_from_args(ret.mlp_constraint_checker,
                                                   [0.5, 0.25, 0.125, 0.075, 0.05])
  # Create the initial pool
  ret.cnn_init_pool = get_initial_cnn_pool()
  ret.cnn_init_vals = [cnn_syn_func1(cnn) for cnn in ret.cnn_init_pool]
  ret.mlp_init_pool = get_initial_mlp_pool('reg')
  ret.mlp_init_vals = [mlp_syn_func1(mlp) for mlp in ret.mlp_init_pool]
  # Create a domain
  ret.nn_domain = NNDomain(None, None)
  ret.cnn_domain = NNDomain('cnn', ret.cnn_constraint_checker)
  ret.mlp_domain = NNDomain('mlp-reg', ret.mlp_constraint_checker)
  # Return
  return ret
예제 #3
0
def get_nn_domain_from_constraints(nn_type, *args, **kwargs):
  """ nn_type is the type of the network.
      See CNNConstraintChecker, MLPConstraintChecker, NNConstraintChecker constructors
      for args and kwargs.
  """
  if nn_type[:3] == 'cnn':
    constraint_checker = CNNConstraintChecker(*args, **kwargs)
  elif nn_type[:3] == 'mlp':
    constraint_checker = MLPConstraintChecker(*args, **kwargs)
  else:
    raise ValueError('Unknown nn_type.')
  return NNDomain(nn_type, constraint_checker)
예제 #4
0
 def test_nnfunction_caller(self):
     """ Unit tests for the Neural Network Function caller. """
     self.report('Testing NNFunctionCaller class.')
     test_cases = [(self.cnns, syn_nn_functions.cnn_syn_func1),
                   (self.mlps, syn_nn_functions.mlp_syn_func1)]
     for nns, func in test_cases:
         func_caller = nn_opt_utils.NNFunctionCaller(
             func, NNDomain(None, None))
         true_vals = np.array([func(nn) for nn in nns])
         single_result = np.array(
             [func_caller.eval_single(nn)[0] for nn in nns])
         mult_result = np.array(func_caller.eval_multiple(nns)[0])
         assert np.linalg.norm(
             true_vals - single_result) < _TOL * np.linalg.norm(true_vals)
         assert np.linalg.norm(
             true_vals - mult_result) < _TOL * np.linalg.norm(true_vals)