significantly improved GB-RBM training
This commit is contained in:
+9
-27
@@ -93,60 +93,42 @@ def cd_binary_binary(entity: Entity, data_pos: Mat):
|
||||
return dw, dbv, dbh
|
||||
|
||||
def cd_gaussian_binary(entity: Entity, data_pos: Mat):
|
||||
params = entity.training_params
|
||||
|
||||
# Positive phase
|
||||
if params.do_batch_sample:
|
||||
h_probs_pos = entity.h_given_v(data_pos + sample_gaussian(data_pos))
|
||||
else:
|
||||
h_probs_pos = entity.h_given_v(data_pos)
|
||||
h_probs_pos = prob(entity.h_given_v(data_pos + sample_gaussian(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)
|
||||
|
||||
if not params.do_rao_blackwell:
|
||||
# Sample hidden states
|
||||
h_probs_pos = sample(h_probs_pos)
|
||||
dbv = np.sum(data_pos-entity.state.b_v, 0)
|
||||
|
||||
# Negative phase
|
||||
data_neg = entity.v_given_h(h_probs_pos)
|
||||
h_probs_neg = entity.h_given_v(data_neg)
|
||||
h_probs_neg = prob(entity.h_given_v(data_neg + sample_gaussian(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)
|
||||
dbv -= np.sum(data_neg-entity.state.b_v, 0)
|
||||
|
||||
return dw, dbv, dbh
|
||||
|
||||
def cd_gaussian_gaussian(entity: Entity, data_pos: Mat):
|
||||
params = entity.training_params
|
||||
|
||||
# Positive phase
|
||||
if params.do_batch_sample:
|
||||
h_probs_pos = entity.h_given_v(data_pos + sample_gaussian(data_pos))
|
||||
else:
|
||||
h_probs_pos = entity.h_given_v(data_pos)
|
||||
|
||||
if not params.do_rao_blackwell:
|
||||
# Sample hidden states
|
||||
h_probs_pos += sample_gaussian(h_probs_pos)
|
||||
h_probs_pos = entity.h_given_v(data_pos)
|
||||
|
||||
# Update weights (positive phase)
|
||||
dw = np.dot(np.transpose(data_pos), h_probs_pos)
|
||||
dw = np.dot(np.transpose(data_pos), h_probs_pos + sample_gaussian(h_probs_pos))
|
||||
dbh = np.sum(h_probs_pos, 0)
|
||||
dbv = np.sum(data_pos, 0)
|
||||
dbv = np.sum(data_pos-entity.state.b_v, 0)
|
||||
|
||||
# Negative phase
|
||||
data_neg = entity.v_given_h(h_probs_pos)
|
||||
h_probs_neg = entity.h_given_v(data_neg)
|
||||
h_probs_neg = entity.h_given_v(data_neg + sample_gaussian(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)
|
||||
dbv -= np.sum(data_neg-entity.state.b_v, 0)
|
||||
|
||||
return dw, dbv, dbh
|
||||
|
||||
|
||||
Reference in New Issue
Block a user