合并两个TensorFlow子模型

huangapple go评论60阅读模式
英文:

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: 合并两个TensorFlow子模型

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])

huangapple
  • 本文由 发表于 2023年5月11日 01:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定