During the preprocessing of a tensorflow dataset I need to check whether a certain value is contained in an unmutable set. If it isn't I need to replace it with a default value. Essentially it is about censoring/replacing certain outliers
In python I would do something like this:
def map_id (value):
s = frozenset([1,2,3])
if value in s:
return value
else:
return 0 # default for all outliers
This map_id function will be called like this
def preprocess(item):
return (map_id(item["investment_id"]), item["features"]), item["target"]
The preprocess function will be called like this
def make_dataset(file_paths, batch_size=4096, mode="train"):
ds = tf.data.TFRecordDataset(file_paths)
ds = ds.map(decode_function)
ds = ds.map(preprocess)
if mode == "train":
ds = ds.shuffle(batch_size * 4)
ds = ds.batch(batch_size).cache().prefetch(tf.data.AUTOTUNE)
return ds
How to write this map_id function in Tensorflow 2.x?