本文整理汇总了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)
示例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)
示例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)
示例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
示例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)
示例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()
示例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)
示例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)
示例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())
示例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)
示例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)
示例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] == {})
示例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()
示例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)
示例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')