- Status has entity paarmeter

- train provides entity parameter to Status
This commit is contained in:
2026-01-06 11:29:47 +01:00
parent 42520e5761
commit ff6254a15d
3 changed files with 13 additions and 10 deletions
+1
View File
@@ -76,6 +76,7 @@ class Entity:
self.enable_training = enable_training self.enable_training = enable_training
self.state = RbmState.from_layer_params(shape) self.state = RbmState.from_layer_params(shape)
self.grad = RbmState.from_layer_params(shape) self.grad = RbmState.from_layer_params(shape)
self.name = f"Entity-{shape[0]}x{shape[1]}"
self.type = None self.type = None
if params.do_gaussian_visible: if params.do_gaussian_visible:
if params.do_gaussian_hidden: if params.do_gaussian_hidden:
+9 -7
View File
@@ -1,25 +1,27 @@
from .entity import Entity
class Status: class Status:
def __init__(self, update_interval=10): def __init__(self, update_interval=10):
self.progress = -1 self.progress = -1
self.update_interval = update_interval self.update_interval = update_interval
@staticmethod @staticmethod
def print_status(status: dict): def print_status(entity: Entity, status: dict):
print("-------------------------------------------") print("-------------------------------------------")
for key in status.keys(): for key in status.keys():
value = status[key]['value'] value = status[key]['value']
unit = status[key]['unit'] unit = status[key]['unit']
if isinstance(value, float): if isinstance(value, float):
print(f"{key} : {value:0.6f}{unit}") print(f"{entity.name}: {key} : {value:0.6f}{unit}")
else: else:
print(f"{key} : {value}{unit}") print(f"{entity.name}: {key} : {value}{unit}")
def on_change(self, status: dict|None=None) -> bool: def on_change(self, entity: Entity, status: dict|None=None) -> bool:
if status is None: if status is None:
self.progress = -1 self.progress = -1
return True return True
do_continue = self.on_report(status=status) do_continue = self.on_report(entity, status=status)
return do_continue return do_continue
def want_report(self, progress) -> bool: def want_report(self, progress) -> bool:
@@ -28,6 +30,6 @@ class Status:
return True return True
return False return False
def on_report(self, status: dict) -> bool: def on_report(self, entity: Entity, status: dict) -> bool:
Status.print_status(status) Status.print_status(entity, status)
return True return True
+3 -3
View File
@@ -203,7 +203,7 @@ def train(entity: Entity, batch: Mat, status: Status):
d_progress = 100.0 / (batch.shape[0]*params.num_epochs) d_progress = 100.0 / (batch.shape[0]*params.num_epochs)
progress = 0 progress = 0
keep_running = True keep_running = True
status.on_change() status.on_change(entity)
cd_func = None cd_func = None
if entity.type == Entity.Type.GB_RBM: if entity.type == Entity.Type.GB_RBM:
cd_func = cd_gaussian_binary cd_func = cd_gaussian_binary
@@ -232,7 +232,7 @@ def train(entity: Entity, batch: Mat, status: Status):
if status.want_report(round(progress)): if status.want_report(round(progress)):
# Calculate error # Calculate error
err_rms = rms_error_accu(batch - entity.reconstruct(entity.forward(batch))) err_rms = rms_error_accu(batch - entity.reconstruct(entity.forward(batch)))
if not status.on_change({"progress": {"value": round(progress), "unit": "%"}, if not status.on_change(entity, {"progress": {"value": round(progress), "unit": "%"},
"err_rms": {"value": err_rms, "unit": ""}}): "err_rms": {"value": err_rms, "unit": ""}}):
keep_running = False keep_running = False
break break
@@ -241,7 +241,7 @@ def train(entity: Entity, batch: Mat, status: Status):
# Update final status # Update final status
err_rms = rms_error_accu(batch - entity.reconstruct(entity.forward(batch))) err_rms = rms_error_accu(batch - entity.reconstruct(entity.forward(batch)))
status.on_change({"progress": {"value": round(progress), "unit": "%"}, status.on_change(entity, {"progress": {"value": round(progress), "unit": "%"},
"err_rms_total": {"value": err_rms, "unit": ""}}) "err_rms_total": {"value": err_rms, "unit": ""}})
return True return True