def remap(self, field_map: Dict[str, str]): """Return a copy of this dataset with some fields renamed.""" new_spec = utils.remap_dict(self.spec(), field_map) new_examples = [ utils.remap_dict(ex, field_map) for ex in self.examples ] return Dataset(new_spec, new_examples, base=self)
def test_remap_dict(self): d = { "a": True, "b": False, "c": True } remap_dict = { "a": "a2", "b": "b2" } expected = { "a2": True, "b2": False, "c": True } self.assertDictEqual(expected, utils.remap_dict(d, remap_dict)) d = { "a": True, "b": False, "c": True } remap_dict = {} self.assertDictEqual(d, utils.remap_dict(d, remap_dict)) d = {} remap_dict = { "a": "a2", "b": "b2" } self.assertDictEqual(d, utils.remap_dict(d, remap_dict)) d = { "a": True, "b": False, "c": True } remap_dict = { "a": "b", } expected = { "b": False, "c": True } self.assertDictEqual(expected, utils.remap_dict(d, remap_dict))
def predict_minibatch(self, inputs): """Predict on a single minibatch of examples.""" model_inputs = [self.preprocess(ex) for ex in inputs] outputs = self.model.predict_minibatch(model_inputs) outputs = [utils.remap_dict(mo, self.FIELD_RENAMES) for mo in outputs] # TODO(gehrmann): temp solution to get ROUGE scores in data table. for ex, mo in zip(inputs, outputs): score = self._scorer.score(target=ex["reference"], prediction=self._get_pred_string( mo["output_text"])) mo["rougeL"] = float(score["rougeL"].fmeasure) return outputs
def predict(self, inputs): """Predict on a single minibatch of examples.""" inputs = list( inputs) # needs to be referenced below, so keep full list model_inputs = (self.preprocess(ex) for ex in inputs) outputs = self.wrapped.predict(model_inputs) outputs = (utils.remap_dict(mo, self.FIELD_RENAMES) for mo in outputs) # TODO(gehrmann): temp solution to get ROUGE scores in data table. for ex, mo in zip(inputs, outputs): score = self._scorer.score(target=ex["reference"], prediction=self._get_pred_string( mo["output_text"])) mo["rougeL"] = float(score["rougeL"].fmeasure) yield mo
def predict_minibatch(self, inputs): """Predict on a single minibatch of examples.""" model_inputs = [self.preprocess(ex) for ex in inputs] outputs = self.model.predict_minibatch(model_inputs) return [utils.remap_dict(mo, self.FIELD_RENAMES) for mo in outputs]
def predict(self, inputs): """Predict on a single minibatch of examples.""" model_inputs = (self.preprocess(ex) for ex in inputs) outputs = self.wrapped.predict(model_inputs) return (utils.remap_dict(mo, self.FIELD_RENAMES) for mo in outputs)