def _forward(self, batch: dict, for_training: bool) -> dict: tensor_batch = arrays_to_variables(batch, self._cuda_device, for_training=for_training) return self._model.forward(**tensor_batch)
def test_forward_pass_runs_correctly(self): training_arrays = arrays_to_variables(self.dataset.as_array_dict()) output_dict = self.model.forward(**training_arrays) assert_almost_equal(numpy.sum(output_dict["label_probs"][0].data.numpy(), -1), 1, decimal=6)
def ensure_model_can_train_save_and_load(self, param_file: str): save_dir = os.path.join(self.TEST_DIR, "save_and_load_test") archive_file = os.path.join(save_dir, "model.tar.gz") model = train_model_from_file(param_file, save_dir) loaded_model = load_archive(archive_file).model state_keys = model.state_dict().keys() loaded_state_keys = loaded_model.state_dict().keys() assert state_keys == loaded_state_keys # First we make sure that the state dict (the parameters) are the same for both models. for key in state_keys: assert_allclose(model.state_dict()[key].numpy(), loaded_model.state_dict()[key].numpy(), err_msg=key) params = Params.from_file(self.param_file) reader = DatasetReader.from_params(params['dataset_reader']) iterator = DataIterator.from_params(params['iterator']) # We'll check that even if we index the dataset with each model separately, we still get # the same result out. model_dataset = reader.read(params['validation_data_path']) model_dataset.index_instances(model.vocab) model_batch_arrays = next(iterator(model_dataset, shuffle=False)) model_batch = arrays_to_variables(model_batch_arrays, for_training=False) loaded_dataset = reader.read(params['validation_data_path']) loaded_dataset.index_instances(loaded_model.vocab) loaded_batch_arrays = next(iterator(loaded_dataset, shuffle=False)) loaded_batch = arrays_to_variables(loaded_batch_arrays, for_training=False) # The datasets themselves should be identical. for key in model_batch.keys(): field = model_batch[key] if isinstance(field, dict): for subfield in field: self.assert_fields_equal(model_batch[key][subfield], loaded_batch[key][subfield], tolerance=1e-6, name=key + '.' + subfield) else: self.assert_fields_equal(model_batch[key], loaded_batch[key], 1e-6, key) # Set eval mode, to turn off things like dropout, then get predictions. model.eval() loaded_model.eval() model_predictions = model.forward(**model_batch) loaded_model_predictions = loaded_model.forward(**loaded_batch) # Check loaded model's loss exists and we can compute gradients, for continuing training. loaded_model_loss = loaded_model_predictions["loss"] assert loaded_model_loss is not None loaded_model_loss.backward() # Both outputs should have the same keys and the values for these keys should be close. for key in model_predictions.keys(): self.assert_fields_equal(model_predictions[key], loaded_model_predictions[key], tolerance=1e-4, name=key) return model, loaded_model
def test_forward_pass_runs_correctly(self): training_arrays = self.dataset.as_array_dict() _ = self.model.forward(**arrays_to_variables(training_arrays))
def test_forward_pass_runs_correctly(self): training_arrays = arrays_to_variables(self.dataset.as_array_dict()) output_dict = self.model.forward(**training_arrays) class_probs = output_dict['class_probabilities'][0].data.numpy() numpy.testing.assert_almost_equal(numpy.sum(class_probs, -1), numpy.ones(class_probs.shape[0]))