- initroduced types: BB-RBM GB-RBM, GG-RBM as property of entity
- training simplification and for clarity: added specialized cd_funcs for each entity type
This commit is contained in:
+86
-3
@@ -1,5 +1,5 @@
|
||||
from collections.abc import Callable
|
||||
from .matrix import gaussian, sample, prob, rms_error_accu, Mat, np
|
||||
from .matrix import sample, sample_gaussian, prob, rms_error_accu, Mat, np
|
||||
from .entity import Entity
|
||||
from .status import Status
|
||||
|
||||
@@ -50,7 +50,7 @@ def cd_jens(entity: Entity, v_states: Mat, params: TrainingParams):
|
||||
h_probs = h_states
|
||||
|
||||
if entity.params.do_gaussian_hidden:
|
||||
h_states += gaussian(h_states.shape)
|
||||
h_states += sample_gaussian(h_states)
|
||||
else:
|
||||
h_probs = entity.forward(v_states)
|
||||
if params.do_rao_blackwell:
|
||||
@@ -83,6 +83,81 @@ def cd_jens(entity: Entity, v_states: Mat, params: TrainingParams):
|
||||
|
||||
return dw, dbv, dbh
|
||||
|
||||
def cd_binary_binary(entity: Entity, data_pos: Mat, params: TrainingParams):
|
||||
# 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 params.do_rao_blackwell:
|
||||
h_states_pos = h_probs_pos
|
||||
else:
|
||||
h_states_pos = sample(h_probs_pos)
|
||||
|
||||
# Negative phase
|
||||
data_neg = entity.v_given_h(h_states_pos)
|
||||
h_probs_neg = entity.h_given_v(data_neg)
|
||||
|
||||
# Gibbs sampling with training params
|
||||
# ToDo
|
||||
|
||||
# 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 cd_gaussian_binary(entity: Entity, data_pos: Mat, params: TrainingParams):
|
||||
# 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
|
||||
h_states_pos = sample(h_probs_pos)
|
||||
|
||||
# Negative phase
|
||||
data_neg = entity.v_given_h(h_states_pos)
|
||||
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 cd_gaussian_gaussian(entity: Entity, data_pos: Mat, params: TrainingParams):
|
||||
# Positive phase
|
||||
h_probs_pos = entity.h_given_v(data_pos)
|
||||
|
||||
# Sample hidden states
|
||||
h_states_pos = h_probs_pos + sample_gaussian(h_probs_pos)
|
||||
|
||||
# Update weights (positive phase)
|
||||
dw = np.dot(np.transpose(data_pos), h_states_pos)
|
||||
dbh = np.sum(h_states_pos, 0)
|
||||
dbv = np.sum(data_pos, 0)
|
||||
|
||||
# Negative phase
|
||||
data_neg = entity.v_given_h(h_states_pos)
|
||||
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]
|
||||
@@ -95,12 +170,20 @@ def to_mini_batch(batch: Mat, mini_batch_size: int):
|
||||
|
||||
return mini_batches
|
||||
|
||||
def train(entity: Entity, batch: Mat, params: TrainingParams, status: Status, cd_func: Callable = cd_jens):
|
||||
def train(entity: Entity, batch: Mat, params: TrainingParams, status: Status):
|
||||
mini_batch_size = min(params.mini_batch_size, batch.shape[0]) if params.mini_batch_size > 0 else batch.shape[0]
|
||||
d_progress = 100.0 / (batch.shape[0]*params.num_epochs)
|
||||
progress = 0
|
||||
keep_running = True
|
||||
status.on_change()
|
||||
cd_func = None
|
||||
if entity.type == Entity.Type.GB_RBM:
|
||||
cd_func = cd_gaussian_binary
|
||||
if entity.type == Entity.Type.BB_RBM:
|
||||
cd_func = cd_binary_binary
|
||||
if entity.type == Entity.Type.GG_RBM:
|
||||
cd_func = cd_gaussian_gaussian
|
||||
|
||||
for mini_batch in to_mini_batch(batch, mini_batch_size):
|
||||
if not keep_running:
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user