Transformers4Rec icon indicating copy to clipboard operation
Transformers4Rec copied to clipboard

[QST] Not generating labels during training after mapping to empty list

Open asos-edharris opened this issue 4 years ago • 2 comments

❓ Questions & Help

Details

Following along the example notebooks (TF), I map the training labels to an empty list, as done in the tutorial: dataloader.map(lambda X, y: (X, [])) However, when I then print out the labels during training they remain empty: Batch output (y_true) = Although, the tutorial says: The reason we set the targets to [] in the data-loader because the true item labels are computed internally by the MaskSequence class. Is there somewhere particular that might be causing this issue that you could suggest?

Thanks!

asos-edharris avatar Dec 21 '21 14:12 asos-edharris

Thank you for your question @asos-edharris! Could you please share the code used to print out the labels during training?

To give more context to the tutorial statement: The labels are computed from the input sequence of item-ids and stored in the MaskSequence class as masked_targets. Then, they are retrieved in NextItemPredictionTask when computing the loss (here ) or the metrics (here)

sararb avatar Dec 22 '21 11:12 sararb

Thanks very much for getting back to me so quickly @sararb.

I printed out the labels during training using the keras.fit class as shown below:

def make_print_data_and_train_step(keras_model):
    original_train_step = keras_model.train_step

    def print_data_and_train_step(original_data):
        data = data_adapter.expand_1d(original_data)
        x, y_true, w = data_adapter.unpack_x_y_sample_weight(data)
        y_pred = keras_model(x, training=True)
        tf.print(y_true, "Batch output (y_true) =")
        K.print_tensor(y_pred, "Prediction (y_pred) =")

        result = original_train_step(original_data)
        
        return result

    return print_data_and_train_step

model.train_step = make_print_data_and_train_step(model)

However, this just returns an empty list which is not what I would expect.

Following your response I have printed out both targets and self.masking.masked_targets in calculate_metrics function in the NextItemPredictionTask class during training.

Printing the self.masking.masked_targets returns (what I think is) the desired output:

<tf.Variable 'Variable:0' shape=(None, None) dtype=int32, numpy=
array([[    0,     0,     0, ...,     0,     0,  6350],
       [    0,     0,     0, ...,     0,  9741,     0],
       [    0,     0,     0, ...,     0,     0, 14441],
       ...,
       [    0,     0,     0, ...,     0, 95265,     0],
       [    0,     0,     0, ...,   976,     0,     0],
       [    0,     0,     0, ...,     0,  1061,  1054]], dtype=int32)>

However, printing the target variable does not give a desired output:

tf.Tensor([  6583   6088   9741  29351  14441   9423 112128 104365  44347 101054
55510  57487  19500  20601  90032  35184  86980  86084  86709    477
  63362 ...], shape=(395,), dtype=int32)

EDIT: since looking more into the compute_loss function, it is apparent that this flattened tf.Tensor is the correct output for targets as it is a transformed version of self.masking.masked_targets. Hence my confusion now only refers to the empty labels printed when accessing the labels through keras.fit.

Hence my question is:

  • Am I to expect an empty list as an output when printing the labels through the keras.fit function or should the labels in keras.fit be set to the targets variable used in the NextItemPredictionTask class?

(the model that I am building here is the TF version of the "3.2. Training a Transformer-based Session-based Recommendation Model" in the "End-to-end Session-based recommendation" tutorial - https://nvidia-merlin.github.io/Transformers4Rec/main/examples/tutorial/03-Session-based-recsys.html)

Thanks in advance for your help!

asos-edharris avatar Dec 22 '21 16:12 asos-edharris