added workdir parameter
This commit is contained in:
+1
-1
@@ -45,7 +45,7 @@ def read_armadillo(filename: str) -> np.ndarray:
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Create stack from project file
|
# Create stack from project file
|
||||||
stack = StackFactory.from_file("/home/jens/work/repos/Rbm/test.prj")
|
stack = StackFactory.from_file("/home/jens/work/repos/Rbm/test.prj", work_dir="../../results")
|
||||||
|
|
||||||
# Load state
|
# Load state
|
||||||
stack.state_load()
|
stack.state_load()
|
||||||
|
|||||||
+8
-3
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from layer import Layer
|
from layer import Layer
|
||||||
|
|
||||||
@@ -9,10 +10,12 @@ class StackException(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class Stack:
|
class Stack:
|
||||||
def __init__(self, stack_type: StackType, name: str):
|
def __init__(self, stack_type: StackType, name: str, work_dir: str = '.'):
|
||||||
self.stack_type = stack_type
|
self.stack_type = stack_type
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.work_dir = work_dir
|
||||||
self.layers: list[Layer] = []
|
self.layers: list[Layer] = []
|
||||||
|
os.makedirs(self.work_dir, exist_ok=True)
|
||||||
|
|
||||||
def num_layers(self):
|
def num_layers(self):
|
||||||
return len(self.layers)
|
return len(self.layers)
|
||||||
@@ -44,8 +47,10 @@ class Stack:
|
|||||||
|
|
||||||
def state_save(self):
|
def state_save(self):
|
||||||
for index, layer in enumerate(self.layers):
|
for index, layer in enumerate(self.layers):
|
||||||
layer.save(f"{self.name}-{index}-state.npz")
|
filepath = os.path.join(self.work_dir, f"{self.name}-{index}-state.npz")
|
||||||
|
layer.save(filepath)
|
||||||
|
|
||||||
def state_load(self):
|
def state_load(self):
|
||||||
for index, layer in enumerate(self.layers):
|
for index, layer in enumerate(self.layers):
|
||||||
layer.load(f"{self.name}-{index}-state.npz")
|
filepath = os.path.join(self.work_dir, f"{self.name}-{index}-state.npz")
|
||||||
|
layer.load(filepath)
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ from cd_train import cd_jens
|
|||||||
from stack import Stack, StackType
|
from stack import Stack, StackType
|
||||||
|
|
||||||
class StackDeep(Stack):
|
class StackDeep(Stack):
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str, work_dir: str = '.'):
|
||||||
Stack.__init__(self, StackType.Deep, name)
|
Stack.__init__(self, StackType.Deep, name, work_dir)
|
||||||
|
|
||||||
def batch_from(self, batch: np.ndarray, from_layer_id: int = 0):
|
def batch_from(self, batch: np.ndarray, from_layer_id: int = 0):
|
||||||
_batch = np.matrix.copy(batch)
|
_batch = np.matrix.copy(batch)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from stack_rnn import StackRnn
|
|||||||
|
|
||||||
class StackFactory:
|
class StackFactory:
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_dict(cls, project: dict, layer_constructor: Callable = None) -> StackDeep|StackRnn:
|
def from_dict(cls, project: dict, work_dir: str = ".", layer_constructor: Callable = None) -> StackDeep|StackRnn:
|
||||||
name = project["stack"]["name"]
|
name = project["stack"]["name"]
|
||||||
layers = project["stack"]["layers"]
|
layers = project["stack"]["layers"]
|
||||||
|
|
||||||
@@ -21,9 +21,9 @@ class StackFactory:
|
|||||||
|
|
||||||
obj = None
|
obj = None
|
||||||
if stack_type == StackType.Deep:
|
if stack_type == StackType.Deep:
|
||||||
obj = StackDeep(name)
|
obj = StackDeep(name, work_dir)
|
||||||
if stack_type == StackType.Rnn:
|
if stack_type == StackType.Rnn:
|
||||||
obj = StackRnn(name)
|
obj = StackRnn(name, work_dir)
|
||||||
|
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return obj
|
return obj
|
||||||
@@ -56,16 +56,16 @@ class StackFactory:
|
|||||||
return obj
|
return obj
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_file(cls, filename: str, layer_constructor: Callable = None) -> StackDeep|StackRnn:
|
def from_file(cls, filename: str, work_dir: str = ".", layer_constructor: Callable = None) -> StackDeep|StackRnn:
|
||||||
obj = None
|
obj = None
|
||||||
with open(filename, "r") as fp:
|
with open(filename, "r") as fp:
|
||||||
prj = json.load(fp)
|
prj = json.load(fp)
|
||||||
obj = StackFactory.from_dict(prj)
|
obj = StackFactory.from_dict(prj, work_dir)
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
stack = StackFactory.from_file("/home/jens/work/repos/Rbm/test.prj")
|
stack = StackFactory.from_file("/home/jens/work/repos/Rbm/test.prj", work_dir="../../results")
|
||||||
|
|
||||||
print("Test: [passed]")
|
print("Test: [passed]")
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from stack import Stack, StackType
|
from stack import Stack, StackType
|
||||||
|
|
||||||
class StackRnn(Stack):
|
class StackRnn(Stack):
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str, work_dir: str = '.'):
|
||||||
Stack.__init__(self, StackType.Rnn, name)
|
Stack.__init__(self, StackType.Rnn, name, work_dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user