在训练期间打印中间张量

Printing intermediate tensors during training

我有一个自定义层,我想通过自定义层的 call() 方法打印未链接到返回张量(代码中显示)的中间张量。我使用的代码是:

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)

说得更清楚,

我使用了 method1,其中我使用了 @tf.function 来打印 rankt 张量,但没有成功。

其次,在method2中,我临时重新分配了z(在call()之后返回的张量),以便它在backprop中执行并得到打印值。在此之后,我将 z 重新分配给原始操作

总结一下,我不想要 z 的值,但我想打印一些依赖于 z 的变量的值,但我无法打印z

以外的任何变量

tf.print这个功能。

在 eager 模式下,它 returns 什么都不做,只打印张量。在计算图构建过程中使用时,returns TF 运算符将张量值标识并打印为 side-effect.

我搜索了很多,但我找不到任何东西来打印中间 tenosors。我发现我们只能打印与执行张量相关联的张量(此处 z)。所以我所做的是,我使用 K.print_tensor() 打印 z 然后,稍后,使用该张量(显然现在是列表形式)来执行我的计算(是边计算,而不是在逻辑中实现)