예제 #1
0
    def get_best_model(self):
        """Returns the best model."""

        # check if we have run fit first.
        if not self.autoqk:
            return None

        self.autoqk.hypermodel.target.print_stats()
        print_qmodel_summary(self.best_model)

        return self.best_model
예제 #2
0
    def build(self, hp):
        """Builds hyperparameterized quantized model."""

        self.groups = {}

        # we are not using the fanin right now.

        q_model, _ = self.quantize_model(hp)

        # transfer weights from previous run as we know we will not
        if self.learning_rate_optimizer:
            # if learning_rate_optimizer, we try to transfer weights from previous run
            print("... freezing layers {}.".format(", ".join(
                self.frozen_layers)))
            for layer_name in self.frozen_layers:
                o_weights = self.model.get_layer(layer_name).get_weights()
                layer = q_model.get_layer(layer_name)
                # don't know if setting trainable to False is good or not yet
                # try to do "soft-freeze" by transferring weights. More experiments
                # needed before we decide what to do.
                # layer.trainable = False
                weights = layer.get_weights()
                # because we can be changing number of layers, we do not know
                # if we can really use some of the weights or not.
                equal_layer = True
                for w in range(len(o_weights)):
                    if o_weights[w].shape != weights[w].shape:
                        equal_layer = False
                        break
                if equal_layer:
                    layer.set_weights(o_weights)

        self.trial_size = self.target.get_trial(q_model)

        # we will use a boosted accuracy computation

        delta = self.target.delta()

        # by default, we use the first metric specified by the
        # user to be the target metric.

        self.score = AutoQKHyperModel.adjusted_score(
            self, delta, self.metrics[0] if self.metrics else None)

        # some papers suggest that we use learning_rate * sqrt(fanin) / layer
        # we cannot do that right now, but we can definitely do that
        # if we are quantizing one layer at a time
        #
        # https://arxiv.org/pdf/1511.00363.pdf

        # we use the magic number to smooth out the average
        total_factor = self.target.get_total_factor()
        delta_lr = 1.0 + (total_factor < 0) * total_factor

        # we assume model has been compiled at least.

        lr = float(self.model.optimizer.lr.numpy())

        # we assume that delta_lr can lower lr to accommodate
        # for more quantization
        #
        # if learning rate scheduler is used, we assume the callback to manage
        # learning rate. Just set it to constant.

        if self.learning_rate_optimizer:
            lr_range = list(lr * np.linspace(delta_lr, 1.1, 5))
            lr_choice = hp.Choice("learning_rate", lr_range)
        else:
            lr_choice = lr
            print("learning_rate: {}".format(lr))

        optimizer = Adam(lr=lr_choice)

        q_model.summary()

        q_model.compile(optimizer=optimizer,
                        loss=self.model.loss,
                        metrics=self.metrics +
                        [self.trial_size_metric(self.trial_size), self.score])
        self.q_model = q_model

        # this just prints a summary of the quantization for debugging
        # purposes

        self.target.print_stats()
        print_qmodel_summary(q_model)

        return q_model