【问题标题】:Tensorflow feed multiple parameters through input_fn pipelineTensorFlow 通过 input_fn 管道提供多个参数
【发布时间】:2023-03-06 13:55:01
【问题描述】:

我正在编写一个高级 tensorflow 应用程序,与构建 this minst estimator 的方式完全相同,只是我正在构建一个预测序列的简单 RNN。我是 tensorflow 的新手,所以我试图解决一个对于以前在 tensorflow 高级 api 中工作过的人来说可能实际上很简单的问题。

这是我的代码的 sn-p 给出一个想法:

def main(argv=None):
    """Run the training experiment."""
    ....
    # Setup the Estimator
    model_estimator = build_estimator(config, params)
    # Setup and start training and validation
    train_spec = tf.estimator.TrainSpec(
        input_fn=lambda: get_train_inputs(128),
        max_steps=2000)
    ...
    tf.estimator.train_and_evaluate(model_estimator, train_spec, eval_spec)

def build_estimator(config, params):
    return tf.estimator.Estimator(
        model_fn=model_fn,
        config=config,
        params=params,
    )

def model_fn(features, mode, params):
    #Input data
    _inputs = tf.placeholder(tf.int32, shape=[batch_size, times_steps])
    _labels = tf.placeholder(tf.float32, shape=[batch_size, num_classes])
    # Sequence lengths for dynamic allocation
    _seqlens = tf.placeholder(tf.int32, shape=[batch_size])
    ...
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=predictions,
        loss=loss,
        train_op=train_op,
        eval_metric_ops=eval_metric_ops
    )

这是我的输入管道函数:

# Get train inputs function
def get_train_inputs(batch_size):
    def train_inputs(batch_size):
            # Build dataset iterator
            x_batch, y_batch, seqlen_batch = sequence_generator.get_sentence_batch(
                batch_size, sequence_generator.train_x, sequence_generator.train_y, sequence_generator.train_seqlens)
            features={'_inputs': x_batch, '_labels': y_batch, '_seqlens': seqlen_batch}
            return features
    return train_inputs(batch_size)

由于我的代码大小,我只在此处粘贴了相关的代码。 这里的问题是:

train_spec = tf.estimator.TrainSpec(
    input_fn=lambda: get_train_inputs(128),
    max_steps=2000)

get_train_inputs(128)features 字典输入到 model_fn 的 _inputs 占位符中,因此 _labels_seqlens 在执行期间保持空白并抛出错误,即没有为这些占位符指定值。 model_fn 只接受两个特征参数:featureslabels。如何将_inputs_labels_seqlens 这三个参数全部输入到模型中?

任何建议都将受到高度赞赏。

注意:输入第三个参数_seqlens 的原因是因为我在我的model_fn 中使用tf.nn.dynamic_rnn,这需要序列长度,而标签在tf.nn.softmax_cross_entropy_with_logits 中使用我的 softmax 函数。

【问题讨论】:

    标签: python tensorflow deep-learning rnn tensorflow-estimator


    【解决方案1】:

    您根本不应该在tf.Estimator 中使用占位符。您应该查看tf.data API (see here)。您的输入函数应返回一次性迭代器的 get_next 操作。抱歉,如果您已经这样做了,但从您的代码中不清楚您的输入函数究竟返回了什么。
    假设您将其设置为像示例中那样返回一个字典,那么您将能够在模型函数中简单地使用 _inputs = features["_inputs"] 等。

    【讨论】:

      【解决方案2】:
      • 除了@xdurch0 答案,使用 FeatureColumns tf.feature_column 描述数据集的特征,这些特征作为输入传递给 Estimator model_fn 以进行训练和评估。
      • model_fn内,使用方法 tf.feature_column.input_layer()` 返回一个密集张量作为 基于指定 FeatureColumn 的输入层。

        您可以查看使用 FeatureColumns here 的示例。

      【讨论】:

        猜你喜欢
        • 2020-04-27
        • 2017-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多