43 lines
948 B
Python
43 lines
948 B
Python
from .params import EntityParams
|
|
from .state import RbmState
|
|
from .matrix import prob, Mat
|
|
|
|
class Entity:
|
|
def __init__(self, shape: tuple[int, int], params: EntityParams):
|
|
self.shape = shape
|
|
self.params = params
|
|
self.state = RbmState.from_layer_params(shape)
|
|
|
|
def v_to_ph(self, v: Mat) -> Mat:
|
|
state = self.state.v_to_h(v)
|
|
if self.params.do_gaussian_hidden:
|
|
return state
|
|
|
|
return prob(state)
|
|
|
|
def h_to_pv(self, h: Mat) -> Mat:
|
|
state = self.state.h_to_v(h)
|
|
if self.params.do_gaussian_visible:
|
|
return state
|
|
|
|
return prob(state)
|
|
|
|
def gibbs_v_to_h(self, v: Mat) -> Mat:
|
|
h = self.v_to_ph(v)
|
|
for i in range(self.params.num_gibbs_samples-1):
|
|
h = self.h_to_pv(h)
|
|
h = self.v_to_ph(h)
|
|
|
|
return h
|
|
|
|
def gibbs_h_to_v(self, h: Mat) -> Mat:
|
|
v = self.h_to_pv(h)
|
|
for i in range(self.params.num_gibbs_samples-1):
|
|
v = self.v_to_ph(v)
|
|
v = self.h_to_pv(v)
|
|
|
|
return v
|
|
|
|
if __name__ == "__main__":
|
|
print("Test: [passed]")
|