Я пытаюсь соединить две модели вместе. У меня есть common_model ииндивидуальная_модель, которые я сначала создаю индивидуально. Позже я хочу соединить выходные данные common_model с входными данными заполнителя отдельной модели.
Однако я не могу с этим справиться, и кажется, что выходные данные common_model всегда подключен к индивидуальному_входу. Может кто-нибудь указать на мою ошибку?
from tensorflow.keras.layers import Input, LSTM, Dense, BatchNormalization, Concatenate, Attention
from tensorflow.keras.models import Model
from tensorflow.keras.regularizers import l2
class MyModel:
def __init__(self, timesteps, features, horizon):
self.timesteps = timesteps
self.features = features
self.horizon = horizon
self.common_model = None # Placeholder for the common model
def create_common_model(self):
# Define the common model
common_inputs = Input(shape=(self.timesteps, self.features), name="common_inputs")
common_dense = Dense(64, activation="relu", kernel_regularizer=l2(0.001), name="common_dense")(common_inputs)
self.common_model = Model(inputs=common_inputs, outputs=common_dense, name="Common_Model")
def create_individual_model(self):
# Define the individual model with a placeholder for the common model's output
individual_inputs = Input(shape=(self.timesteps, self.features), name="individual_inputs")
individual_dense = Dense(32, activation="relu", kernel_regularizer=l2(0.001), name="individual_dense")(individual_inputs)
# Concatenate with the placeholder
common_placeholder = Input(shape=self.common_model.output.shape[1:], name="common_placeholder") # Adjust shape if needed
merged = Concatenate(name="concat_layer")([individual_dense, common_placeholder])
output = Dense(self.horizon, activation="linear", name="final_output")(merged)
# Create the individual model
individual_model = Model(inputs=[individual_inputs, common_placeholder], outputs=output, name="Individual_Model")
return individual_model
def create_full_model(self):
# Ensure the common model exists
if not self.common_model:
self.create_common_model()
# Get the individual model
individual_model = self.create_individual_model()
# Replace the common placeholder with the common model's output
full_output = individual_model([individual_model.input[0], self.common_model.output])
# Construct the full model
full_model = Model(inputs=[individual_model.input[0], self.common_model.input], outputs=full_output, name="Full_Model")
return full_model
new_mo = MyModel(timesteps, features, horizon)
f = new_mo.create_full_model()
По сути, я хочу получить это изображение, но я хочу иметь две совершенно отдельные модели.
Модель
Я пытаюсь соединить две модели вместе. У меня есть common_model ииндивидуальная_модель, которые я сначала создаю индивидуально. Позже я хочу соединить выходные данные common_model с входными данными заполнителя отдельной модели. Однако я не могу с этим справиться, и кажется, что выходные данные common_model всегда подключен к индивидуальному_входу. Может кто-нибудь указать на мою ошибку? [code]from tensorflow.keras.layers import Input, LSTM, Dense, BatchNormalization, Concatenate, Attention from tensorflow.keras.models import Model from tensorflow.keras.regularizers import l2
class MyModel: def __init__(self, timesteps, features, horizon): self.timesteps = timesteps self.features = features self.horizon = horizon self.common_model = None # Placeholder for the common model
def create_common_model(self): # Define the common model common_inputs = Input(shape=(self.timesteps, self.features), name="common_inputs") common_dense = Dense(64, activation="relu", kernel_regularizer=l2(0.001), name="common_dense")(common_inputs)
def create_individual_model(self): # Define the individual model with a placeholder for the common model's output individual_inputs = Input(shape=(self.timesteps, self.features), name="individual_inputs") individual_dense = Dense(32, activation="relu", kernel_regularizer=l2(0.001), name="individual_dense")(individual_inputs)
# Concatenate with the placeholder common_placeholder = Input(shape=self.common_model.output.shape[1:], name="common_placeholder") # Adjust shape if needed
# Create the individual model individual_model = Model(inputs=[individual_inputs, common_placeholder], outputs=output, name="Individual_Model") return individual_model
def create_full_model(self): # Ensure the common model exists if not self.common_model: self.create_common_model()
# Get the individual model individual_model = self.create_individual_model()
# Replace the common placeholder with the common model's output full_output = individual_model([individual_model.input[0], self.common_model.output])
# Construct the full model full_model = Model(inputs=[individual_model.input[0], self.common_model.input], outputs=full_output, name="Full_Model") return full_model
new_mo = MyModel(timesteps, features, horizon)
f = new_mo.create_full_model() [/code] По сути, я хочу получить это изображение, но я хочу иметь две совершенно отдельные модели. Модель