예제 #1
0
 def _setup_loss(self):
     if self.loss[TYPE] == 'mean_squared_error':
         self.train_loss_function = MSELoss()
         self.eval_loss_function = MSEMetric(name='eval_loss')
     elif self.loss[TYPE] == 'mean_absolute_error':
         self.train_loss_function = MAELoss()
         self.eval_loss_function = MAEMetric(name='eval_loss')
     else:
         raise ValueError('Unsupported loss type {}'.format(
             self.loss[TYPE]))
예제 #2
0
    def _setup_loss(self):
        if self.loss[TYPE] == "mean_squared_error":
            self.train_loss_function = MSELoss()
        elif self.loss[TYPE] == "mean_absolute_error":
            self.train_loss_function = MAELoss()
        elif self.loss[TYPE] == "root_mean_squared_error":
            self.train_loss_function = RMSELoss()
        elif self.loss[TYPE] == "root_mean_squared_percentage_error":
            self.train_loss_function = RMSPELoss()
        else:
            raise ValueError(
                "Unsupported loss type {}".format(self.loss[TYPE])
            )

        self.eval_loss_function = self.train_loss_function
예제 #3
0
def test_model_weights_match_training(tmpdir, csv_filename):
    np.random.seed(1)

    input_features = [number_feature()]
    output_features = [number_feature()]
    output_feature_name = output_features[0][NAME]

    # Generate test data
    data_csv_path = generate_data(input_features,
                                  output_features,
                                  os.path.join(tmpdir, csv_filename),
                                  num_examples=100)

    config = {
        "input_features": input_features,
        "output_features": output_features,
        "trainer": {
            "epochs": 5,
            "batch_size": 32
        },
    }

    model = LudwigModel(config=config, )

    training_stats, _, _ = model.train(training_set=data_csv_path,
                                       random_seed=1919)

    # generate predicitons from training data
    df = pd.read_csv(data_csv_path)
    predictions = model.predict(df)

    # compute loss on predictions from training data
    loss_function = MSELoss()
    loss = loss_function(
        torch.tensor(predictions[0][output_feature_name +
                                    "_predictions"].values),  # predictions
        torch.tensor(df[output_feature_name].values),  # target
    ).type(torch.float32)

    # get last loss value from training
    last_training_loss = torch.tensor(
        training_stats[TRAINING][output_feature_name][LOSS][-1])

    # loss from predictions should match last loss value recorded during training
    assert torch.isclose(loss, last_training_loss), (
        "Model predictions on training set did not generate same loss value as in training. "
        "Need to confirm that weights were correctly captured in model.")
예제 #4
0
 def _setup_loss(self):
     if self.loss[TYPE] == 'mean_squared_error':
         self.train_loss_function = MSELoss()
         self.eval_loss_function = MSEMetric(name='eval_loss')
     elif self.loss[TYPE] == 'mean_absolute_error':
         self.train_loss_function = MAELoss()
         self.eval_loss_function = MAEMetric(name='eval_loss')
     elif self.loss[TYPE] == SOFTMAX_CROSS_ENTROPY:
         self.train_loss_function = SoftmaxCrossEntropyLoss(
             num_classes=self.vector_size,
             feature_loss=self.loss,
             name='train_loss')
         self.eval_loss_function = SoftmaxCrossEntropyMetric(
             num_classes=self.vector_size,
             feature_loss=self.loss,
             name='eval_loss')
     else:
         raise ValueError('Unsupported loss type {}'.format(
             self.loss[TYPE]))