train: added cd_binary_gaussian
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user