我有一个自定义层,我想打印没有通过自定义层的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
张量,但它不起作用
其次,在method2
中,我临时重新分配了z
(在call()
之后返回张量),以便它在backprop
中执行,并获得打印的值。在此之后,我将z
重新分配给原始操作
总而言之,我不想要z
的值,但我想打印某个变量的值,该值取决于z
,但除了z
之外,我无法打印任何变量
这个函数有^{} 函数
在急切模式下,它不返回任何内容,只打印张量。当在计算图构建过程中使用时,它返回TF运算符,这些运算符进行标识并打印张量值作为副作用
我已经烧焦了很多,但我找不到任何东西来印刷中间期债券。我发现我们只能打印链接到执行的张量的张量(这里^{)。所以我所做的是,我使用
K.print_tensor()
打印z
,然后,稍后,使用那个张量(显然现在是列表形式)来执行我的计算(是边计算,不是逻辑实现)相关问题 更多 >
编程相关推荐