From 94595ddd8ad0c953ac194e49d96c9cafb408bc05 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 3 Jan 2026 12:10:17 +0100 Subject: [PATCH] train: added cd_binary_gaussian --- src/rbm/train.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/rbm/train.py b/src/rbm/train.py index af960f5..8cda7a4 100644 --- a/src/rbm/train.py +++ b/src/rbm/train.py @@ -42,6 +42,25 @@ def cd_jens(entity: Entity, v_states: Mat): return dw, dbv, dbh +# %%%%%%%%% START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# data = batchdata(:,:,batch); +# poshidprobs = 1./(1 + exp(-data*vishid - repmat(hidbiases,numcases,1))); +# batchposhidprobs(:,:,batch)=poshidprobs; +# posprods = data' * poshidprobs; +# poshidact = sum(poshidprobs); +# posvisact = sum(data); +# +# %%%%%%%%% END OF POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# poshidstates = poshidprobs > rand(numcases,numhid); +# +# %%%%%%%%% START NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# negdata = 1./(1 + exp(-poshidstates*vishid' - repmat(visbiases,numcases,1))); +# neghidprobs = 1./(1 + exp(-negdata*vishid - repmat(hidbiases,numcases,1))); +# negprods = negdata'*neghidprobs; +# neghidact = sum(neghidprobs); +# negvisact = sum(negdata); +# +# %%%%%%%%% END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% def cd_binary_binary(entity: Entity, data_pos: Mat): params = entity.training_params # Positive phase @@ -118,6 +137,56 @@ def cd_gaussian_gaussian(entity: Entity, data_pos: Mat): return dw, dbv, dbh +# %%%%%%%%%START POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# data = batchdata(:,:, batch); +# poshidprobs = (data * vishid) + repmat(hidbiases, numcases, 1); +# batchposhidprobs(:,:, batch)=poshidprobs; +# +# posprods = data' * poshidprobs; +# poshidact = sum(poshidprobs); +# posvisact = sum(data); +# +# %%%%%%%%%END OF POSITIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# poshidstates = poshidprobs + randn(numcases, numhid); +# +# %%%%%%%%%START NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# negdata = 1. / (1 + exp(-poshidstates * vishid' - repmat(visbiases,numcases,1))); +# neghidprobs = (negdata * vishid) + repmat(hidbiases, numcases, 1); +# negprods = negdata'*neghidprobs; +# neghidact = sum(neghidprobs); +# negvisact = sum(negdata); +# +# %%%%%%%%%END OF NEGATIVE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +def cd_binary_gaussian(entity: Entity, data_pos: Mat): + params = entity.training_params + # Positive phase + h_probs_pos = entity.h_given_v(data_pos) + + # Update weights (positive phase) + dw = np.dot(np.transpose(data_pos), h_probs_pos) + dbh = np.sum(h_probs_pos, 0) + dbv = np.sum(data_pos, 0) + + # Sample hidden states + if not params.do_rao_blackwell: + h_probs_pos += sample_gaussian(h_probs_pos) + + # Negative phase + data_neg = prob(entity.v_given_h(h_probs_pos)) + h_probs_neg = entity.h_given_v(data_neg) + + # Gibbs sampling + for _ in range(params.num_gibbs_samples-1): + data_neg = prob(entity.v_given_h(h_probs_neg)) + h_probs_neg = entity.h_given_v(data_neg) + + # Update weights (negative phase) + dw -= np.dot(np.transpose(data_neg), h_probs_neg) + dbh -= np.sum(h_probs_neg, 0) + dbv -= np.sum(data_neg, 0) + + return dw, dbv, dbh + def to_mini_batch(batch: Mat, mini_batch_size: int): mini_batches = [] remain = batch.shape[0] @@ -147,6 +216,8 @@ def train(entity: Entity, batch: Mat, status: Status): cd_func = cd_binary_binary if entity.type == Entity.Type.GG_RBM: cd_func = cd_gaussian_gaussian + if entity.type == Entity.Type.BG_RBM: + cd_func = cd_binary_gaussian for mini_batch in to_mini_batch(batch, mini_batch_size): if not keep_running: