tensorflow 中的共轭两个子模型

aiqt4smr  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(116)

我想建立一个简单的模型与图中相同。我为每一个都有一个子模型,在子模型中我有lstm。然而,我不知道我应该如何确切地定义输入和输出。下面是我的问题的一个简单代码,子模型在这里彼此完全相同。你能告诉我如何运行这个模型吗?谢谢。下面是我想创建的模型:

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,  Input, concatenate
import keras

X_train1 = np.random.randint(10, size = (10, 20, 28))
Y_train1= np.random.randint(10, size = (10, 28))

X_train2 = np.random.randint(10, size = (10, 10, 28))
Y_train2= np.random.randint(10, size = (10, 28))

def sub_model1(X_train, Y_train):  
    model = Sequential()
    model.add(LSTM(100, activation='linear', input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=True))
    model.add(LSTM(32, activation='linear', return_sequences=False))
    model.add(Dense(100, activation='linear'))
    return model.add(Dense(Y_train.shape[1], activation='linear'))

model1 = sub_model1(X_train1, Y_train1)
model2 = sub_model1(X_train2, Y_train2)

concat   = concatenate([model1, model2])

output   = Dense(28, activation="linear")(concat)

#model    =  Model(inputs = INPUT, outputs = output)
#how to define that?

model.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['MAE'])

history  = model.fit(X_train, Y_train, epochs =2, batch_size = 100)
kxeu7u2r

kxeu7u2r1#

你需要使用keras.Input层,你可以用下面的方法来连接:

def sub_model1(x, n_dim=28):  

 x= LSTM(100, activation='linear', return_sequences=True)(x)
 x= LSTM(32, activation='linear', return_sequences=False)(x)
 x= Dense(100, activation='linear')(x)
 return Dense(n_dim, activation='linear')(x)

input_1 = tf.keras.layers.Input([20, 28], dtype=tf.float32, name='input_1')
input_2 = tf.keras.layers.Input([10, 28], dtype=tf.float32, name='input_2')
x1 = sub_model1(input_1)
x2 = sub_model1(input_2)

concat   = concatenate([x1, x2])

output   = Dense(28, activation="linear")(concat)

model = tf.keras.models.Model(inputs=[input_1, input_2], outputs=output)

#check output size
model([X_train1, X_train2]).shape
#TensorShape([10, 28])

相关问题