如何将组件的名称添加/更改到现有的 Tensorflow 数据集对象?

San*_*ta7 5 python tensorflow tensorflow-datasets

从 Tensorflow 数据集指南它说

为元素的每个组件命名通常很方便,例如,如果它们代表训练示例的不同特征。除了元组,您还可以使用 collections.namedtuple 或将字符串映射到张量的字典来表示数据集的单个元素。

dataset = tf.data.Dataset.from_tensor_slices(
   {"a": tf.random_uniform([4]),
    "b": tf.random_uniform([4, 100], maxval=100, dtype=tf.int32)})
print(dataset.output_types)  # ==> "{'a': tf.float32, 'b': tf.int32}"
print(dataset.output_shapes)  # ==> "{'a': (), 'b': (100,)}"
Run Code Online (Sandbox Code Playgroud)

https://www.tensorflow.org/guide/datasets

这在 Keras 中非常有用。如果将数据集对象传递给model.fit,则组件的名称可用于匹配 Keras 模型的输入。例子:

image_input = keras.Input(shape=(32, 32, 3), name='img_input')
timeseries_input = keras.Input(shape=(None, 10), name='ts_input')

x1 = layers.Conv2D(3, 3)(image_input)
x1 = layers.GlobalMaxPooling2D()(x1)

x2 = layers.Conv1D(3, 3)(timeseries_input)
x2 = layers.GlobalMaxPooling1D()(x2)

x = layers.concatenate([x1, x2])

score_output = layers.Dense(1, name='score_output')(x)
class_output = layers.Dense(5, activation='softmax', name='class_output')(x)

model = keras.Model(inputs=[image_input, timeseries_input],
                    outputs=[score_output, class_output])

train_dataset = tf.data.Dataset.from_tensor_slices(
    ({'img_input': img_data, 'ts_input': ts_data},
     {'score_output': score_targets, 'class_output': class_targets}))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(64)

model.fit(train_dataset, epochs=3)
Run Code Online (Sandbox Code Playgroud)

因此,它对于查找、添加和更改 tf 数据集对象中的组件的名称非常有用。完成这些任务的最佳方法是什么?

P-G*_*-Gn 6

您可以使用map对数据集进行修改,如果这是您正在寻找的。例如,要将普通tuple输出转换为dict具有有意义名称的输出,

import tensorflow as tf

# dummy example
ds_ori = tf.data.Dataset.zip((tf.data.Dataset.range(0, 10), tf.data.Dataset.range(10, 20)))
ds_renamed = ds_ori.map(lambda x, y: {'input': x, 'output': y})

batch_ori = ds_ori.make_one_shot_iterator().get_next()
batch_renamed = ds_renamed.make_one_shot_iterator().get_next()

with tf.Session() as sess:
  print(sess.run(batch_ori))
  print(sess.run(batch_renamed))
  # (0, 10)
  # {'input': 0, 'output': 10}
Run Code Online (Sandbox Code Playgroud)


All*_*hvk 1

虽然接受的答案有利于更改(现有)组件的名称,但它并没有谈论“添加”。这可以按如下方式完成:

y_dataset = x_dataset.map(fn1)

您可以在其中根据需要定义 fn1

@tf.function
def fn1(x):
    ##use x to derive additional columns u want. Set the shape as well
    y = {}
    y.update(x)
    y['new1'] = new1
    y['new2'] = new2
    return y
Run Code Online (Sandbox Code Playgroud)