- added train
- refactored CdTrain
This commit is contained in:
+63
-5
@@ -2,7 +2,8 @@ import numpy as np
|
||||
from collections.abc import Callable
|
||||
from params import RbmParams
|
||||
from state import RbmState
|
||||
from helper import sample, prob, uniform
|
||||
from helper import sample, prob, uniform, rms_error_accu
|
||||
from status import Status
|
||||
|
||||
class RbmLayer:
|
||||
def __init__(self, name: str, num_visible, num_hidden, params: RbmParams):
|
||||
@@ -17,11 +18,68 @@ class RbmLayer:
|
||||
def load(self):
|
||||
self.state = RbmState.from_file(self.state_filename)
|
||||
|
||||
def train_batch(self, v_states: np.ndarray, cd_func: Callable):
|
||||
for epochs in range(self.params.num_epochs):
|
||||
# Contrastive divergence learning: calculate gradients
|
||||
dwhv, dbh, dbv = cd_func(v_states)
|
||||
def train(self, batch: np.ndarray, cd_func: Callable, status: Status):
|
||||
num_cases = min(self.params.mini_batch_size, batch.shape[0])
|
||||
d_progress = 100.0 / (batch.shape[0] * self.params.num_epochs)
|
||||
last_status = status
|
||||
status.progress = 0
|
||||
batch_row_index = 0
|
||||
|
||||
keep_running = True
|
||||
|
||||
training_size_remain = batch.shape[0]
|
||||
while training_size_remain > 0 and keep_running:
|
||||
mini_batch_size = min(self.params.mini_batch_size, training_size_remain)
|
||||
mini_batch = batch[batch_row_index:batch_row_index + mini_batch_size - 1]
|
||||
training_size_remain -= mini_batch_size
|
||||
batch_row_index += mini_batch_size
|
||||
|
||||
inc_bv = np.zeros(self.state.b_v.shape)
|
||||
inc_bh = np.zeros(self.state.bh.shape)
|
||||
inc_whv = np.zeros(self.state.w_hv.shape)
|
||||
|
||||
v_states = mini_batch
|
||||
if self.params.do_batch_sample:
|
||||
v_states = sample(mini_batch)
|
||||
|
||||
for epochs in range(self.params.num_epochs):
|
||||
# Contrastive divergence learning: calculate gradients
|
||||
dwhv, dbh, dbv = cd_func(v_states, self.params, self.v_to_ph, self.h_to_pv)
|
||||
|
||||
# Adjust weight and biases
|
||||
kl = self.params.learning_rate/num_cases
|
||||
inc_bv = self.params.momentum*inc_bv + kl*dbv
|
||||
inc_bh = self.params.momentum*inc_bh + kl*dbh
|
||||
inc_whv = self.params.momentum*inc_whv + kl*dwhv - self.params.learning_rate*self.state.w_hv
|
||||
|
||||
self.state.b_v += inc_bv
|
||||
self.state.b_h += inc_bh
|
||||
self.state.w_hv += inc_whv
|
||||
|
||||
status.progress += round(d_progress*mini_batch_size)
|
||||
|
||||
if status.__dict__ != last_status.__dict__:
|
||||
# Calculate error
|
||||
status.err = rms_error_accu(mini_batch - self.h_to_pv(self.v_to_ph(v_states)))
|
||||
if not status.on_change():
|
||||
keep_running = False
|
||||
break
|
||||
|
||||
status.on_change()
|
||||
|
||||
def v_to_ph(self, v: np.ndarray) -> np.ndarray:
|
||||
state = self.state.v_to_h(v)
|
||||
if self.params.do_gaussian_visible:
|
||||
return state
|
||||
|
||||
return prob(state)
|
||||
|
||||
def h_to_pv(self, h: np.ndarray) -> np.ndarray:
|
||||
state = self.state.h_to_v(h)
|
||||
if self.params.do_gaussian_visible:
|
||||
return state
|
||||
|
||||
return prob(state)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user