Computing sample weight in Tensorflow dataset pipeline

Viewed 520

Suppose I have the following Tensorflow Dataset where the labels are [0,1] int values. The dataset is highly imbalanced and I've already computed that I'd like to use sample weights {1: 5.1, 0: 0.8} applied as a mapping.

The weight is not part of the original TFRecords file. How can I amend my code to incorporate this sample weight mapping such that I return a "sample_weight" feature that I can later use in my custom Estimator?

def train_input_fn(self):       
    feature_map = _get_features()

    def _parse_line(line):
        parsed_features = tf.parse_example(line, feature_map)
        labels = parsed_features.pop('target_open')

        return parsed_features, tf.reshape(labels, (-1,1))

    dataset = tf.data.TFRecordDataset('train.tfrecords')\
        .shuffle(buffer_size=10000)\
        .batch(self.batch_size)\
        .map(_parse_line, num_parallel_calls=6)\
        .repeat()\
        .prefetch(2)

    return dataset
0 Answers
Related