Ejemplo n.º 1
0
    def testGivens(self):
        class MockOperator(TrainingOperator):
            def setup(self, config):
                self.train_epoch = MagicMock(returns=dict(mean_accuracy=10))
                self.validate = MagicMock(returns=dict(mean_accuracy=10))

        def three_model_creator(config):
            return nn.Linear(1, 1), nn.Linear(1, 1), nn.Linear(1, 1)

        def three_optimizer_creator(models, config):
            opts = [
                torch.optim.SGD(model.parameters(), lr=0.1) for model in models
            ]
            return opts[0], opts[1], opts[2]

        runner = PyTorchRunner(three_model_creator,
                               single_loader,
                               three_optimizer_creator,
                               loss_creator,
                               training_operator_cls=MockOperator)
        runner.setup()

        self.assertEqual(len(runner.given_models), 3)
        self.assertEqual(len(runner.given_optimizers), 3)

        runner2 = PyTorchRunner(model_creator, single_loader,
                                optimizer_creator, loss_creator)
        runner2.setup()

        self.assertNotEqual(runner2.given_models, runner2.models)
        self.assertNotEqual(runner2.given_optimizers, runner2.optimizers)
Ejemplo n.º 2
0
 def testSingleLoader(self):
     runner = PyTorchRunner(model_creator, single_loader, optimizer_creator,
                            loss_creator)
     runner.setup()
     runner.step()
     with self.assertRaises(ValueError):
         runner.validate()
Ejemplo n.º 3
0
    def testMultiModel(self):
        def multi_model_creator(config):
            return nn.Linear(1, 1), nn.Linear(1, 1), nn.Linear(1, 1)

        def multi_optimizer_creator(models, config):
            opts = [
                torch.optim.SGD(model.parameters(), lr=0.1) for model in models
            ]
            return opts[0], opts[1], opts[2]

        runner = PyTorchRunner(multi_model_creator, single_loader,
                               multi_optimizer_creator, loss_creator)

        with self.assertRaises(ValueError):
            runner.setup()
Ejemplo n.º 4
0
    def testMultiLoaders(self):
        def three_data_loader(config):
            return (LinearDataset(2, 5), LinearDataset(2, 5, size=400),
                    LinearDataset(2, 5, size=400))

        runner = PyTorchRunner(model_creator, three_data_loader,
                               optimizer_creator, loss_creator)
        with self.assertRaises(ValueError):
            runner.setup()

        runner2 = PyTorchRunner(model_creator, three_data_loader,
                                optimizer_creator, loss_creator)
        with self.assertRaises(ValueError):
            runner2.setup()
Ejemplo n.º 5
0
 def testNativeLoss(self):
     runner = PyTorchRunner(model_creator,
                            single_loader,
                            optimizer_creator,
                            loss_creator=nn.MSELoss)
     runner.setup()
     runner.train_epoch()
Ejemplo n.º 6
0
 def testStep(self):
     mock_function = MagicMock(return_value=dict(mean_accuracy=10))
     runner = PyTorchRunner(
         model_creator,
         create_dataloaders,
         optimizer_creator,
         loss_creator,
         train_function=mock_function)
     runner.setup()
     runner.step()
     runner.step()
     result = runner.step()
     self.assertEqual(mock_function.call_count, 3)
     self.assertEqual(result["epoch"], 3)
     self.assertEqual(runner.stats()["epoch"], 3)
Ejemplo n.º 7
0
    def testtrain_epoch(self):
        class MockOperator(TrainingOperator):
            def setup(self, config):
                self.count = 0

            def train_epoch(self, *args, **kwargs):
                self.count += 1
                return {"count": self.count}

        runner = PyTorchRunner(model_creator,
                               create_dataloaders,
                               optimizer_creator,
                               loss_creator,
                               training_operator_cls=MockOperator)
        runner.setup()
        runner.train_epoch(num_steps=1)
        runner.train_epoch(num_steps=1)
        result = runner.train_epoch()
        self.assertEqual(runner.training_operator.count, 3)
        self.assertEqual(result["count"], 3)
        self.assertEqual(runner.stats()["epoch"], 3)
Ejemplo n.º 8
0
    def testGivens(self):
        def three_model_creator(config):
            return nn.Linear(1, 1), nn.Linear(1, 1), nn.Linear(1, 1)

        def three_optimizer_creator(models, config):
            opts = [
                torch.optim.SGD(model.parameters(), lr=0.1) for model in models
            ]
            return opts[0], opts[1], opts[2]

        runner = PyTorchRunner(three_model_creator, single_loader,
                               three_optimizer_creator, loss_creator)
        runner.setup()

        self.assertEqual(len(runner.given_models), 3)
        self.assertEqual(len(runner.given_optimizers), 3)

        runner2 = PyTorchRunner(model_creator, single_loader,
                                optimizer_creator, loss_creator)
        runner2.setup()

        self.assertNotEqual(runner2.given_models, runner2.models)
        self.assertNotEqual(runner2.given_optimizers, runner2.optimizers)
Ejemplo n.º 9
0
 def testValidate(self):
     mock_function = MagicMock(returns=dict(mean_accuracy=10))
     runner = PyTorchRunner(
         model_creator,
         create_dataloaders,
         optimizer_creator,
         loss_creator,
         validation_function=mock_function)
     runner.setup()
     runner.step()
     runner.step()
     runner.step()
     self.assertEqual(mock_function.call_count, 0)
     runner.validate()
     self.assertTrue(mock_function.called)
     self.assertEqual(runner.stats()["epoch"], 3)
Ejemplo n.º 10
0
    def testValidate(self):
        class MockOperator(TrainingOperator):
            def setup(self, config):
                self.train_epoch = MagicMock(returns=dict(mean_accuracy=10))
                self.validate = MagicMock(returns=dict(mean_accuracy=10))

        runner = PyTorchRunner(model_creator,
                               create_dataloaders,
                               optimizer_creator,
                               loss_creator,
                               training_operator_cls=MockOperator)
        runner.setup()
        runner.train_epoch()
        runner.train_epoch()
        runner.train_epoch()
        self.assertEqual(runner.training_operator.validate.call_count, 0)
        runner.validate()
        self.assertTrue(runner.training_operator.validate.called)
        self.assertEqual(runner.stats()["epoch"], 3)