วิธีที่จะต้องช่วยตามรอยกเวทอยู่ในกำหนดเอง keras เลเยอร์?

0

คำถาม

ฉันต้องการจะสร้างกำหนดเอง keras เลเยอร์(เป็น codebook สำหรับ VQVAE นคุณครูแต่พอกลับมา) ขณะที่การฝึกฉันอยากจะมี tf.Variable ซึ่งร่องรอยการใช้ของแต่ละคนรหัสดังนั้นฉันสามารถเริ่มการไม่ใช้รหัส ดังนั้นฉันสร้างของฉัน Codebook เลเยอร์เป็นตาม...

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 ต้องเป็นของขวัญตอนที่กเวทเป็นโหลดซึ่งไม่ใช่กระเป๋าตอนที่ฉันวิ่งอยู่ใน inference ไม่มีเวลาคุยกับเธอ ผมต้องทำยังไงให้มันดังนั้น keras ไม่ได้ตามหาตัวแปรในของฉันเลเยอร์. ฉันไม่ต้องการมัน persisted หรือจะเป็นส่วนหนึ่งของ layers.weights.

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

คำตอบที่ดีที่สุด

1

ตามที่ docs:

ตัวแปรตั้งเป็นคุณลักษณะของชิ้นออกเป็นตามรอยที่กเวทของเสื้อผ้าหลายชั้น(อยู่ในเลเยอร์.กเวท)

ดังนั้นคำถามคือคุณสามารถใช้ได้ 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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่