0

I wish to exam the values of a tensor after mask is applied to it.

Here is a truncated part of the model. I let temp = x so later I wish to print temp to check the exact values.

So given a 4-class classification model using acoustic features. Assume I have data in (1000,50,136) as (batch, timesteps, features)

The objective is to check if the model is studying the features by timesteps. In other words, we wish to reassure the model is learning using slice as the red rectangle in the picture. Logically, it is the way for Keras LSTM layer but the confusion matrix produced is quite different when a parameter changes (eg. Dense units). The validation accuracy stays 45% thus we would like to visualize the model.

The proposed idea is to print out the first step of the first batch and print out the input in the model. If they are the same, then model is learning in the right way ((136,1) features once) instead of (50,1) timesteps of a single feature once.

enter image description here

input_feature = Input(shape=(X_train.shape[1],X_train.shape[2]))
x = Masking(mask_value=0)(input_feature)
temp = x
x = Dense(Dense_unit,kernel_regularizer=l2(dense_reg), activation='relu')(x)
        

I have tried tf.print() which brought me AttributeError: 'Tensor' object has no attribute '_datatype_enum'


As Get output from a non final keras model layer suggested by Lescurel.

model2 = Model(inputs=[input_attention, input_feature], outputs=model.get_layer('masking')).output
print(model2.predict(X_test))

AttributeError: 'Masking' object has no attribute 'op'
Leo
  • 153
  • 2
  • 19
  • Does this answer your question? [Get output from a non final keras model layer](https://stackoverflow.com/questions/51949208/get-output-from-a-non-final-keras-model-layer) – Lescurel Feb 02 '21 at 09:26
  • @Lescurel Thanks, I tried the method but an AttributeError popped up. – Leo Feb 02 '21 at 09:48
  • You have a typo, should be `model2 = Model(inputs=[input_attention, input_feature], outputs=model.get_layer('masking').output)` – Lescurel Feb 02 '21 at 09:57
  • Based on your code, I would guess that you want to pass a dense layer an input with deleted masked values. This is impossible because of two reasons. First, dense layer must have fixed input size. Second, masking layer does not change the input in any way, it only attaches the corrsponding mask (see https://www.tensorflow.org/guide/keras/masking_and_padding#masking). Please, specify your global goal as well. – Ivan K. Feb 02 '21 at 11:12

1 Answers1

0

You want to output after mask. lescurel's link in the comment shows how to do that. This link to github, too.

You need to make a new model that

  • takes as inputs the input from your model
  • takes as outputs the output from the layer

I tested it with some made-up code derived from your snippets.

import numpy as np
from keras import Input
from keras.layers import Masking, Dense
from keras.regularizers import l2
from keras.models import Sequential, Model
X_train = np.random.rand(4,3,2)
Dense_unit = 1
dense_reg = 0.01
mdl = Sequential()
mdl.add(Input(shape=(X_train.shape[1],X_train.shape[2]),name='input_feature'))
mdl.add(Masking(mask_value=0,name='masking'))
mdl.add(Dense(Dense_unit,kernel_regularizer=l2(dense_reg),activation='relu',name='output_feature'))
mdl.summary()
mdl2mask = Model(inputs=mdl.input,outputs=mdl.get_layer("masking").output)
maskoutput = mdl2mask.predict(X_train)
mdloutput = mdl.predict(X_train)
maskoutput # print output after/of masking
mdloutput # print output of mdl
maskoutput.shape #(4, 3, 2): masking has the shape of the layer before (input here)
mdloutput.shape #(4, 3, 1): shape of the output of dense
Roland Puntaier
  • 3,250
  • 30
  • 35
  • Masking does not change the input tensor in any way, it only creates the associated mask. In the example at https://www.tensorflow.org/guide/keras/masking_and_padding#masking, if you call print(masked_embedding) instead of print(masked_embedding._keras_mask) in the "Mask-generating layers: Embedding and Masking section", you will get the initial tensor. Moreover, dense layer does not use masks. What one can do is to check the mask (via ._keras_mask), not the tenosr. Am I missing something? If not, then the question is incomplete or incorrect. – Ivan K. Feb 02 '21 at 17:49
  • The [Masking class](https://github.com/keras-team/keras/blob/master/keras/layers/core.py#:~:text=compute_mask) has a `call()` like all layers. The layer's `output_shape` is provided by `compute_output_shape()`. `Embedding` is not the only possible use of the `Masking` layer. The question is incomplete. – Roland Puntaier Feb 02 '21 at 20:56
  • Thank you guys, I have updated the question so my objective is more clear now. – Leo Feb 03 '21 at 01:44