- added ready of armadillo dat-file (Quick and dirty)
- added stack training for DeepStack
This commit is contained in:
+35
-1
@@ -1,8 +1,10 @@
|
||||
import numpy as np
|
||||
from layer import Layer
|
||||
from stack import Stack, StackType, StackException
|
||||
from stack_factory import StackFactory
|
||||
from params import RbmParams
|
||||
|
||||
if __name__ == "__main__":
|
||||
def test():
|
||||
params = RbmParams()
|
||||
stack = Stack(StackType.Deep, "Stack")
|
||||
dims = [(2, 3), (3, 4), (4, 5)]
|
||||
@@ -21,4 +23,36 @@ if __name__ == "__main__":
|
||||
stack.remove(lay1)
|
||||
stack.remove(lay0)
|
||||
|
||||
def read_armadillo(filename: str) -> np.ndarray:
|
||||
result = None
|
||||
with open(filename) as fp:
|
||||
identifier = fp.readline().replace("\n", '')
|
||||
if "ARMA_MAT_TXT_FN008" not in identifier:
|
||||
raise Exception("Not a armadillo data file!")
|
||||
|
||||
line = fp.readline().replace("\n", '').split(' ')
|
||||
shape = [int(s) for s in line]
|
||||
print(f"shape: {shape}")
|
||||
|
||||
result = np.zeros(shape=shape, dtype=np.float64)
|
||||
line = fp.readline().replace("\n", '').split(' ')
|
||||
line = line[1:]
|
||||
data = [float(s) for s in line]
|
||||
for row in range(shape[0]):
|
||||
result[row, :] = data
|
||||
|
||||
return result
|
||||
|
||||
def main():
|
||||
# Create stack from project file
|
||||
stack = StackFactory.from_file("/home/jens/work/repos/Rbm/test.prj")
|
||||
|
||||
# Load train data
|
||||
batch = read_armadillo("/home/jens/work/repos/Rbm/test.training.dat")
|
||||
|
||||
# Train
|
||||
stack.train(batch)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
print("Test: [passed]")
|
||||
|
||||
@@ -1,5 +1,25 @@
|
||||
import numpy as np
|
||||
from status import Status
|
||||
from cd_train import cd_jens
|
||||
from stack import Stack, StackType
|
||||
|
||||
class StackDeep(Stack):
|
||||
def __init__(self, name: str):
|
||||
Stack.__init__(self, StackType.Deep, name)
|
||||
|
||||
def batch_from(self, batch: np.ndarray, from_layer_id: int = 0):
|
||||
_batch = np.matrix.copy(batch)
|
||||
for index, layer in enumerate(self.layers):
|
||||
if index == from_layer_id:
|
||||
break
|
||||
_batch = layer.v_to_ph(_batch)
|
||||
|
||||
return _batch
|
||||
|
||||
def train(self, batch: np.ndarray):
|
||||
_batch = np.matrix.copy(batch)
|
||||
for index, layer in enumerate(self.layers):
|
||||
print(f"Train layer {index}")
|
||||
_batch = self.batch_from(batch, index)
|
||||
layer.train(_batch, cd_func=cd_jens, status=Status())
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from stack_rnn import StackRnn
|
||||
|
||||
class StackFactory:
|
||||
@classmethod
|
||||
def from_dict(cls, project: dict, layer_constructor: Callable = None) -> Stack:
|
||||
def from_dict(cls, project: dict, layer_constructor: Callable = None) -> StackDeep|StackRnn:
|
||||
name = project["stack"]["name"]
|
||||
layers = project["stack"]["layers"]
|
||||
|
||||
@@ -56,7 +56,7 @@ class StackFactory:
|
||||
return obj
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, filename: str, layer_constructor: Callable = None) -> Stack:
|
||||
def from_file(cls, filename: str, layer_constructor: Callable = None) -> StackDeep|StackRnn:
|
||||
obj = None
|
||||
with open(filename, "r") as fp:
|
||||
prj = json.load(fp)
|
||||
|
||||
Reference in New Issue
Block a user