는 방법을 자유롭게 할 수 있는 추적 가중치를 사용자 지정 keras 층?

0

질문

나를 만들고 싶은 사용자 지정 keras 레이어(코드북에 대한 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ý
..................................................................................................................