首页 > 解决方案 > 在张量流中拟合 RNN 估计器

问题描述

我正在尝试使用RNNEstimator()该类训练 TF 估计器,但在定义估计器时遇到了麻烦。我的目标如下:

  1. 创建一个 tf.data.Dataset。
  2. 将其输入 RNN 估计器。

第一部分似乎工作正常。我定义

def _parse_func(record):
    # takes tf record as input and returns the following tensors
    # numeric_tensor.shape = (5,170) and y.shape=()
    return {'numerical': numeric_tensor,}, y

def input_fn(filenames=['data.tfrecord']):
    # Returns parsed tf record i.e. the tf.data.Dataset
    dataset = tf.data.TFRecordDataset(filenames=filenames)
    dataset = dataset.map(map_func=_parse_func)
    dataset = dataset.repeat()
    dataset = dataset.batch(batch_size=BATCH_SIZE)
    return dataset

现在让我们进入肉质部分

估算器负责创建会话和图形。因此,我只需按以下格式创建估算器:

# create the column
column = tf.contrib.feature_column.sequence_numeric_column('numerical')

# create the estimator
estimator = RNNEstimator(
    head=tf.contrib.estimator.regression_head(),
    sequence_feature_columns=[column],
    num_units=[32, 16], cell_type='lstm')

# train the estimator
estimator.train(input_fn=input_fn, steps=100)

但是,这不起作用。它给了我各种各样的错误!特别是,目前我得到:

TypeError:输入必须是 SparseTensor。

此外,我似乎无法将损失更改为对数损失。我尝试通过使用以下方法将其传递给 head 参数来设置它:

head = tf.contrib.estimator.regression_head(loss_fn=tf.losses.log_loss)

标签: pythontensorflow

解决方案


推荐阅读