model_wrappers.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:importance-sampling 作者: idiap 项目源码 文件源码
def _augment_model(self, model, score, reweighting):
        # Extract some info from the model
        loss = model.loss
        optimizer = model.optimizer.__class__(**model.optimizer.get_config())
        output_shape = K.int_shape(model.output)[1:]
        if isinstance(loss, str) and loss.startswith("sparse"):
            output_shape = output_shape[:-1] + (1,)

        # Make sure that some stuff look ok
        assert not isinstance(loss, list)

        # We need to create two more inputs
        #   1. the targets
        #   2. the predicted scores
        y_true = Input(shape=output_shape)
        pred_score = Input(shape=(reweighting.weight_size,))

        # Create a loss layer and a score layer
        loss_tensor = LossLayer(loss)([y_true, model.output])
        last_layer = -2 if isinstance(model.layers[-1], Activation) else -1
        score_tensor = _get_scoring_layer(
            score,
            y_true,
            model.output,
            loss,
            model.layers[last_layer]
        )

        # Create the sample weights
        weights = reweighting.weight_layer()([score_tensor, pred_score])

        # Create the output
        weighted_loss = multiply([loss_tensor, weights])

        # Create the metric layers
        metrics = model.metrics or []
        metrics = [
            MetricLayer(metric)([y_true, model.output])
            for metric in metrics
        ]

        # Finally build, compile, return
        new_model = TransparentModel(
            inputs=_tolist(model.input) + [y_true, pred_score],
            outputs=[weighted_loss],
            observed_tensors=[loss_tensor, weighted_loss, score_tensor] + metrics
        )
        new_model.compile(
            optimizer=optimizer,
            loss=lambda y_true, y_pred: y_pred
        )

        return new_model
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号