From 6a75e0b33b8a8f2c9c6a5e9f94e7c3ed3b9c2ed9 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 3 Jun 2026 22:14:34 +0200 Subject: [PATCH] added more Rnn helber --- src/stack/rnn_helper.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/stack/rnn_helper.py b/src/stack/rnn_helper.py index a27ee56..6d0b586 100644 --- a/src/stack/rnn_helper.py +++ b/src/stack/rnn_helper.py @@ -1,27 +1,39 @@ -import numpy as np +from rbm.matrix import Mat, np VOCAB = ' .!?ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' _CH2IDX = {ch: i for i, ch in enumerate(VOCAB)} +def vec2idx(vec: Mat) -> int: + index = np.argmax(vec) + return int(index) + +def idx2vec(index: int) -> np.ndarray: + result = np.zeros([1, len(VOCAB)]) + result[:, index] = 1 + return result + def ch2idx(ch: str) -> int: - return _CH2IDX[ch] + idx = _CH2IDX[ch] + return idx def idx2ch(idx: int) -> str: return VOCAB[idx] +def vocab_size(): + return len(VOCAB) def concat(v: np.ndarray, c: np.ndarray) -> np.ndarray: - return np.concatenate([v, c]) + return np.concatenate([v, c], axis=1) def split(vc: np.ndarray, v_size: np.ndarray|int) -> tuple[np.ndarray, np.ndarray]: n = len(v_size) if isinstance(v_size, np.ndarray) else v_size return vc[:n], vc[n:] -def shift_right(m: np.ndarray) -> np.ndarray: - return np.hstack([np.zeros((m.shape[0], 1)), m[:, :-1]]) +def shift_right(m: np.ndarray, amount: int = 1) -> np.ndarray: + return np.hstack([np.zeros((m.shape[0], amount)), m[:, :-amount]]) -def shift_left(m: np.ndarray) -> np.ndarray: - return np.hstack([m[:, 1:], np.zeros((m.shape[0], 1))]) +def shift_left(m: np.ndarray, amount: int = 1) -> np.ndarray: + return np.hstack([m[:, amount:], np.zeros((m.shape[0], amount))]) def shift_up(m: np.ndarray) -> np.ndarray: return np.vstack([m[1:, :], np.zeros((1, m.shape[1]))]) @@ -29,12 +41,3 @@ def shift_up(m: np.ndarray) -> np.ndarray: def shift_down(m: np.ndarray) -> np.ndarray: return np.vstack([np.zeros((1, m.shape[1])), m[:-1, :]]) -def clamp_one_hot(src_dst: np.ndarray) -> np.ndarray: - index = np.argmax(src_dst) - result = np.zeros_like(src_dst) - result[index] = 1 - return result - -class Rnn: - def __init__(self): - pass