Ejemplo n.º 1
0
    def __init__(self, num_params, layer_dims, layer_activations,
                 train_threshold_ratio, batch_size, keep_prob,
                 regularisation_coefficient, losses_list):
        self.log = logging.getLogger(__name__)
        start = time.time()

        self.save_archive_filename = (
            mlu.archive_foldername + "neural_net_archive_" +
            mlu.datetime_to_string(datetime.datetime.now()) + "_"
            # We include 6 random bytes for deduplication in case multiple nets
            # are created at the same time.
            + base64.urlsafe_b64encode(nr.bytes(6)).decode() + ".ckpt")

        self.log.info("Constructing net")
        self.graph = tf.Graph()
        self.tf_session = tf.Session(graph=self.graph)

        if not len(layer_dims) == len(layer_activations):
            self.log.error('len(layer_dims) != len(layer_activations)')
            raise ValueError

        # Hyperparameters for the net. These are all constant.
        self.num_params = num_params
        self.train_threshold_ratio = train_threshold_ratio
        self.batch_size = batch_size
        self.keep_prob = keep_prob
        self.regularisation_coefficient = regularisation_coefficient

        self.losses_list = losses_list

        with self.graph.as_default():
            ## Inputs
            self.input_placeholder = tf.placeholder(
                tf.float32, shape=[None, self.num_params])
            self.output_placeholder = tf.placeholder(tf.float32,
                                                     shape=[None, 1])
            self.keep_prob_placeholder = tf.placeholder_with_default(1.,
                                                                     shape=[])
            self.regularisation_coefficient_placeholder = tf.placeholder_with_default(
                0., shape=[])

            ## Initialise the network

            weights = []
            biases = []

            # Input + internal nodes
            prev_layer_dim = self.num_params
            bias_stddev = 0.5
            for (i, dim) in enumerate(layer_dims):
                weights.append(
                    tf.Variable(tf.random_normal([prev_layer_dim, dim],
                                                 stddev=1.4 /
                                                 np.sqrt(prev_layer_dim)),
                                name="weight_" + str(i)))
                biases.append(
                    tf.Variable(tf.random_normal([dim], stddev=bias_stddev),
                                name="bias_" + str(i)))
                prev_layer_dim = dim

            # Output node
            weights.append(
                tf.Variable(tf.random_normal([prev_layer_dim, 1],
                                             stddev=1.4 /
                                             np.sqrt(prev_layer_dim)),
                            name="weight_out"))
            biases.append(
                tf.Variable(tf.random_normal([1], stddev=bias_stddev),
                            name="bias_out"))

            # Get the output var given an input var
            def get_output_var(input_var):
                prev_h = input_var
                for w, b, act in zip(weights[:-1], biases[:-1],
                                     layer_activations):
                    prev_h = tf.nn.dropout(
                        act(tf.matmul(prev_h, w) + b),
                        keep_prob=self.keep_prob_placeholder)
                return tf.matmul(prev_h, weights[-1]) + biases[-1]

            ## Define tensors for evaluating the output var and gradient on the full input
            self.output_var = get_output_var(self.input_placeholder)
            self.output_var_gradient = tf.gradients(self.output_var,
                                                    self.input_placeholder)

            ## Declare common loss functions

            # Get the raw loss given the expected and actual output vars
            def get_loss_raw(expected, actual):
                return tf.reduce_mean(
                    tf.reduce_sum(tf.square(expected - actual),
                                  reduction_indices=[1]))

            # Regularisation component of the loss.
            loss_reg = (self.regularisation_coefficient_placeholder *
                        tf.reduce_mean([tf.nn.l2_loss(W) for W in weights]))

            ## Define tensors for evaluating the loss on the full input
            self.loss_raw = get_loss_raw(self.output_placeholder,
                                         self.output_var)
            self.loss_total = self.loss_raw + loss_reg

            ## Training
            self.train_step = tf.train.AdamOptimizer().minimize(
                self.loss_total)

            # Initialiser for ... initialising
            self.initialiser = tf.global_variables_initializer()

            # Saver for saving and restoring params
            self.saver = tf.train.Saver(write_version=tf.train.SaverDef.V2)
        self.log.debug("Finished constructing net in: " +
                       str(time.time() - start))
Ejemplo n.º 2
0
 def __init__(self, interface,
              max_num_runs = float('+inf'),
              target_cost = float('-inf'),
              max_num_runs_without_better_params = float('+inf'),
              controller_archive_filename=default_controller_archive_filename,
              controller_archive_file_type=default_controller_archive_file_type,
              archive_extra_dict = None,
              start_datetime = None,
              **kwargs):
     
     #Make logger
     self.remaining_kwargs = mlu._config_logger(**kwargs)
     self.log = logging.getLogger(__name__)
     
     #Variable that are included in archive
     self.num_in_costs = 0
     self.num_out_params = 0
     self.num_last_best_cost = 0
     self.out_params = []
     self.out_type = []
     self.out_extras = []
     self.in_costs = []
     self.in_uncers = []
     self.in_bads = []
     self.in_extras = []
     self.best_cost = float('inf')
     self.best_uncer = float('nan')
     self.best_index = float('nan')
     self.best_params = float('nan')
     
     #Variables that used internally
     self.last_out_params = None
     self.curr_params = None
     self.curr_cost = None
     self.curr_uncer = None
     self.curr_bad = None
     self.curr_extras = None
     
     #Constants
     self.controller_wait = float(1)
     
     #Learner related variables
     self.learner_params_queue = None
     self.learner_costs_queue = None
     self.end_learner = None
     self.learner = None
     
     #Variables set by user
     
     #save interface and extract important variables
     if isinstance(interface, mli.Interface):
         self.interface = interface
     else:
         self.log.error('interface is not a Interface as defined in the MLOOP package.')
         raise TypeError
     
     self.params_out_queue = interface.params_out_queue
     self.costs_in_queue = interface.costs_in_queue
     self.end_interface = interface.end_event
     
     #Other options
     if start_datetime is None:
         self.start_datetime = datetime.datetime.now()
     else:
         self.start_datetime = datetime.datetime(start_datetime)
     self.max_num_runs = float(max_num_runs)
     if self.max_num_runs<=0:
         self.log.error('Number of runs must be greater than zero. max_num_runs:'+repr(self.max_num_run))
         raise ValueError
     self.target_cost = float(target_cost)
     self.max_num_runs_without_better_params = float(max_num_runs_without_better_params)
     if self.max_num_runs_without_better_params<=0:
         self.log.error('Max number of repeats must be greater than zero. max_num_runs:'+repr(max_num_runs_without_better_params))
         raise ValueError
     
     if mlu.check_file_type_supported(controller_archive_file_type):
         self.controller_archive_file_type = controller_archive_file_type
     else:
         self.log.error('File in type is not supported:' + repr(controller_archive_file_type))
         raise ValueError
     if controller_archive_filename is None:
         self.controller_archive_filename = None
     else:
         if not os.path.exists(mlu.archive_foldername):
             os.makedirs(mlu.archive_foldername)
         self.controller_archive_filename =str(controller_archive_filename)
         self.total_archive_filename = mlu.archive_foldername + self.controller_archive_filename + '_' + mlu.datetime_to_string(self.start_datetime) + '.' + self.controller_archive_file_type
     
     self.archive_dict = {'archive_type':'controller',
                          'num_out_params':self.num_out_params,
                          'out_params':self.out_params,
                          'out_type':self.out_type,
                          'out_extras':self.out_extras,
                          'in_costs':self.in_costs,
                          'in_uncers':self.in_uncers,
                          'in_bads':self.in_bads,
                          'in_extras':self.in_extras,
                          'max_num_runs':self.max_num_runs,
                          'start_datetime':mlu.datetime_to_string(self.start_datetime)}
     
     if archive_extra_dict is not None:
         self.archive_dict.update(archive_extra_dict)
     
     self.log.debug('Controller init completed.')
Ejemplo n.º 3
0
    def __init__(
            self,
            interface,
            max_num_runs=float('+inf'),
            target_cost=float('-inf'),
            max_num_runs_without_better_params=float('+inf'),
            controller_archive_filename=default_controller_archive_filename,
            controller_archive_file_type=default_controller_archive_file_type,
            archive_extra_dict=None,
            start_datetime=None,
            **kwargs):

        #Make logger
        self.remaining_kwargs = mlu._config_logger(**kwargs)
        self.log = logging.getLogger(__name__)

        #Variable that are included in archive
        self.num_in_costs = 0
        self.num_out_params = 0
        self.num_last_best_cost = 0
        self.out_params = []
        self.out_type = []
        self.out_extras = []
        self.in_costs = []
        self.in_uncers = []
        self.in_bads = []
        self.in_extras = []
        self.best_cost = float('inf')
        self.best_uncer = float('nan')
        self.best_index = float('nan')
        self.best_params = float('nan')

        #Variables that used internally
        self.last_out_params = None
        self.curr_params = None
        self.curr_cost = None
        self.curr_uncer = None
        self.curr_bad = None
        self.curr_extras = None

        #Constants
        self.controller_wait = float(1)

        #Learner related variables
        self.learner_params_queue = None
        self.learner_costs_queue = None
        self.end_learner = None
        self.learner = None

        #Variables set by user

        #save interface and extract important variables
        if isinstance(interface, mli.Interface):
            self.interface = interface
        else:
            self.log.error(
                'interface is not a Interface as defined in the M-LOOP package.'
            )
            raise TypeError

        self.params_out_queue = interface.params_out_queue
        self.costs_in_queue = interface.costs_in_queue
        self.end_interface = interface.end_event

        #Other options
        if start_datetime is None:
            self.start_datetime = datetime.datetime.now()
        else:
            self.start_datetime = datetime.datetime(start_datetime)
        self.max_num_runs = float(max_num_runs)
        if self.max_num_runs <= 0:
            self.log.error(
                'Number of runs must be greater than zero. max_num_runs:' +
                repr(self.max_num_run))
            raise ValueError
        self.target_cost = float(target_cost)
        self.max_num_runs_without_better_params = float(
            max_num_runs_without_better_params)
        if self.max_num_runs_without_better_params <= 0:
            self.log.error(
                'Max number of repeats must be greater than zero. max_num_runs:'
                + repr(max_num_runs_without_better_params))
            raise ValueError

        if mlu.check_file_type_supported(controller_archive_file_type):
            self.controller_archive_file_type = controller_archive_file_type
        else:
            self.log.error('File in type is not supported:' +
                           repr(controller_archive_file_type))
            raise ValueError
        if controller_archive_filename is None:
            self.controller_archive_filename = None
        else:
            # Store self.controller_archive_filename without any path, but
            # include any path components in controller_archive_filename when
            # constructing the full path.
            controller_archive_filename = str(controller_archive_filename)
            self.controller_archive_filename = os.path.basename(
                controller_archive_filename)
            filename_suffix = mlu.generate_filename_suffix(
                self.controller_archive_file_type,
                file_datetime=self.start_datetime,
            )
            filename = controller_archive_filename + filename_suffix
            self.total_archive_filename = os.path.join(mlu.archive_foldername,
                                                       filename)

            # Include any path info from controller_archive_filename when
            # creating directory for archive files.]
            archive_dir = os.path.dirname(self.total_archive_filename)
            if not os.path.exists(archive_dir):
                os.makedirs(archive_dir)

        self.archive_dict = {
            'mloop_version': __version__,
            'archive_type': 'controller',
            'num_out_params': self.num_out_params,
            'out_params': self.out_params,
            'out_type': self.out_type,
            'out_extras': self.out_extras,
            'in_costs': self.in_costs,
            'in_uncers': self.in_uncers,
            'in_bads': self.in_bads,
            'in_extras': self.in_extras,
            'max_num_runs': self.max_num_runs,
            'start_datetime': mlu.datetime_to_string(self.start_datetime)
        }

        if archive_extra_dict is not None:
            self.archive_dict.update(archive_extra_dict)

        self.log.debug('Controller init completed.')