如何在Keras和Tensorflow中使用不同长度的输入和输出time_steps?

时间:2019-03-24 12:58:44

标签: python tensorflow machine-learning keras neural-network

假设我有一个Keras模型:

model = Sequential()

    model.add(LSTM(8, return_sequences=True, input_shape=(None, 1)))
    model.add(LSTM(8, return_sequences=True))
    model.add(TimeDistributed(Dense(1)))

    model.compile(loss='mean_squared_error',
                  optimizer='adam')

final_input_sentencefinal_output_sentence的形状均为(1,x,1),其中x只是较长字符串的长度。我使用while循环来计算:

while len(input_sentence) != len(output_sentence):

    if len(input_sentence) < len(output_sentence):
        input_sentence.append([0])
    elif len(input_sentence) > len(output_sentence):
        output_sentence.append([0])

我可以实时拟合此模型:

model.fit(final_input_sentence, final_output_sentence, epochs=1, verbose=0)

我的问题是如何使模型适合不同的time_steps(可能我必须更改模型,但问题是如何?)?因此,例如输入形状为(1,x,1),输出形状为(1,y,1)。这段代码可以正常工作,但是当我删除while循环时,会出现类似Incompatible shapes: [1,23,1] vs. [1,3,1]的错误。我知道我可以进行零填充等操作,但是我想生成没有任何长度限制的输出值。有什么想法我该怎么做?如果不清楚或需要任何其他信息,请告诉我。

0 个答案:

没有答案