使用Estimator构建LSTM网络

时间:2016-12-15 02:20:00

标签: tensorflow lstm

我正在尝试使用Estimator构建LSTM网络。我的数据看起来像

X = [[1,2,3], [2,3,4], ... , [98,99,100]]
y = [2, 3, ... , 99]

我正在使用Estimator:

regressor = learn.Estimator(model_fn=lstm_model,
                            params=model_params,
                            )

其中lstm_model函数是

def lstm_model(features, targets, mode, params):

    def lstm_cells(layers):
        if isinstance(layers[0], dict):
            return [tf.nn.rnn_cell.BasicLSTMCell(layer['steps'],state_is_tuple=True) for layer in layers]
        return [tf.nn.rnn_cell.BasicLSTMCell(steps, state_is_tuple=True) for steps in layers]

    stacked_lstm = tf.nn.rnn_cell.MultiRNNCell(lstm_cells(params['rnn_layers']), state_is_tuple=True)
    output, layers = tf.nn.rnn(stacked_lstm, [features], dtype=tf.float32)
    return learn.models.linear_regression(output, targets)

和params

model_params = {
    'steps': 1000,
    'learning_rate': 0.03,
    'batch_size': 24,
    'time_steps': 3,
    'rnn_layers': [{'steps': 3}],
    'dense_layers': [10, 10]
}

然后我做了拟合

regressor.fit(X, y)

我面临的问题是

output, layers = tf.nn.rnn(stacked_lstm, [features], dtype=tf.float32)

需要序列,但我不确定如何将我的功能拆分为张量列表。 lstm_model函数内的要素形状为(?,3)

我有两个问题,如何分批进行培训?以及如何分割“功能”以便

output, layers = tf.nn.rnn(stacked_lstm, [features], dtype=tf.float32)

不会抛出错误。我得到的错误是

    raise TypeError("%s that don't all match." % prefix)
TypeError: Tensors in list passed to 'values' of 'Concat' Op have types [float64, float32] that don't all match.

我正在使用tensorflow 0.12

1 个答案:

答案 0 :(得分:1)

我必须设置要素的形状 (batch_size,time_step,1)或(None,time_step,1)然后将功能取消堆叠以进入rnn。取消" time_step"中的功能。所以你有一个时间步长大小的张量列表,每个张量的形状应该是(None,1)或(batch_size,1)