Search code examples
pythonkerasdeep-learningtensorflow2.0tf.keras

How to build a vector which contains the last n vectors of the output of deep neural network


I am using keras, tensorflow 2.0, to build a deep neural network, and I need to build another loss function based on the last n = 4 outputs of the my model.

For example, I have the below DNN model:

import tensorflow as tf
X = Input(shape=(32,))
Y = Dense(32, activation= 'relu', kernel_initializer=ini)(X)

The output size of this model is a vector Y whose size is 32; what I need is to define another vector Z which contains the last 4 vectors of Y, for example Z = [Y_4, Y_3 , Y_2 , Y_1] which will be in this case size of 128.

I need to use that vector Z to build another loss function, so during the training, I will have the output of my DNN model Y and another vector Z which will be updated following the last coming vector Y.

I am expecting to get a vector Z with size of 128 containing the last 4 outputs of my DNN model. I fact, I couldn't start trying that process.


Solution

  • From what I understand, you want Z to be the last 4 items in your model output Y, which is then used to calculate your loss function. Then will this help?

    def my_loss_fn(y_true, y_pred):
        y_pred = y_pred[-4:, :]
        # Your code here
    
    # init the model as usual
    model = Model(...)
    model.compile(optimizer='adam', loss=my_loss_fn)