ฉันต้องการจะสร้างกำหนดเอง 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
.