简体   繁体   English

如何集成 2 个 keras 模型输出?

[英]How 2 keras model outputs can be integrated?

I've been thinking if I were to build multiple keras models that have different objectives, I will illustrate with an example to explain what I mean.我一直在想,如果我要构建多个具有不同目标的 keras 模型,我将用一个例子来说明我的意思。 Let's assume the main objective is to predict the price for a stock sign, let's say there will be 2 models, the first model is for sentiment analysis and its main objective is to process news articles and predict whether the stock price will go up or down.让我们假设主要目标是预测股票符号的价格,假设有 2 个模型,第一个模型用于情绪分析,其主要目标是处理新闻文章并预测股价是上涨还是下跌. The second model would be an LSTM that takes as input the historical prices data and predicts the next period (which can be a minute, a day, a month ...) price.第二个模型是一个 LSTM,它将历史价格数据作为输入并预测下一个时期(可以是一分钟、一天、一个月......)的价格。 Let's say I want the LSTM model to consider as well the outcome of sentiment analysis and possibly the output of other models that predict different metrics in the same fashion, is this possible?假设我希望 LSTM 模型也考虑情绪分析的结果,以及可能以相同方式预测不同指标的其他模型的输出,这可能吗? and how is it usually done?它通常是如何完成的? I think the same logic might apply on many other examples (recommender systems, retail inventory prediction ...)我认为相同的逻辑可能适用于许多其他示例(推荐系统、零售库存预测......)

The Keras Functional API helps you to create more flexible models including the multiple input pipelines. Keras Functional API可帮助您创建更灵活的模型,包括多个输入管道。

Per your requirement, You have a RNN (LSTM) model which would do sequence processing of stock prices (numerical data) and another CNN or RNN model doing Text processing of news articles (text data).根据您的要求,您有一个 RNN (LSTM) 模型可以对股票价格(数值数据)进行序列处理,另一个 CNN 或 RNN 模型可以对新闻文章(文本数据)进行文本处理。 With the Keras Functional API, you could create a Model that will have these 2 modules in separate Input Pipelines which you could then merge to predict stock price.使用 Keras Functional API,您可以创建一个模型,该模型将在单独的输入管道中包含这两个模块,然后您可以合并这些模块以预测股票价格。

Example of a Shoe price predictor Model:鞋类价格预测模型示例:

在此处输入图片说明

In the figure you have 3 input pipelines (say specifications of a shoe, its description and the picture(s)), which would then be merged by layers.concatenate() to form the model to predict the price of the shoe.在图中,您有 3 个输入管道(比如鞋子的规格、描述和图片),然后通过layers.concatenate()将它们合并以形成模型来预测鞋子的价格。

TF Keras Functional API Guide TF Keras 函数式 API 指南

Example implementation of a 2-input question-answering model: 2 输入问答模型的示例实现:

from tensorflow.keras.models import Model
from tensorflow.keras import layers
from tensorflow.keras import Input

text_vocabulary_size = 10000
question_vocabulary_size = 10000
answer_vocabulary_size = 500

text_input = Input(shape=(None,), dtype='int32', name='text')

embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input)

encoded_text = layers.LSTM(32)(embedded_text)

question_input = Input(shape=(None,),
   dtype='int32',
   name='question')

embedded_question = layers.Embedding(
    32, question_vocabulary_size)(question_input)

encoded_question = layers.LSTM(16)(embedded_question)

concatenated = layers.concatenate([encoded_text, encoded_question],
   axis=-1)
answer = layers.Dense(answer_vocabulary_size,
   activation='softmax')(concatenated)

model = Model([text_input, question_input], answer)

model.compile(optimizer='rmsprop',
  loss='categorical_crossentropy',
  metrics=['acc'])

在此处输入图片说明

Refer Deep Learning with Python Book by François Chollet Chap 17.1请参阅 François Chollet 的《使用 Python 进行深度学习》一书第 17.1 章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM