fixed wrong number of bits for binary encoding

This commit is contained in:
2026-06-05 11:11:32 +02:00
parent 49a99dc750
commit b20ea4edb6
+5 -5
View File
@@ -13,7 +13,7 @@ class Label:
class Fitter(Model):
def __init__(self, dim: tuple[int,int], work_dir: str = '.'):
super().__init__(f"label-{dim[0]}x{dim[1]}", work_dir)
self.unit1 = Entity(dim, EntityParams(), TrainingParams(num_epochs=5000, do_rao_blackwell=False, momentum=0.9))
self.unit1 = Entity(dim, EntityParams(), TrainingParams(num_epochs=5000, do_rao_blackwell=True, momentum=0.9))
def forward(self, x: Mat) -> Mat:
return self.unit1.forward(x)
@@ -26,7 +26,7 @@ class Label:
if encoding == Label.EncodingType.Binary:
self.label2vec = self.label2vec_binary
self.vec2label = self.vec2label_binary
self.fitter = Label.Fitter((round(math.log(voc_size,2)), num_dim), work_dir)
self.fitter = Label.Fitter((math.ceil(math.log(voc_size,2)), num_dim), work_dir)
elif encoding == Label.EncodingType.OneHot:
self.label2vec = self.label2vec_onehot
self.vec2label = self.vec2label_onehot
@@ -46,7 +46,7 @@ class Label:
return dec_data
def label2vec_binary(self, list_of_labels: np.array):
num_bits = round(math.log(self.voc_size,2))
num_bits = math.ceil(math.log(self.voc_size,2))
batch = np.zeros((len(list_of_labels), num_bits))
for index, label in enumerate(list_of_labels):
b = self._enc_binary(label, num_bits)
@@ -87,10 +87,10 @@ class Label:
return np.argmax(label_vecs, axis=-1)
if __name__ == "__main__":
voc_size = 100
voc_size = 10000
# Create Labeler
enc = Label(voc_size, 16, work_dir="../../results", encoding=Label.EncodingType.OneHot)
enc = Label(voc_size, 16, work_dir="../../results", encoding=Label.EncodingType.Binary)
# Load
enc.fitter.init(0.1)