Search code examples
pythontensorflowkeraskeras-layertf.keras

Printing intermediate tensors during training


I am having a custom layer and I want to print the intermediate tensors which are not linked to the returned tensor(shown in code) by call() method of custom layer. The code I used is:

class Similarity(Layer):
    
    def __init__(self, num1, num2):    
        super(Similarity, self).__init__()
        self.num1 = num1
        self.num2 = num2
#         self.total = tf.Variable(initial_value=tf.zeros((16,self.num1, 1)), trainable=False)    
        
    def build(self, input_shape):
        super(Similarity, self).build((None, self.num1, 1))
            
    
    def compute_mask(self, inputs, mask=None):
        # Just pass the received mask from previous layer, to the next layer or 
        # manipulate it if this layer changes the shape of the input
        return mask
        
    def call(self, inputs, mask=None):
        print(">>", type(inputs), inputs.shape, inputs)

        normalized = tf.nn.l2_normalize(inputs, axis = 2)
        print("norm", normalized)
        # multiply row i with row j using transpose
        # element wise product
        similarity = tf.matmul(normalized, normalized,
                         adjoint_b = True # transpose second matrix
                         )
    
        print("SIM", similarity)
        
        z=tf.linalg.band_part(similarity, 0, -1)*3 + tf.linalg.band_part(similarity, -1, 0)*2 - tf.linalg.band_part(similarity,0,0)*6 + tf.linalg.band_part(similarity,0,0)
#         z = K.print_tensor(tf.reduce_sum(z, 2, keepdims=True))
        z = tf.reduce_sum(z, 2, keepdims=True)
    
        z = tf.argsort(z)                # <----------- METHOD2: Reassigned the Z to the tensor I want to print temporarily
        z = K.print_tensor(z)
        print(z)
    
        z=tf.linalg.band_part(similarity, 0, -1)*3 + tf.linalg.band_part(similarity, -1, 0)*2 - tf.linalg.band_part(similarity,0,0)*6 + tf.linalg.band_part(similarity,0,0)

        z = K.print_tensor(tf.reduce_sum(z, 2, keepdims=True)) #<------------- THIS LINE WORKS/PRINTS AS Z is returned
        # z = tf.reduce_sum(z, 2, keepdims=True)
        
        
        @tf.function                             
                                              #<------------- METHOD1: Want to print RANKT tensor but this DID NOT WORKED
        def f(z):
            rankt = K.print_tensor(tf.argsort(z))
#             rankt = tf.reshape(rankt, (-1, self.num1))
#             rankt = K.print_tensor(rankt)
            return rankt
        
        pt = f(z)
        
        return z               # <--------- The returned tensor
    
    def compute_output_shape(self, input_shape):
        print("IS", (None, self.num1, 1))
        return (None, self.num1, 1)

To be more clear,

I used method1 in which I used @tf.function to print rankt tensor but it didn't worked.

Secondly, in method2, I reassigned z(returned tensor after call()) temporarily, so that it's executed in backprop and I get the printed values. After this I reassigned z to original opertaions

To summarize it I don't want value of z but I want to print value of some variable which is depended upon z but I am not able to print any variable other than z


Solution

  • I have searhed a lot but I couldn't find anything to print intermediate tenosors. I turns out that we could only print the tensors which are linked to the exectuted tensor (here z). So what I did was, I printed z using K.print_tensor() and then, later on, used that tensor (obviously now in list form) to perform my computation (was side computation, not to be implemented in logic)