- wrap numpy and cupy in matrix as np
- added type alias Mat for np.ndarray
This commit is contained in:
+10
-10
@@ -1,14 +1,14 @@
|
||||
import numpy as np
|
||||
from status import Status
|
||||
from cd_train import cd_jens
|
||||
from stack import Stack, StackType
|
||||
from matrix import Mat, np
|
||||
|
||||
class StackDeep(Stack):
|
||||
def __init__(self, name: str, work_dir: str = '.'):
|
||||
Stack.__init__(self, StackType.Deep, name, work_dir)
|
||||
|
||||
def batch_from(self, batch: np.ndarray, from_layer_id: int = 0):
|
||||
_batch = np.matrix.copy(batch)
|
||||
def batch_from(self, batch: Mat, from_layer_id: int = 0):
|
||||
_batch = np.copy(batch)
|
||||
for index, layer in enumerate(self.layers):
|
||||
if index == from_layer_id:
|
||||
break
|
||||
@@ -16,27 +16,27 @@ class StackDeep(Stack):
|
||||
|
||||
return _batch
|
||||
|
||||
def train(self, batch: np.ndarray, status=Status()):
|
||||
_batch = np.matrix.copy(batch)
|
||||
def train(self, batch: Mat, status=Status()):
|
||||
_batch = np.copy(batch)
|
||||
for index, layer in enumerate(self.layers):
|
||||
print(f"Train layer {index} for {layer.entity.params.num_epochs} epochs")
|
||||
_batch = self.batch_from(batch, index)
|
||||
layer.entity.train(_batch, cd_func=cd_jens, status=status)
|
||||
|
||||
|
||||
def pass_up(self, visible: np.ndarray, from_layer_id: int = 0):
|
||||
h = np.matrix.copy(visible)
|
||||
def pass_up(self, visible: Mat, from_layer_id: int = 0):
|
||||
h = np.copy(visible)
|
||||
for layer in self.layers[from_layer_id:]:
|
||||
h = layer.entity.gibbs_v_to_h(h)
|
||||
return h
|
||||
|
||||
def pass_down(self, hidden: np.ndarray, from_layer_id: int = 0):
|
||||
v = np.matrix.copy(hidden)
|
||||
def pass_down(self, hidden: Mat, from_layer_id: int = 0):
|
||||
v = np.copy(hidden)
|
||||
for layer in list(reversed(self.layers))[from_layer_id:]:
|
||||
v = layer.entity.gibbs_h_to_v(v)
|
||||
return v
|
||||
|
||||
def pass_down_up(self, visible: np.ndarray, from_layer_id: int = 0):
|
||||
def pass_down_up(self, visible: Mat, from_layer_id: int = 0):
|
||||
h = self.pass_up(visible, from_layer_id)
|
||||
v = self.pass_down(h)
|
||||
return v
|
||||
Reference in New Issue
Block a user