本文整理匯總了Python中torchbearer.Model.fit_generator方法的典型用法代碼示例。如果您正苦於以下問題:Python Model.fit_generator方法的具體用法?Python Model.fit_generator怎麽用?Python Model.fit_generator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torchbearer.Model
的用法示例。
在下文中一共展示了Model.fit_generator方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_main_loop_metrics
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
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)
示例2: test_main_loop_verbose
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
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
示例3: test_main_loop_stop_training
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
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)
示例4: test_main_loop_callback_calls
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
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()
示例5: test_main_loop_validation_setup
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
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)
示例6: test_fit_valid_sets_args
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
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)
示例7: test_fit_no_valid
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
def test_fit_no_valid(self):
x = torch.rand(1, 5)
y = torch.rand(1, 5)
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)
torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
torchbearermodel.fit_generator = Mock()
fit = torchbearermodel.fit_generator
torchbearermodel.fit(x, y, 1, validation_split=None)
self.assertTrue(fit.call_args[1]['validation_generator'] is None)
示例8: test_main_loop_backward
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
def test_main_loop_backward(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 = None
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)
self.assertTrue(torchbearerstate[torchbearer.LOSS].backward.call_count == epochs*len(data))
示例9: test_main_loop_pass_state
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
def test_main_loop_pass_state(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 = None
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=True)
self.assertTrue(len(torchbearerstate[torchbearer.MODEL].call_args) == 2)
示例10: add_kld_loss_callback
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
def add_kld_loss_callback(state):
KLD = kld_Loss(state['mu'], state['logvar'])
return KLD
def save_reconstruction_callback(num_images=8, folder='results/'):
import os
os.makedirs(os.path.dirname(folder), exist_ok=True)
@torchbearer.callbacks.on_step_validation
def saver(state):
if state[torchbearer.BATCH] == 0:
data = state[torchbearer.X]
recon_batch = state[torchbearer.Y_PRED]
comparison = torch.cat([data[:num_images],
recon_batch.view(128, 1, 28, 28)[:num_images]])
save_image(comparison.cpu(),
str(folder) + 'reconstruction_' + str(state[torchbearer.EPOCH]) + '.png', nrow=num_images)
return saver
model = VAE()
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=0.001)
loss = bce_loss
from torchbearer import Model
torchbearer_model = Model(model, optimizer, loss, metrics=['loss']).to('cuda')
torchbearer_model.fit_generator(traingen, epochs=10, validation_generator=testgen,
callbacks=[add_kld_loss_callback, save_reconstruction_callback()], pass_state=True)
示例11: super
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
super(SimpleModel, self).__init__()
self.convs = nn.Sequential(
nn.Conv2d(3, 16, stride=2, kernel_size=3),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16, 32, stride=2, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 64, stride=2, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU()
)
self.classifier = nn.Linear(576, 10)
def forward(self, x):
x = self.convs(x)
x = x.view(-1, 576)
return self.classifier(x)
model = SimpleModel()
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=0.001)
loss = nn.CrossEntropyLoss()
from torchbearer import Model
torchbearer_model = Model(model, optimizer, loss, metrics=['acc', 'loss']).to('cuda')
torchbearer_model.fit_generator(traingen, epochs=10, validation_generator=testgen)
示例12: kld_Loss
# 需要導入模塊: from torchbearer import Model [as 別名]
# 或者: from torchbearer.Model import fit_generator [as 別名]
BCE = F.binary_cross_entropy(recon_x, x.view(-1, 784), size_average=False)
return BCE
def kld_Loss(mu, logvar):
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return KLD
# Reconstruction + KL divergence losses summed over all elements and batch
def loss_function(y_pred, y_true):
recon_x, mu, logvar = y_pred
x = y_true
BCE = bce_loss(recon_x, x)
KLD = kld_Loss(mu, logvar)
return BCE + KLD
model = VAE()
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=0.001)
loss = loss_function
from torchbearer import Model
torchbearer_model = Model(model, optimizer, loss, metrics=['loss']).to('cuda')
torchbearer_model.fit_generator(traingen, epochs=10, validation_generator=testgen, pass_state=False)