我正在努力弄清楚如何将一个功能模型提供给keras中的LSTM门。我有一个元组的时间序列(整数,浮点数,浮点数)。ints是不有序的,应该通过和嵌入层。然后,我希望元组(在嵌入int之后)通过LSTM层。
我开始的时候
from keras.layers import Input
from keras.layers import Embedding
from keras.layers import LSTM
import keras
inputs = [(42, 0.5, 0.6), (36, 0.4, 0.7), (50, 0.2, 0.9)] # example. The real data is a sequence of millions of tuples
input_id = Input(shape=(1,), dtype='int32', name='input_type') # id is within [0, 99]
embed_id = Embedding(output_dim=3, input_dim=20, input_length=1)(input_id)
input_v1 = Input(shape=(1,), dtype='float', name='input_v1')
input_v2 = Input(shape=(1,), dtype='float', name='input_v2')
input_merged = keras.layers.concatenate([embed_id, input_v1, input_v2], axis=-1)
lstm = LSTM(40) # how do I tell it to use input_merged as input ?
级联抱怨: ValueError:Concatenate
层需要具有匹配形状的输入,但连接轴除外。得到输入形状:(无,1,3),(无,1),(无,1)
我很确定这可以用整形来安排。但我真正想知道的是:这是给需要处理的Keras时间序列提供数据的正确方法吗?
我也不确定如何为LSTM提供连接结果。所有重复的例子,我可以找到使用顺序模型。
发布于 2017-07-07 06:25:04
嵌入层需要有完整的整数序列,这样它才能正确计算。
因此,作为第一步,我将对输入数据进行拆分:
intInputs = np.array([el[0] for el in inputs])
floatInputs = np.array([el[1],el[2]] for el in inputs)
#this could be optimized creating empty numpy arrays first and doing only one loop to fill them
在那之后,我们必须明白,如果我们把整个序列给嵌入,我们也必须把整个序列传递给模型。为此,我认为最好重新调整输入,使之具有多个时间步骤的“一个样本”(除非您确实有多个序列):
intInputs = intInputs.reshape((1,seqLength))
floatInputs = floatInputs.reshape((1,seqLength,2))
然后我们去找模特:
input_id=Input((seqLength,))
input_v =Input((seqLength,2))
embed_id = Embedding(output_dim=3,input_dim=20,input_length=seqLength)(input_id)
#are you sure "input_dim" is 20? Normally you should have the total amount of existing Ids, and I suppose this should at least be the last id+1.
这样,连接将有形状(None,seqLength,3),(None,seqLength,2),并且您将能够在最后一个轴上连接(其他的长度相等)。
input_merged = Concatenate(axis=-1)([embed_id,input_v])
而LSTM作为任何其他层接收输入:
lstm = LSTM(40)(input_merged)
https://stackoverflow.com/questions/44960558
复制相似问题