- fixed armadillo data read

- added shape to layer
- changed construction of layer from Stack factory
- fixed rms_error_accu scaling
- RBM: added image display using opencv
- improved Status print
This commit is contained in:
2025-12-17 17:05:40 +01:00
parent a1e7a803d1
commit 6ef577c769
6 changed files with 29 additions and 32 deletions
+17 -26
View File
@@ -1,29 +1,7 @@
import os.path
import numpy as np
from layer import Layer
from stack import Stack, StackType, StackException
import cv2 as cv
from stack_factory import StackFactory
from params import RbmParams
def test():
params = RbmParams()
stack = Stack(StackType.Deep, "Stack")
dims = [(2, 3), (3, 4), (4, 5)]
for n, dim in enumerate(dims):
layer = Layer(f"Layer-{n}", dim, params)
stack.append(layer)
stack.init(std=0.1)
stack.state_save()
lay0 = stack.layers[0]
lay1 = stack.from_name("Layer-1")
lay2 = stack.from_index(2)
stack.remove(lay2)
stack.remove(lay1)
stack.remove(lay0)
def read_armadillo(filename: str) -> np.ndarray:
result = None
@@ -37,14 +15,18 @@ def read_armadillo(filename: str) -> np.ndarray:
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]):
line = fp.readline().replace("\n", '').split(' ')
line = line[1:]
data = [float(s) for s in line]
result[row, :] = data
return result
def cv_show(vec: np.array, shape):
img = cv.Mat(np.resize(vec, shape))
img_n = cv.normalize(src=img, dst=None, alpha=255, beta=0, norm_type=cv.NORM_MINMAX, dtype=cv.CV_8U)
cv.imshow(f"training samples", img_n)
def main(prj_name: str = "test"):
work_dir = "../../results"
@@ -63,12 +45,21 @@ def main(prj_name: str = "test"):
training_data_path = os.path.join(prj_root, f"{prj_name}.training.dat")
batch = read_armadillo(training_data_path)
# Shape of training vector
shape = stack.from_index(0).shape[0:2] + (1,)
# Train
stack.train(batch)
# Save state
stack.state_save()
for n in range(batch.shape[0]):
cv_show(batch[n,:], shape)
cv.waitKeyEx(200)
if __name__ == "__main__":
main("norb_small_16h")
cv.destroyAllWindows()
print("Test: [passed]")