【问题标题】:How can I merge Tensorflow Dataset columns?如何合并 TensorFlow Dataset 列?
【发布时间】:2021-02-15 14:19:47
【问题描述】:

我有一个 Keras 模型,它采用形状为 (n, 288, 1) 的输入层,其中 288 是特征数。我正在使用 TensorFlow 数据集 tf.data.experimental.make_batched_features_dataset,我的输入层将是 (n, 1, 1),这意味着它一次为模型提供一个特征。如何制作形状为 (n, 288, 1) 的输入张量?我的意思是如何在一个张量中使用我的所有特征?

【问题讨论】:

  • 问题与kubeflow 无关 - 请不要向无关标签发送垃圾邮件(已删除)。

标签: tensorflow keras tensorflow-datasets


【解决方案1】:

您可以在 Keras 输入层中指定输入的形状。这里是一个示例代码,其中演示了相同的虚拟数据。

import tensorflow as tf

## Creating dummy data for demo
def make_sample():
    return tf.random.normal([288, 1])

n_samples = 100
samples = [make_sample() for _ in range(n_samples)]
labels = [tf.random.uniform([1]) for _ in range(n_samples)]


# Use tf.data to create dataset
batch_size = 4

dataset = tf.data.Dataset.from_tensor_slices((samples, labels))
dataset = dataset.batch(batch_size)

# Build keras function model
inputs = tf.keras.Input(shape=[288, 1], name='input')
x = tf.keras.layers.Dense(1)(inputs)
model = tf.keras.Model(inputs=[inputs], outputs=[x])

# Compile loss and optimizer
model.compile(loss='mse', optimizer='sgd', metrics=['mae'])
model.fit(dataset, epochs=1)

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    相关资源
    最近更新 更多