added more Rnn helber
This commit is contained in:
+19
-16
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user