Keras LSTM функциональный API несколько входов - PullRequest
0 голосов
/ 09 июля 2019

Я пытаюсь использовать два входа для обучения модели LSTM: цена и настроение, после нормализации этих двух данных: trainX и trainS, я следую документу keras, чтобы обучить режим

print(trainX.shape)
print(trainS.shape)
(22234, 1, 51) --> 51 is because these datasets are time sequence, and I look back for 51 hours of the history price data
(22285, 1)

Кодв основном следует документу с несколькими входами Keras: https://keras.io/getting-started/functional-api-guide/#all-models-are-callable-just-like-layers Но я получаю ошибку, когда подгоняю модель

Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[0., 0., 0., ..., 0., 0., 0.]],

       [[0., 0., 0., ..., 0., 0., 0.]],

       [[0., 0., 0., ..., 0., 0., 0.]],

       ...,

       [[0., 0., 0., ..., 0., 0., 0.]],

       [[0., 0., 0., ....

from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model

# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.
# Note that we can name any layer by passing it a "name" argument.
main_input = Input(shape=(trainX.shape[0],), dtype='int32', name='main_input')

# This embedding layer will encode the input sequence
# into a sequence of dense 512-dimensional vectors.
x = Embedding(output_dim=512, input_dim=10000, input_length=trainX.shape[0])(main_input)

# A LSTM will transform the vector sequence into a single vector,
# containing information about the entire sequence
lstm_out = LSTM(32)(x)
auxiliary_output = Dense(2, activation='sigmoid', name='aux_output')(lstm_out)

import keras
auxiliary_input = Input(shape=(trainS.shape[0],), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input])

# We stack a deep densely-connected network on top
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

# And finally we add the main logistic regression layer
main_output = Dense(2, activation='sigmoid', name='main_output')(x)

auxiliary_output = Dense(2, activation='sigmoid', name='aux_output')(lstm_out)

auxiliary_input = Input(shape=(5,), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input])

# We stack a deep densely-connected network on top
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

# And finally we add the main logistic regression layer
main_output = Dense(2, activation='sigmoid', name='main_output')(x)
model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])

model.compile(optimizer='rmsprop', loss='binary_crossentropy',
              loss_weights=[1., 0.2])
model.fit(trainX, trainS, epochs=100, batch_size=1, verbose=2, shuffle=False)

1 Ответ

0 голосов
/ 09 июля 2019

Вызов соответствия модели должен передать список np.arrays таким образом, чтобы их размер пакета был одинаковым, а оставшиеся измерения должны соответствовать тому, что определено для входных данных / целей.

т.е. вам нужно позвонить

model.fit([input0, input1], [output0, output1])

Все они должны иметь одинаковую форму [0].

Я заметил следующее в вашем коде:

main_input = Input(shape=(trainX.shape[0],)

Это неверно. Вы хотите, чтобы форма ввода была trainX.shape[1:]. Нет необходимости определять размер партии, но вы должны определить другие размеры.

...