我想建立一个简单的模型与图中相同。我为每一个都有一个子模型,在子模型中我有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)
1条答案
按热度按时间kxeu7u2r1#
你需要使用
keras.Input
层,你可以用下面的方法来连接: