- 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:
+29
-1
@@ -1,6 +1,6 @@
|
||||
from .state import RbmState
|
||||
from .matrix import prob, Mat
|
||||
|
||||
from enum import Enum
|
||||
class EntityParams:
|
||||
def __init__(self, do_gaussian_visible: bool = False, do_gaussian_hidden: bool = False, num_gibbs_samples: int = 1):
|
||||
# Entity parameters
|
||||
@@ -21,11 +21,29 @@ class EntityParams:
|
||||
return obj
|
||||
|
||||
class Entity:
|
||||
class Type(Enum):
|
||||
BB_RBM = "BB-RBM"
|
||||
BG_RBM = "BG-RBM"
|
||||
GB_RBM = "GB-RBM"
|
||||
GG_RBM = "GG-RBM"
|
||||
|
||||
def __init__(self, shape: tuple[int, int], params: EntityParams):
|
||||
self.shape = shape
|
||||
self.params = params
|
||||
self.state = RbmState.from_layer_params(shape)
|
||||
self.grad = RbmState.from_layer_params(shape)
|
||||
self.type = None
|
||||
if params.do_gaussian_visible:
|
||||
if params.do_gaussian_hidden:
|
||||
self.type = Entity.Type.GG_RBM
|
||||
else:
|
||||
self.type = Entity.Type.GB_RBM
|
||||
else:
|
||||
if params.do_gaussian_hidden:
|
||||
self.type = Entity.Type.BG_RBM
|
||||
else:
|
||||
self.type = Entity.Type.BB_RBM
|
||||
|
||||
|
||||
def __call__(self, x: Mat):
|
||||
return self.forward(x)
|
||||
@@ -72,6 +90,7 @@ class Entity:
|
||||
|
||||
return prob(state)
|
||||
|
||||
|
||||
def _h_to_pv(self, h: Mat) -> Mat:
|
||||
state = self.state.h_to_v(h)
|
||||
if self.params.do_gaussian_visible:
|
||||
@@ -79,5 +98,14 @@ class Entity:
|
||||
|
||||
return prob(state)
|
||||
|
||||
def h_given_v(self, v: Mat) -> Mat:
|
||||
state = self.state.v_to_h(v)
|
||||
return state
|
||||
|
||||
|
||||
def v_given_h(self, h: Mat) -> Mat:
|
||||
state = self.state.h_to_v(h)
|
||||
return state
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Test: [passed]")
|
||||
|
||||
Reference in New Issue
Block a user