我正在尝试将两个网络堆叠在一起。我想分别计算每个网络的损失。例如下图; LSTM1的损失应为(Loss1 + Loss2),系统的损失应仅为(Loss2)
我用上述想法实现了如下所示的网络,但不知道如何编译和运行它。
def build_lstm1():
x = Input(shape=(self.timesteps, self.input_dim,), name = 'input')
h = LSTM(1024, return_sequences=True))(x)
scores = TimeDistributed(Dense(self.input_dim, activation='sigmoid', name='dense'))(h)
LSTM1 = Model(x, scores)
return LSTM1
def build_lstm2():
x = Input(shape=(self.timesteps, self.input_dim,), name = 'input')
h = LSTM(1024, return_sequences=True))(x)
labels = TimeDistributed(Dense(self.input_dim, activation='sigmoid', name='dense'))(h)
LSTM2 = Model(x, labels)
return LSTM2
lstm1 = build_lstm1()
lstm2 = build_lstm2()
combined = Model(inputs = lstm1.input ,
outputs = [lstm1.output,
lstm2(lstm1.output).output)])
答案 0 :(得分:0)
这是使用Keras的Model功能API的错误方法。同样,不可能将LSTM1丢失为Loss1 + Loss2。只会是Loss1。同样,对于LSTM2,它只会是Loss2。但是,对于合并后的网络,您可以将Loss1和Loss2的任何线性组合作为总损失,即
Loss_overall = a。损失1 + b。损失2。其中a,b为非负实数
模型功能API的真正本质在于,它允许您在单个模型中创建具有多个输出和多个输入的深度学习架构。
def build_lstm_combined():
x = Input(shape=(self.timesteps, self.input_dim,), name = 'input')
h_1 = LSTM(1024, return_sequences=True))(x)
scores = TimeDistributed(Dense(self.input_dim, activation='sigmoid', name='dense'))(h_1)
h_2 = LSTM(1024, return_sequences=True))(h_1)
labels = TimeDistributed(Dense(self.input_dim, activation='sigmoid', name='dense'))(h_2)
LSTM_combined = Model(x,[scores,labels])
return LSTM_combined
此组合模型的损失为Loss1和Loss2的组合。编译模型时,您可以指定每个损失的权重以获得总损失。如果您希望的损失为0.5Loss1 + Loss2,则可以通过以下方式进行操作:
model_1 = build_lstm_combined()
model_1.compile(optimizer=Adam(0.001), loss = ['categorical_crossentropy','categorical_crossentropy'],loss_weights= [0.5,1])