Beispiel #1
0
def test_unique_together(db: Session):
    # GIVEN
    db.add(SimpleModel(col_1="foo", col_2="bar"))
    db.add(SimpleModel(col_1="foo", col_2="bar"))

    # THEN 1
    with pytest.raises(IntegrityError) as exc:
        # WHEN
        db.commit()

    # THEN 2
    assert "Duplicate entry" in str(exc)
Beispiel #2
0
    def test_valid_value(self, db: Session):
        # WHEN
        instance = SimpleModel(type=SimpleModel.Type.SUPER)
        db.add(instance)
        db.commit()

        # THEN
        assert instance.type == SimpleModel.Type.SUPER
Beispiel #3
0
    def test_invalid_value(self, db: Session):
        # THEN 1
        with pytest.raises(StatementError) as exc:
            # WHEN
            db.add(SimpleModel(type="INVALID_TYPE"))
            db.commit()

        # THEN 2
        assert "is not a valid" in str(exc)
Beispiel #4
0
    def visualize_entropy_linerp(self):
        # We are visualizing only between w_1 and w_2 here
        dummy_model = SimpleModel().to(self.config.firelab.device_name)

        # acts_ent_linerp_train = compute_activations_entropy_linerp(self.model.w_1, self.model.w_2, dummy_model.nn, self.train_dataloader)
        acts_ent_linerp_test = compute_activations_entropy_linerp(self.model.w_1, self.model.w_2, dummy_model.nn, self.val_dataloader)
        weights_ent_linerp = compute_weights_entropy_linerp(self.model.w_1, self.model.w_2)

        # self.writer.add_figure('activations_entropy_linerp/train', generate_acts_entropy_linerp_plot(acts_ent_linerp_train))
        self.writer.add_figure('activations_entropy_linerp/test', generate_acts_entropy_linerp_plot(acts_ent_linerp_test))
        self.writer.add_figure('weights_entropy_linerp', generate_weights_entropy_linerp_plot(weights_ent_linerp))

        for i, w in enumerate(get_weights_linerp(self.model.w_1, self.model.w_2)):
            self.writer.add_histogram('Weights histogram', w.detach().cpu(), i)
Beispiel #5
0
    def visualize_linerp(self):
        dummy_model = SimpleModel().to(self.config.firelab.device_name)

        w1to2_linerp_scores_train = linerp(self.model.w_1, self.model.w_2, dummy_model, self.train_dataloader)
        w1to2_linerp_scores_test = linerp(self.model.w_1, self.model.w_2, dummy_model, self.val_dataloader)

        self.writer.add_figure('w_1 to w_2 linerp/accuracy', generate_linerp_plot(
            [s[1] for s in w1to2_linerp_scores_train],
            [s[1] for s in w1to2_linerp_scores_test],
        ))

        self.writer.add_figure('w_1 to w_2 linerp/loss', generate_linerp_plot(
            [s[0] for s in w1to2_linerp_scores_train],
            [s[0] for s in w1to2_linerp_scores_test],
        ))
Beispiel #6
0
 def init_torch_model_builder(self):
     if self.config.hp.model_name == 'fast_resnet':
         self.torch_model_builder = lambda: FastResNet(
             n_classes=10, n_input_channels=self.config.hp.get('n_input_channels', 1)).nn
     elif self.config.hp.model_name == 'resnet18':
         self.torch_model_builder = lambda: ResNet18(
             n_classes=10, n_input_channels=self.config.hp.get('n_input_channels', 1)).nn
     elif self.config.hp.model_name == "vgg":
         self.torch_model_builder = lambda: VGG11(
             n_input_channels=self.config.hp.get('n_input_channels', 1),
             use_bn=self.config.hp.get('use_bn', True)).model
     elif self.config.hp.model_name == "simple":
         self.torch_model_builder = lambda: SimpleModel().nn
     elif self.config.hp.model_name == "conv":
         self.torch_model_builder = lambda: ConvModel(self.config.hp.conv_model_config).nn
     else:
         raise NotImplementedError(f"Model {self.config.hp.model_name} is not supported")
Beispiel #7
0
def test_class_getters_aka_quasi_manager(db: Session):
    # GIVEN
    with freeze_time("2020-01-02 03:45") as older_timestamp:
        timestamp = older_timestamp.time_to_freeze
        SimpleModelFactory(type=SimpleModel.Type.NORMAL, created_at=timestamp)
        SimpleModelFactory(type=SimpleModel.Type.SUPER, created_at=timestamp)

    with freeze_time("2021-02-03 04:56") as newer_timestamp:
        timestamp = newer_timestamp.time_to_freeze
        obj_to_return = SimpleModelFactory(type=SimpleModel.Type.NORMAL,
                                           created_at=timestamp)
        SimpleModelFactory(type=SimpleModel.Type.SUPER, created_at=timestamp)

    # WHEN
    result = SimpleModel.get_latest_by_type(db, SimpleModel.Type.NORMAL)

    # THEN
    assert result == obj_to_return