def _input_fn(): """Example-based input function.""" english_label = label_vocabulary[1] if label_vocabulary else 1.0 chinese_label = label_vocabulary[0] if label_vocabulary else 0.0 if n_classes > 2: # For multi-class labels, English is class 0, Chinese is class 1. chinese_label = label_vocabulary[1] if label_vocabulary else 1 english_label = label_vocabulary[0] if label_vocabulary else 0 serialized_examples = [ x.SerializeToString() for x in [ util.make_example( age=1.0, language='english', label=english_label), util.make_example( age=2.0, language='english', label=english_label), util.make_example( age=3.0, language='chinese', label=chinese_label), util.make_example( age=4.0, language='chinese', label=chinese_label) ] ] features = tf.io.parse_example(serialized=serialized_examples, features=feature_spec) labels = features.pop('label') if n_classes > 2 and not label_vocabulary: labels = tf.sparse.to_dense(labels, default_value=-1) return features, labels
def _input_fn(): """Example-based input function.""" serialized_examples = [ x.SerializeToString() for x in [ util.make_example(age=1.0, language='english', label=1.0), util.make_example(age=2.0, language='english', label=1.0), util.make_example(age=3.0, language='chinese', label=0.0), util.make_example(age=4.0, language='chinese', label=0.0) ] ] features = tf.parse_example(serialized_examples, feature_spec) labels = features.pop('label') return features, labels
def testMakeExample(self): expected = example_pb2.Example() expected.features.feature['single_float'].float_list.value[:] = [1.0] expected.features.feature['single_int'].int64_list.value[:] = [2] expected.features.feature['single_str'].bytes_list.value[:] = ['apple'] expected.features.feature['multi_float'].float_list.value[:] = [ 4.0, 5.0 ] expected.features.feature['multi_int'].int64_list.value[:] = [6, 7] expected.features.feature['multi_str'].bytes_list.value[:] = [ 'orange', 'banana' ] self.assertEqual( expected, util.make_example(single_float=1.0, single_int=2, single_str='apple', multi_float=[4.0, 5.0], multi_int=[6, 7], multi_str=['orange', 'banana']))
def make_example_with_label(values_t1=None, values_t2=None, values_t3=None): """Make example with label.""" effective_t1 = 0.0 effective_t2 = 0.0 effective_t3 = 0.0 args = {} if values_t1 is not None: args['values_t1'] = float(values_t1) effective_t1 = values_t1 if values_t2 is not None: args['values_t2'] = float(values_t2) effective_t2 = values_t2 if values_t3 is not None: args['values_t3'] = float(values_t3) effective_t3 = values_t3 label = (3 * effective_t1 + 6 * effective_t2 + 9 * effective_t3 + 4 * (effective_t1 + effective_t1**2 + effective_t1**3) + 5 * (effective_t2 + effective_t2**2 + effective_t2**3) + 6 * (effective_t3 + effective_t3**2 + effective_t3**3)) args['label'] = float(label) return util.make_example(**args)
def _makeExample(self, **kwargs): return util.make_example(**kwargs)
def _makeExample(self, **kwargs) -> example_pb2.Example: return util.make_example(**kwargs)
def _makeExample(self, **kwargs) -> tf.train.Example: return util.make_example(**kwargs)
def generate_regression_examples( num_examples) -> Iterator[example_pb2.Example]: for _ in range(num_examples): yield util.make_example(label=float(np.random.random()), prediction=float(np.random.uniform()))
def generate_classification_examples( num_examples) -> Iterator[example_pb2.Example]: for _ in range(num_examples): yield util.make_example(label=float(np.random.choice([0, 1])), prediction=float(np.random.uniform()))