我试图训练一个动态的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两侧的维度无济于事;任何建议将不胜感激!
答案 0 :(得分:1)
ValueError
看起来像是因为num_units
没有提供给DynamicRNNEstimator
的构造函数。其他一些问题:
input_fn
只会运行一次!因此,它应该构建一个TensorFlow图,它可以迭代数据集或具有随机的TensorFlow操作。MULTIPLE_VALUE
而不是SINGLE_VALUE
作为预测类型。将所有这些放在一起:
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)