如何有联合国进行重在定义keras层?

0

的问题

我想创建一个自定义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.

keras python tensorflow
2021-11-23 10:45:03
1

最好的答案

1

根据该 文件:

变量设定为属性的一层进行跟踪,作为砝码的层(在层。重量)

因此,问题是是否可以使用 tf.zeros 单独或一起 tf.constant:

import tensorflow as tf

class Codebook(tf.keras.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.constant(tf.zeros(num_codes, dtype = tf.int32))

     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) 
code_book = Codebook(num_codes=5, code_reset_limit=True)
print(code_book.weights)
[]
2021-11-23 13:35:05

@chasep255任何意见?
AloneTogether

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................