【发布时间】:2016-11-01 06:03:57
【问题描述】:
我正在使用 TensorFlow 对时间序列数据进行预测。所以就像我有 50 个标签,我想找出下一个可能的 5 个标签。
我浏览了教程演示:循环神经网络
但我发现它可以像上图中的第五个那样提供,这是不同的。
我想知道我可以使用哪种型号?我正在考虑 seq2seq 模型,但不确定它是否正确。
【问题讨论】:
标签: tensorflow deep-learning lstm
我正在使用 TensorFlow 对时间序列数据进行预测。所以就像我有 50 个标签,我想找出下一个可能的 5 个标签。
我浏览了教程演示:循环神经网络
但我发现它可以像上图中的第五个那样提供,这是不同的。
我想知道我可以使用哪种型号?我正在考虑 seq2seq 模型,但不确定它是否正确。
【问题讨论】:
标签: tensorflow deep-learning lstm
您是对的,您可以使用 seq2seq 模型。为简洁起见,我写了一个示例,说明如何在 Keras 中执行此操作,它也有一个 Tensorflow 后端。我没有运行这个例子,所以它可能需要调整。如果您的标签是 one-hot,则需要使用交叉熵损失。
from keras.models import Model
from keras.layers import Input, LSTM, RepeatVector
# The input shape is your sequence length and your token embedding size
inputs = Input(shape=(seq_len, embedding_size))
# Build a RNN encoder
encoder = LSTM(128, return_sequences=False)(inputs)
# Repeat the encoding for every input to the decoder
encoding_repeat = RepeatVector(5)(encoder)
# Pass your (5, 128) encoding to the decoder
decoder = LSTM(128, return_sequences=True)(encoding_repeat)
# Output each timestep into a fully connected layer
sequence_prediction = TimeDistributed(Dense(1, activation='linear'))(decoder)
model = Model(inputs, sequence_prediction)
model.compile('adam', 'mse') # Or categorical_crossentropy
model.fit(X_train, y_train)
【讨论】:
(num_samples, seq_len, one_hot_size) 并且你应该使用交叉熵损失
sequence_prediction 需要与上述模型中的 y_train 大小相同:y_train.shape = (num_samples, 5, 1)。如果你使用它,用你的嵌入大小替换最后一个维度,但这应该从你的数据中自然产生。