Use both losses on a subnetwork of combined networks

Viewed 116

I am trying to stack two networks together. I want to calculate loss of each network separately. For example in the image below; loss of LSTM1 should be (Loss1 + Loss2) and loss of system should be just (Loss2)

stacked networks

I implemented a network like below with the idea above but have no idea how to compile and run it.

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

This is a wrong way of using the Model functional API of Keras. Also it's not possible to have loss of LSTM1 as Loss1+ Loss2. It will be only Loss1. Similarly for LSTM2 it will be only Loss2. However, for the combined network you can have any linear combination of Loss1 and Loss2 as the overall Loss i.e.

Loss_overall = a.Loss1 + b.Loss2. where a,b are non-negative real numbers

The real essence of Model Functional API is that it allows you create Deep learning architecture with multiple outputs and multiple inputs in single model.

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

This combined model has loss which is combination of both Loss1 and Loss2. While compiling the model you can specify the weights of each loss to get overall loss. If your desired loss is 0.5Loss1 + Loss2 you can do this by:

model_1 = build_lstm_combined()
model_1.compile(optimizer=Adam(0.001), loss = ['categorical_crossentropy','categorical_crossentropy'],loss_weights= [0.5,1])
Related