我想使用 print 命令打印下面的调用函数中的一些对象,但当代码成功运行时它什么也打印不出来。我正在阅读(THIS)keras 调试教程,但我仍然很困惑为什么它没有打印任何内容。
#Hyperparams
learning_rate = 0.001
weight_decay = 0.0001
batch_size = 100
num_epochs = 1
image_size = 72 # We'll resize input images to this size
patch_size = 6 # Size of the patches to be extract from the input images
num_patches = (image_size // patch_size) ** 2
projection_dim = 64
num_heads = 4
transformer_units = [
projection_dim * 2,
projection_dim,
] # Size of the transformer layers
transformer_layers = 8
mlp_head_units = [2048, 1024]
我想打印下面的调用函数中的(位置和编码)。为此,我使用了打印,但它不起作用。而这里,他们就这么做了。
class PatchEncoder(layers.Layer):
def __init__(self, num_patches, projection_dim, position_embedding):
super.__init__()
self.num_patches = num_patches
self.projection = layers.Dense(units=projection_dim)
self.position_embedding = layers.Embedding(
input_dim=num_patches, output_dim=projection_dim
)
def call(self, patch):
positions = tf.range(start=0, limit=self.num_patches, delta=1)
encoded = self.projection(patch) + self.position_embedding(positions)
print("Encoded shape is:",encoded.shape)
print("pos.shape is:", positions.shape)
return encoded
对于评论中分享的代码。将其添加为导入之前的第一个代码单元。
%%bash
pip install -U tensorflow_addons
在感兴趣的日常中添加哟
class Patches(layers.Layer):
def __init__(self, patch_size):
super(Patches, self).__init__()
self.patch_size = patch_size
def call(self, images):
print("yo")
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size, self.patch_size, 1],
strides=[1, self.patch_size, self.patch_size, 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patch_dims = patches.shape[-1]
patches = tf.reshape(patches, [batch_size, -1, patch_dims])
return patches
运行用于打印示例图像的单元格,然后就会打印。
yo
Image size: 72 X 72
Patch size: 6 X 6
Patches per image: 144
Elements per patch: 108
尝试添加 tf.print() 而不仅仅是 print() 语句