tensorflow DynamicRnnEstimator - 没有前缀或后缀

时间:2017-12-30 13:19:51

标签: python tensorflow

我试图训练一个动态的rnn估算器,但似乎无法让回归器识别出我的数据的正确形状。

import random
import numpy as np
import tensorflow as tf
from tensorflow.contrib.learn import DynamicRnnEstimator
from tensorflow.contrib.learn.python.learn.estimators.constants import (
    ProblemType,
)
from tensorflow.contrib.learn.python.learn.estimators.rnn_common import (
    PredictionType,
)
from tensorflow.contrib.layers import real_valued_column

X = np.random.uniform(size=(1000, 10))
M = X.shape[0]
N = X.shape[1]
y = np.random.uniform(size=1000)

seq_feat_cols = [real_valued_column(column_name='X', dimension=N)]
rnn = DynamicRnnEstimator(ProblemType.LINEAR_REGRESSION,
                          PredictionType.SINGLE_VALUE,
                          sequence_feature_columns=seq_feat_cols)

def get_batch():
    period_steps = 20
    start = random.randint(0, (M - 1) - period_steps - 1)
    end = start + period_steps
    x_tf = tf.expand_dims(X[start:end], axis=0)
    return {'X': x_tf}, tf.constant(y[start:end])

rnn.fit(input_fn=get_batch, steps=10)

这是屈服:

ValueError: Provided a prefix or suffix of None: 1 and None

我试图扩大我的ndarray两侧的维度无济于事;任何建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

ValueError看起来像是因为num_units没有提供给DynamicRNNEstimator的构造函数。其他一些问题:

  • 您指定的input_fn只会运行一次!因此,它应该构建一个TensorFlow图,它可以迭代数据集或具有随机的TensorFlow操作。
  • 您似乎每个时间步都有一个标签,在这种情况下,我认为您需要MULTIPLE_VALUE而不是SINGLE_VALUE作为预测类型。
  • Estimator需要批量维度(可以是一个)

将所有这些放在一起:

import random
import numpy as np
import tensorflow as tf
from tensorflow.contrib.learn import DynamicRnnEstimator
from tensorflow.contrib.learn.python.learn.estimators.constants import (
    ProblemType,
)
from tensorflow.contrib.learn.python.learn.estimators.rnn_common import (
    PredictionType,
)
from tensorflow.contrib.layers import real_valued_column

X = np.random.uniform(size=(1000, 10))
M = X.shape[0]
N = X.shape[1]
y = np.random.uniform(size=1000)

seq_feat_cols = [real_valued_column('X')]
rnn = DynamicRnnEstimator(ProblemType.LINEAR_REGRESSION,
                          PredictionType.MULTIPLE_VALUE,
                          num_units=5,
                          sequence_feature_columns=seq_feat_cols)

def get_batch():
  period_steps = 20
  start = tf.random_uniform(
      shape=(),
      minval=0,
      maxval=(M - 1) - period_steps - 1,
      dtype=tf.int32)
  end = start + period_steps
  x_sliced = tf.constant(X)[None, start:end, :]
  y_sliced = tf.constant(y)[None, start:end]
  x_sliced.set_shape((1, period_steps, N))
  y_sliced.set_shape((1, period_steps))
  return {'X': x_sliced}, y_sliced

rnn.fit(input_fn=get_batch, steps=10)