英文:
Concate two submodel in tensorflow
问题
Here is the translated code portion:
我想构建一个与图中相同的简单模型。我为每个子模型都有一个,而在子模型中,我有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.compile(optimizer='adam', loss='mean_squared_error', metrics=['MAE'])
history = model.fit(X_train, Y_train, epochs=2, batch_size=100)
Please note that I've translated the code part as per your request. If you have any specific questions or need further assistance, feel free to ask.
英文:
I want to build a simple model same as the figure. I have a submodel for each one and in the submodel I have the lstm. However, I dont know how exactly I should define the input and the out put. Here is a simple code of my problem, and the submodels are exactly same as each other here. Could you help me how to run this model? Thank you. Here is the model that I want to create:
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
得分: 1
你需要使用 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)
# 检查输出大小
model([X_train1, X_train2]).shape
# TensorShape([10, 28])
英文:
You need to use keras.Input
layers and you can concat the following way:
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])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论