def test_layer_clamping_respects_clamp_max() -> None: layer = lr.Layer(name="in", size=3, spec=sp.LayerSpec(clamp_max=0.5)) layer.clamp(act_ext=[0, 1]) layer.handle(ev.Clamp(layer_name="lr1", acts=[0, 1])) expected = [0, 0.5, 0] for i in range(len(expected)): assert math.isclose(layer.units.act[i], expected[i], abs_tol=1e-6)
def clamp_layer(self, name: str, acts: Sequence[float], hard: bool = True, weight: float = 1.0) -> None: """Clamps the layer's activations. After forcing, the layer's activations will be set to the values contained in `acts` and will not change from cycle to cycle. Args: name: The name of the layer. acts: A sequence containing the activations that the layer's units will be clamped to. If its length is less than the number of units in the layer, it will be tiled. If its length is greater, the extra values will be ignored. hard: Toggles hard or soft clamping. (default: True) weight: Weighting on clamping (default: 1.0) ValueError: If `name` does not match any existing layer name. """ self._validate_layer_name(name) self.handle(events.Clamp(name, acts, hard, weight))
def test_clamp_checks_if_acts_contains_values_outside_0_1() -> None: with pytest.raises(ValueError): ev.Clamp(layer_name="lr1", acts=(1, 2))
def test_clamp_event_does_nothing_if_the_names_do_not_match() -> None: clamp = ev.Clamp(layer_name="lr1", acts=[0.7, 0.7]) layer = lr.Layer("WHALES", 3) layer.handle(clamp) assert not layer.clamped
def test_clamp_event_clamps_a_layer_if_the_names_match() -> None: clamp = ev.Clamp(layer_name="lr1", acts=[0.7, 0.7]) layer = lr.Layer("lr1", 3) layer.handle(clamp) assert layer.clamped assert all(layer.units.act == 0.7)