gpt4 book ai didi

tensorflow - 使用 Estimator 构建 LSTM 网络

转载 作者:行者123 更新时间:2023-12-02 03:08:25 24 4
gpt4 key购买 nike

我正在尝试使用 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)

和参数是

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

最佳答案

我必须将特征的形状设置为(batch_size, time_step, 1) 或 (None, time_step, 1) 然后拆开特征以进入 rnn。拆开“time_step”中的特征,这样你就有了一个包含时间步长大小的张量列表,每个张量的形状应该是 (None, 1) 或 (batch_size, 1)

关于tensorflow - 使用 Estimator 构建 LSTM 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41155371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com