首页 文章

连接嵌入式输入以在keras中提供循环LSTM

提问于
浏览
0

我正在试图找出如何将功能模型提供给keras中的LSTM门 . 我有一个时间序列的元组(int,float,float) . 整数不是有序的,应该通过并嵌入图层 . 然后我想要元组(在嵌入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 layer要求输入具有匹配的形状,但concat轴除外 . 得到输入形状:[(无,1,3),(无,1),(无,1)]

我非常肯定可以安排重塑 . 但我真正想知道的是:这是否需要一些处理来喂养Keras时间序列的数据?

我也不确定如何为连接结果提供LSTM门 . 我能找到的所有重复示例都使用顺序模型 .

1 回答

  • 2

    嵌入层需要您拥有整个整数序列,因此它可以正确计算 .

    所以,作为第一步,我将分割输入数据:

    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)
    

相关问题