def get_inputs( self, output_index: int) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: """Returns labels, predictions, and weights for output at given offset.""" labels, preds, example_weights = super(TFCompilableMetricsAccumulator, self).get_inputs(output_index) if self._pad: def pad_value( name: Text, a: np.ndarray, configured_value: Optional[Union[float, int]] ) -> Union[int, float]: if configured_value is None: return 0 if a.dtype.kind == 'i' else .0 if isinstance(configured_value, int) and a.dtype.kind == 'i': return configured_value if isinstance(configured_value, float) and a.dtype.kind == 'f': return configured_value raise ValueError( '%s padding is configured to be %s but data is %s' % (name, type(configured_value), a.dtype)) labels = [ metric_util.pad(l, self._pad_to_dim, pad_value('label', l, self._label_padding)) for l in labels ] preds = [ metric_util.pad( p, self._pad_to_dim, pad_value('prediction', p, self._prediction_padding)) for p in preds ] return (np.array(labels), np.array(preds), np.array(example_weights))
def get_inputs( self, output_index: int) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: """Returns labels, predictions, and weights for output at given offset.""" labels, preds, example_weights = self._inputs[output_index] if self._pad: def pad_value(a: np.array) -> Union[int, float]: return -1 if a.dtype.kind == 'i' else -1.0 labels = [ metric_util.pad(l, self._last_dim, pad_value(l)) for l in labels ] preds = [ metric_util.pad(p, self._last_dim, pad_value(p)) for p in preds ] return (np.array(labels), np.array(preds), np.array(example_weights))
def testPad2D(self): self.assertAllClose( np.array([[1.0, 2.0, 0.0, 0.0, 0.0], [3.0, 4.0, 0.0, 0.0, 0.0]]), metric_util.pad(np.array([[1.0, 2.0], [3.0, 4.0]]), 5, 0.0))
def testPad1DMultipleValues(self): self.assertAllClose(np.array([1.0, 2.0, -1.0, -1.0]), metric_util.pad(np.array([1.0, 2.0]), 4, -1.0))
def testPad1DSingleValue(self): self.assertAllClose(np.array([1.0, -1.0]), metric_util.pad(np.array([1.0]), 2, -1.0))
def testPadNoChange(self): self.assertAllClose(np.array([1.0, 2.0]), metric_util.pad(np.array([1.0, 2.0]), 2, -1.0))