【问题标题】:build two Sequential lstm networks构建两个顺序 lstm 网络
【发布时间】:2020-12-15 18:17:36
【问题描述】:

我有以下模型,我想构建相同的序列网络并最终连接两个网络的输出。这是我的模型:

import numpy as np
import tensorflow as tf
from keras.models import Sequential, Model,load_model
from keras.layers import Dense, Dropout, Activation, Flatten, LSTM, Embedding, Input, concatenate, Lambda
from keras.utils import np_utils
from sklearn.metrics import mean_squared_error
#from keras.utils.vis_utils import plot_model
import keras
from keras_self_attention import SeqSelfAttention, SeqWeightedAttention
X1 = np.random.normal(size=(100,1,2))
X2 = np.random.normal(size=(100,1,2))
X3 = np.random.normal(size=(100,1,2))
Y = np.random.normal(size=(100,18))
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X1.shape[1],X1.shape[2])))
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=18))
model.compile(optimizer = 'adam', loss = 'mean_squared_error',metrics = ['MAE'])
model.fit(X1, Y, epochs =1, batch_size = 100)

这是模型。我想将红色部分添加到模型中。有谁能够帮助我?谢谢

【问题讨论】:

    标签: tensorflow deep-learning lstm


    【解决方案1】:

    最好使用 Functionnal API 来处理多个输入:

    def sub_model(input):
        mod1=LSTM(50,return_sequences=True)(input)
        mod1=LSTM(50,dropout=0.2)(mod1)
        return Dense(18)(mod1)
    
    inp1=Input(shape=(1,2))
    inp2=Input(shape=(1,2))
    
    mod1=sub_model(inp1)
    mod2=sub_model(inp2)
    
    concat=Concatenate()([mod1,mod2])
    output=Dense(18)(concat)
    
    model=models.Model([inp1,inp2],output)
    

    这给了你:

    要训练它,你可以使用model.fit() like :

    model.fit([X1,X2],y)
    

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多