cd_binary_binary: added gibbs sampling

This commit is contained in:
2026-01-01 20:02:22 +01:00
parent e67207504b
commit 8b97350564
3 changed files with 7 additions and 6 deletions
-1
View File
@@ -102,7 +102,6 @@ class Entity:
state = self.state.v_to_h(v)
return state
def v_given_h(self, h: Mat) -> Mat:
state = self.state.h_to_v(h)
return state
+4 -2
View File
@@ -100,8 +100,10 @@ def cd_binary_binary(entity: Entity, data_pos: Mat, params: TrainingParams):
data_neg = prob(entity.v_given_h(h_probs_pos))
h_probs_neg = prob(entity.h_given_v(data_neg))
# Gibbs sampling with training params
# ToDo
# Gibbs sampling
for _ in range(params.num_gibbs_samples-1):
data_neg = prob(entity.v_given_h(h_probs_neg))
h_probs_neg = prob(entity.h_given_v(data_neg))
# Update weights (negative phase)
dw -= np.dot(np.transpose(data_neg), h_probs_neg)
+3 -3
View File
@@ -10,7 +10,7 @@ from rbm.train import TrainingParams
class LabelLearner(Model):
def __init__(self, name: str, work_dir: str = '.'):
super().__init__(name, work_dir)
self.unit1 = Entity((16, 16), EntityParams())
self.unit1 = Entity((16, 8), EntityParams())
def forward(self, x: Mat):
x = self.unit1.forward(x)
@@ -45,7 +45,7 @@ if __name__ == "__main__":
model.load()
# Train
model.train(encoded, TrainingParams(learning_rate=0.01, momentum=0.9, do_rao_blackwell=True, num_epochs=10000))
model.train(encoded, TrainingParams(learning_rate=0.01, momentum=0.9, do_rao_blackwell=True, num_epochs=1000))
# save state
model.save()
@@ -57,7 +57,7 @@ if __name__ == "__main__":
axes[index].imshow(img)
axes[index].axis('off')
axes[index].set_title(f'{test_labels[index]}')
print(inp)
plt.show()
print("Test: [passed]")