當前位置: 首頁>>代碼示例>>Python>>正文


Python torchbearer.Model類代碼示例

本文整理匯總了Python中torchbearer.Model的典型用法代碼示例。如果您正苦於以下問題:Python Model類的具體用法?Python Model怎麽用?Python Model使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Model類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_fit_valid_sets_args

    def test_fit_valid_sets_args(self, gtvs):
        x = torch.rand(1,5)
        y = torch.rand(1,5)
        val_data = (1,2)
        val_split = 0.2
        shuffle = False

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()
        metric = Metric('test')

        loss = torch.tensor([2], requires_grad=True)
        criterion = Mock(return_value=loss)

        gtvs.return_value = (1, 2)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
        torchbearermodel.fit_generator = Mock()
        torchbearermodel.fit(x, y, 1, validation_data=val_data, validation_split=val_split, shuffle=shuffle)

        gtvs.assert_called_once()
        self.assertTrue(list(gtvs.call_args[0][0].numpy()[0]) == list(x.numpy()[0]))
        self.assertTrue(list(gtvs.call_args[0][1].numpy()[0]) == list(y.numpy()[0]))
        self.assertTrue(gtvs.call_args[0][2] == val_data)
        self.assertTrue(gtvs.call_args[0][3] == val_split)
        self.assertTrue(gtvs.call_args[1]['shuffle'] == shuffle)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:27,代碼來源:test_torchbearer.py

示例2: test_main_loop_metrics

    def test_main_loop_metrics(self):
        metric = Metric('test')
        metric.process = Mock(return_value={'test': 0})
        metric.process_final = Mock(return_value={'test': 0})
        metric.reset = Mock(return_value=None)

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        generator = DataLoader(data)
        train_steps = len(data)

        epochs = 1

        callback = MagicMock()

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        loss = torch.tensor([2], requires_grad=True)
        criterion = Mock(return_value=loss)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
        torchbearerstate = torchbearermodel.fit_generator(generator, train_steps, epochs, 0, [callback], initial_epoch=0, pass_state=False)

        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].reset.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process.call_count == len(data))
        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process_final.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRICS]['test'] == 0)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:28,代碼來源:test_torchbearer.py

示例3: test_test_loop_stop_training

    def test_test_loop_stop_training(self):
        metric = Metric('test')
        metric_list = MetricList([metric])

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])),
                (torch.Tensor([3]), torch.Tensor([3]))]
        validation_generator = DataLoader(data)
        validation_steps = len(data)

        callback = MagicMock()
        callback_List = torchbearer.CallbackList([callback])

        torchmodel = Mock(return_value=1)
        optimizer = MagicMock()

        criterion = Mock(return_value=2)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])

        state = torchbearermodel.main_state.copy()
        state.update({torchbearer.METRIC_LIST: metric_list, torchbearer.VALIDATION_GENERATOR: validation_generator,
                      torchbearer.CallbackList: callback_List, torchbearer.VALIDATION_STEPS: validation_steps,
                      torchbearer.CRITERION: criterion, torchbearer.STOP_TRAINING: True, torchbearer.METRICS: {}})

        torchbearerstate = torchbearermodel._test_loop(state, callback_List, False, Model._load_batch_standard, num_steps=None)

        self.assertTrue(torchbearerstate[torchbearer.MODEL].call_count == 1)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:27,代碼來源:test_torchbearer.py

示例4: test_main_loop_verbose

    def test_main_loop_verbose(self):
        metric = Metric('test')

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        generator = DataLoader(data)
        train_steps = len(data)

        epochs = 1

        callback = MagicMock()

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        loss = torch.tensor([2], requires_grad=True)
        criterion = Mock(return_value=loss)

        import sys
        from io import StringIO
        saved_std_err = sys.stderr
        out = StringIO()
        sys.stderr = out

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
        torchbearerstate = torchbearermodel.fit_generator(generator, train_steps, epochs, 1, [callback], initial_epoch=0, pass_state=False)

        output = out.getvalue().strip()
        self.assertTrue(output != '')
        sys.stderr = saved_std_err
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:30,代碼來源:test_torchbearer.py

示例5: test_main_loop_stop_training

    def test_main_loop_stop_training(self):
        class stop_training_test_callback(Callback):
            def on_sample(self, state):
                super().on_sample(state)
                state[torchbearer.STOP_TRAINING] = True

        metric = Metric('test')

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        generator = DataLoader(data)
        train_steps = None

        epochs = 1

        callback = stop_training_test_callback()

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        loss = Mock()
        criterion = Mock(return_value=loss)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
        torchbearerstate = torchbearermodel.fit_generator(generator, train_steps, epochs, 0, [callback], initial_epoch=0, pass_state=True)
        self.assertTrue(torchbearerstate[torchbearer.MODEL].call_count == 1)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:26,代碼來源:test_torchbearer.py

示例6: test_main_loop_callback_calls

    def test_main_loop_callback_calls(self):
        metric = Metric('test')

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        generator = DataLoader(data)
        train_steps = 2

        epochs = 1

        callback = MagicMock()

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        loss = Mock()
        criterion = Mock(return_value=loss)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
        torchbearerstate = torchbearermodel.fit_generator(generator, train_steps, epochs, 0, [callback], initial_epoch=0, pass_state=True)
        callback.on_start.assert_called_once()
        callback.on_start_epoch.asser_called_once()
        callback.on_start_training.assert_called_once()
        self.assertTrue(callback.on_sample.call_count == train_steps*epochs)
        self.assertTrue(callback.on_forward.call_count == train_steps*epochs)
        self.assertTrue(callback.on_criterion.call_count == train_steps*epochs)
        self.assertTrue(callback.on_backward.call_count == train_steps*epochs)
        self.assertTrue(callback.on_step_training.call_count == train_steps*epochs)
        callback.on_end_training.assert_called_once()
        callback.on_end_epoch.assert_called_once()
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:30,代碼來源:test_torchbearer.py

示例7: test_main_loop_validation_setup

    def test_main_loop_validation_setup(self):
        metric = Metric('test')

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        generator = DataLoader(data)
        valgenerator = DataLoader(data)
        train_steps = 2

        epochs = 1

        callback = MagicMock()

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        loss = torch.tensor([2], requires_grad=True)
        criterion = Mock(return_value=loss)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
        torchbearermodel._test_loop = Mock()
        torchbearerstate = torchbearermodel.fit_generator(generator, train_steps, epochs, 0, [callback],
                                                          validation_generator=valgenerator, initial_epoch=0,
                                                          pass_state=False)

        self.assertTrue(torchbearerstate[torchbearer.VALIDATION_STEPS] == len(valgenerator))
        self.assertTrue(torchbearerstate[torchbearer.VALIDATION_GENERATOR] == valgenerator)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:27,代碼來源:test_torchbearer.py

示例8: test_test_loop_metrics

    def test_test_loop_metrics(self):
        metric = Metric('test')
        metric.process = Mock(return_value={'test': 0})
        metric.process_final = Mock(return_value={'test': 0})
        metric.reset = Mock(return_value=None)
        metric_list = MetricList([metric])

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        validation_generator = DataLoader(data)
        validation_steps = len(data)

        callback = MagicMock()
        callback_List = torchbearer.CallbackList([callback])

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        criterion = Mock(return_value=2)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])

        state = torchbearermodel.main_state.copy()
        state.update({torchbearer.METRIC_LIST: metric_list, torchbearer.VALIDATION_GENERATOR: validation_generator,
                 torchbearer.CallbackList: callback_List, torchbearer.MODEL: torchmodel, torchbearer.VALIDATION_STEPS: validation_steps,
                 torchbearer.CRITERION: criterion, torchbearer.STOP_TRAINING: False, torchbearer.METRICS: {}})

        torchbearerstate = torchbearermodel._test_loop(state, callback_List, False, Model._load_batch_standard, num_steps=None)

        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].reset.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process.call_count == len(data))
        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process_final.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRICS]['test'] == 0)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:33,代碼來源:test_torchbearer.py

示例9: test_load_batch_predict_list

    def test_load_batch_predict_list(self):
        items = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2]))]
        iterator = iter(items)
        state = {'training_iterator': iterator, 'device': 'cpu', 'dtype': torch.int}

        Model._load_batch_predict('training', state)
        self.assertTrue(state['x'].item() == items[0][0].item())
        self.assertTrue(state['y_true'].item() == items[0][1].item())
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:8,代碼來源:test_torchbearer.py

示例10: test_deep_to_tensor

    def test_deep_to_tensor(self):
        tensor = MagicMock()
        new_dtype = torch.float16
        new_device = 'cuda:1'

        Model._deep_to(tensor, new_device, new_dtype)
        self.assertTrue(tensor.to.call_args[0][0] == new_device)
        self.assertTrue(tensor.to.call_args[0][1] == new_dtype)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:8,代碼來源:test_torchbearer.py

示例11: test_deep_to_tensor_int_dtype

    def test_deep_to_tensor_int_dtype(self):
        tensor = MagicMock()
        tensor.dtype = torch.uint8
        new_device = 'cuda:1'
        new_dtype = torch.uint8

        Model._deep_to(tensor, new_device, new_dtype)
        self.assertTrue(tensor.to.call_args[0][0] == new_device)
        self.assertTrue(len(tensor.to.call_args[0]) == 1)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:9,代碼來源:test_torchbearer.py

示例12: test_state_dict_kwargs

    def test_state_dict_kwargs(self):
        keywords = {'destination': None, 'prefix': '', 'keep_vars': False}
        torchmodel = MagicMock()
        optimizer = MagicMock()

        torchbearermodel = Model(torchmodel, optimizer, torch.nn.L1Loss(), [])
        torchbearermodel.state_dict(**keywords)

        self.assertTrue(torchmodel.state_dict.call_args[1] == keywords)
        self.assertTrue(optimizer.state_dict.call_args[1] == {})
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:10,代碼來源:test_torchbearer.py

示例13: test_eval

    def test_eval(self):
        torchmodel = torch.nn.Sequential(torch.nn.Linear(1,1))
        optimizer = MagicMock()
        metric_list = MagicMock()

        torchbearermodel = Model(torchmodel, optimizer, torch.nn.L1Loss(), [])
        torchbearermodel.main_state = {torchbearer.MODEL: torchmodel, torchbearer.METRIC_LIST: metric_list}
        torchbearermodel.eval()
        self.assertTrue(torchbearermodel.main_state[torchbearer.MODEL].training == False)
        torchbearermodel.main_state[torchbearer.METRIC_LIST].eval.assert_called_once()
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:10,代碼來源:test_torchbearer.py

示例14: test_deep_to_list

    def test_deep_to_list(self):
        tensor_1 = MagicMock()
        tensor_2 = MagicMock()
        tensors = [tensor_1, tensor_2]
        new_dtype = torch.float16
        new_device = 'cuda:1'

        Model._deep_to(tensors, new_device, new_dtype)
        for tensor in tensors:
            self.assertTrue(tensor.to.call_args[0][0] == new_device)
            self.assertTrue(tensor.to.call_args[0][1] == new_dtype)
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:11,代碼來源:test_torchbearer.py

示例15: test_cpu

    def test_cpu(self):
        torchmodel = torch.nn.Sequential(torch.nn.Linear(1,1))
        torchmodel.load_state_dict = Mock()

        optimizer = torch.optim.SGD(torchmodel.parameters(), 0.1)
        optimizer.load_state_dict = Mock()

        torchbearermodel = Model(torchmodel, optimizer, torch.nn.L1Loss(), [])
        torchbearermodel.to = Mock()
        torchbearermodel.cpu()

        self.assertTrue(torchbearermodel.to.call_args[0][0] == 'cpu')
開發者ID:little1tow,項目名稱:torchbearer,代碼行數:12,代碼來源:test_torchbearer.py


注:本文中的torchbearer.Model類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。