我想创建一个自定义keras层(a码本一VQVAE的模型。) 在训练的同时,我希望有一个 tf.Variable
其轨道的使用情况的每一个码所以我可以重新启动未使用的代码。 所以我创造了我的码本层如下...
class Codebook(layers.Layer):
def __init__(self, num_codes, code_reset_limit = None, **kwargs):
super().__init__(**kwargs)
self.num_codes = num_codes
self.code_reset_limit = code_reset_limit
if self.code_reset_limit:
self.code_counter = tf.Variable(tf.zeros(num_codes, dtype = tf.int32), trainable = False)
def build(self, input_shape):
self.codes = self.add_weight(name = 'codes',
shape = (self.num_codes, input_shape[-1]),
initializer = 'random_uniform',
trainable = True)
super().build(input_shape)
问题是 Layer
类找到成员的变量 self.code_counter
并将它添加到名单的权重,这是保存与层。 它还希望 self.code_counter
是时存在的权都载入其中是没有的情况下,当我运行中的推理方式。 我怎么能让这keras不轨道的一个变量在我的层。 我不想要它坚持或是一部分 layers.weights
.