【问题标题】:Printing intermediate tensors during training在训练期间打印中间张量
【发布时间】:2020-10-14 07:32:04
【问题描述】:

我有一个自定义层,我想通过自定义层的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 中,我临时重新分配了zcall() 之后的返回张量),以便它在backprop 中执行并得到打印的值。在此之后,我将 z 重新分配给原始操作

总而言之,我不想要z 的值,但我想打印一些依赖于z 的变量的值,但我无法打印除z 之外的任何变量

【问题讨论】:

    标签: python tensorflow keras keras-layer tf.keras


    【解决方案1】:

    这里有tf.print 函数。

    在急切模式下,它什么也不返回,只打印张量。在计算图构建过程中使用时,它会返回 TF 运算符,这些运算符执行标识并打印张量值作为副作用。

    【讨论】:

    • 如何在训练期间以 Eager 模式运行 tf.print?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2018-06-22
      • 2016-11-12
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      相关资源
      最近更新 更多