From ea041288fcc9a5f0af09d9fea572ac428e644442 Mon Sep 17 00:00:00 2001 From: Aba Date: Mon, 20 Nov 2023 03:17:58 -0800 Subject: [PATCH] Fix softmax --- deepsocflow/py/bundle.py | 6 +++--- deepsocflow/py/model.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/deepsocflow/py/bundle.py b/deepsocflow/py/bundle.py index 34a31f3..92a462c 100644 --- a/deepsocflow/py/bundle.py +++ b/deepsocflow/py/bundle.py @@ -434,11 +434,11 @@ def apply_act(act_dict): if self.softmax: self.before_softmax = np.copy(self.proc['int']) self.softmax_frac = self.proc['frac'] - self.proc['int'] = self.proc['int'] / 2**self.softmax_frac + self.proc['int'] = (self.proc['int'] / 2**self.softmax_frac).astype(np.float32) self.softmax_max_f = self.proc['int'].max() - exp = np.exp(self.proc['int'] - self.softmax_max_f) - self.proc['int'] = exp/np.sum(exp, axis=1)[0] + exp = np.exp(self.proc['int'] - self.softmax_max_f).astype(np.float32) + self.proc['int'] = exp/np.sum(exp, axis=1, dtype=np.float32)[0] assert np.all(np.argmax(self.out['int'], axis=-1) == np.argmax(self.proc['int'], axis=-1)) else: diff --git a/deepsocflow/py/model.py b/deepsocflow/py/model.py index cfdc639..b77915d 100644 --- a/deepsocflow/py/model.py +++ b/deepsocflow/py/model.py @@ -298,8 +298,8 @@ def verify_inference(self, SIM, SIM_PATH): y_tiled_exp = b.o_int if b.softmax: y_tiled_sim = np.loadtxt(f"{hw.DATA_DIR}/{b.idx}_y_tiled_sim.txt", np.float32).reshape(y_tiled_exp.shape) - error = np.sum(np.abs(y_tiled_sim-y_tiled_exp)) - assert np.allclose(y_tiled_sim, y_tiled_exp, 1e-3), f"Error={error}, for y_tiled_sim at {b.idx=}. \n y_tiled_sim=\n{y_tiled_sim} \n y_tiled_exp=\n{y_tiled_exp}\n \nbefore_softmax=\n{b.before_softmax}" + error = np.max(np.abs(y_tiled_sim-y_tiled_exp)) + assert np.allclose(y_tiled_sim, y_tiled_exp, atol=1e-1), f"Error={error}, \nsub:\n{y_tiled_sim-y_tiled_exp} for y_tiled_sim at {b.idx=}. \n y_tiled_sim=\n{y_tiled_sim} \n y_tiled_exp=\n{y_tiled_exp}\n \nbefore_softmax=\n{b.before_softmax}" else: y_tiled_sim = np.loadtxt(f"{hw.DATA_DIR}/{b.idx}_y_tiled_sim.txt", np.float32).reshape(y_tiled_exp.shape) error = np.sum(np.abs(y_tiled_sim-y_tiled_exp)) @@ -318,4 +318,4 @@ def verify_inference(self, SIM, SIM_PATH): error = np.sum(np.abs(y_packed_sim-y_packed_exp)) assert error == 0, f"Error={error}, for y_packed_sim at {b.idx=}, y_packed_sim=\n{y_packed_sim[:100]} \n y_packed_exp=\n{y_packed_exp[:100]}\n" - print(f"Bundle {b.idx}, Error: {error}") \ No newline at end of file + print(f"Bundle {b.idx}, Error: {error}. Passed") \ No newline at end of file