def test_forward_pass_with_missing_data(): it = Undivided(default=np.random.randn(3, 2, 4))(simple_net.handler) with pytest.raises(KeyError): for _ in run_network(simple_net, it): pass for _ in run_network(simple_net, it, all_inputs=False): pass
def train(self, net, training_data_iter, **named_data_iters): """ Train a network using a data iterator and further named data iterators. """ if self.verbose: if self.logging_function == print: self.logging_function('\n\n' + 10 * '- ' + "Before Training" + 10 * ' -') else: self.logging_function(10 * '- ' + "Before Training" + 10 * ' -') assert set(training_data_iter.data_shapes.keys()) == set( net.buffer.Input.outputs.keys()), \ "The data names provided by the training data iterator {} do not "\ "map to the network input names {}".format( training_data_iter.data_shapes.keys(), net.buffer.Input.outputs.keys()) self.stepper.start(net) named_data_iters['training_data_iter'] = training_data_iter self._start_hooks(net, named_data_iters) if self._emit_hooks(net, 'update') or self._emit_hooks(net, 'epoch'): return should_stop = False while not should_stop: self.current_epoch_nr += 1 sys.stdout.flush() train_scores = {s.__name__: [] for s in self.train_scorers} train_scores.update({n: [] for n in net.get_loss_values()}) if self.verbose: if self.logging_function == print: self.logging_function('\n\n' + 12 * '- ' + "Epoch" + str(self.current_epoch_nr) + 12 * ' -') else: self.logging_function(12 * '- ' + "Epoch" + str(self.current_epoch_nr) + 12 * ' -') iterator = training_data_iter(handler=net.handler) for _ in run_network(net, iterator): self.current_update_nr += 1 self.stepper.run() gather_losses_and_scores(net, self.train_scorers, train_scores) net.apply_weight_modifiers() if self._emit_hooks(net, 'update'): should_stop = True break self._add_log('rolling_training', aggregate_losses_and_scores(train_scores, net, self.train_scorers)) should_stop |= self._emit_hooks(net, 'epoch')