Skip to content

Commit

Permalink
Initialization of Dense with all-zero weights (#585)
Browse files Browse the repository at this point in the history
* fixed using all 0 weight matrix

* added unit test
  • Loading branch information
PhilippPlank authored Jan 27, 2023
1 parent 1896c21 commit 043a3c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lava/utils/weightutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def _determine_weight_exp(weights: np.ndarray,

scale = 0

if max_weight == min_weight == 0:
weight_exp = -0
return weight_exp

if sign_mode == SignMode.MIXED:
pos_scale = 127 / max_weight if max_weight > 0 else np.inf
neg_scale = -128 / min_weight if min_weight < 0 else np.inf
Expand Down
12 changes: 12 additions & 0 deletions tests/lava/utils/test_weightutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ def test_optimize_weight_bits_weight_exp(self) -> None:
self.assertEqual(optimized.weight_exp, -4)
self.assertEqual(optimized.num_weight_bits, 3)

def test_optimize_weight_bits_wgts_all_zero(self) -> None:
weights = np.array([0, 0, 0])
sign_mode = SignMode.EXCITATORY

optimized = optimize_weight_bits(weights=weights,
sign_mode=sign_mode,
loihi2=True)

np.testing.assert_array_equal(optimized.weights, np.array([0, 0, 0]))
self.assertEqual(optimized.weight_exp, -0)
self.assertEqual(optimized.num_weight_bits, 0)

def test_determine_weight_exp_inhibitory_0(self) -> None:
weight_exp = _determine_weight_exp(weights=np.array([-255, -128, -1]),
sign_mode=SignMode.INHIBITORY)
Expand Down

0 comments on commit 043a3c2

Please sign in to comment.