我不完全确定这是否是最干净的解决方案,但我将所有内容拼接在一起。10 个单词位置中的每一个都有自己的输入,但这应该不是太大的问题。这个想法是制作一个嵌入层并多次使用它。首先,我们将生成一些数据:
n_samples = 1000
time_series_length = 50
news_words = 10
news_embedding_dim = 16
word_cardinality = 50
x_time_series = np.random.rand(n_samples, time_series_length, 1)
x_news_words = np.random.choice(np.arange(50), replace=True, size=(n_samples, time_series_length, news_words))
x_news_words = [x_news_words[:, :, i] for i in range(news_words)]
y = np.random.randint(2, size=(n_samples))
现在我们将定义图层:
## Input of normal time series
time_series_input = Input(shape=(50, 1, ), name='time_series')
## For every word we have it's own input
news_word_inputs = [Input(shape=(50, ), name='news_word_' + str(i + 1)) for i in range(news_words)]
## Shared embedding layer
news_word_embedding = Embedding(word_cardinality, news_embedding_dim, input_length=time_series_length)
## Repeat this for every word position
news_words_embeddings = [news_word_embedding(inp) for inp in news_word_inputs]
## Concatenate the time series input and the embedding outputs
concatenated_inputs = concatenate([time_series_input] + news_words_embeddings, axis=-1)
## Feed into LSTM
lstm = LSTM(16)(concatenated_inputs)
## Output, in this case single classification
output = Dense(1, activation='sigmoid')(lstm)
编译模型后,我们可以像这样拟合它:
model.fit([x_time_series] + x_news_words, y)
编辑:
在您在评论中提到的内容之后,您可以添加一个总结新闻的密集层,并将其添加到您的时间序列(股票价格)中:
## Summarize the news:
news_words_concat = concatenate(news_words_embeddings, axis=-1)
news_words_transformation = TimeDistributed(Dense(combined_news_embedding))(news_words_concat)
## New concat
concatenated_inputs = concatenate([time_series_input, news_words_transformation], axis=-1)