Özel keras katmanında izlenmeyen ağırlıklar nasıl bulunur?

0

Soru

Özel bir keras katmanı (bir VQVAE modeli için bir kod kitabı) oluşturmak istiyorum.) Eğitim sırasında bir tf.Variable kullanılmayan kodları yeniden başlatabilmem için her kodun kullanımını izler. Bu yüzden Kod Defteri katmanımı aşağıdaki gibi oluşturdum...

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) 
                                                                                                             

Sahip olduğum sorun şu ki Layer sınıf üye değişkeni bulur self.code_counter ve katmanla birlikte kaydedilen ağırlıklar listesine ekler. Ayrıca bekler bu self.code_counter ağırlıklar yüklendiğinde mevcut olmak, çıkarım modunda çalıştığımda durum böyle değil. Keras'ın katmanımdaki bir değişkeni izlememesi için nasıl yapabilirim. Bunun devam etmesini ya da bir parçası olmasını istemiyorum. layers.weights.

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

En iyi cevabı

1

Belgelere göre:

Bir katmanın öznitelikleri olarak ayarlanan değişkenler, katmanların ağırlıkları olarak izlenir (katmanda.kilolar)

Yani soru, kullanıp kullanamayacağınızdır tf.zeros tek başına veya birlikte 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 herhangi bir geri bildirim?
AloneTogether

Diğer dillerde

Bu sayfa diğer dillerde

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