Beispiel #1
0
    def test_scheduled_contrastive_hebbian(self):
        o = pnl.TransferMechanism()
        m = pnl.ContrastiveHebbianMechanism(
            input_size=2,
            hidden_size=0,
            target_size=2,
            separated=False,
            mode=pnl.SIMPLE_HEBBIAN,
            integrator_mode=True,
            enable_learning=False,
            matrix=[[0, -1], [-1, 0]],
            # auto=0,
            # hetero=-1,
        )

        # set max passes to ensure failure if no convergence instead of infinite loop
        m.max_passes = 1000

        s = pnl.sys(m, o)
        ms = pnl.Scheduler(system=s)
        ms.add_condition(o, pnl.WhenFinished(m))
        s.scheduler_processing = ms
        # m.reinitialize_when=pnl.Never()
        print('matrix:\n', m.afferents[1].matrix)
        results = s.run(inputs=[2, 2], num_trials=4)
        print(results)
        np.testing.assert_allclose(results,
                                   [[np.array([2.])], [np.array([2.])],
                                    [np.array([2.])], [np.array([2.])]])
    def test_scheduled_contrastive_hebbian(self):
        o = pnl.TransferMechanism()
        m = pnl.ContrastiveHebbianMechanism(
            input_size=2,
            hidden_size=0,
            target_size=2,
            separated=False,
            mode=pnl.SIMPLE_HEBBIAN,
            integrator_mode=True,
            enable_learning=False,
            matrix=[[0, -1], [-1, 0]],
            # auto=0,
            # hetero=-1,
        )

        # set max passes to ensure failure if no convergence instead of infinite loop
        m.max_passes = 1000

        c = pnl.Composition()
        c.add_linear_processing_pathway([m, o])
        c.scheduler.add_condition(o, pnl.WhenFinished(m))
        c._analyze_graph()
        print('matrix:\n', m.afferents[1].matrix)
        c.run(inputs={m: [2, 2]}, num_trials=4)
        results = c.results
        print(results)
        np.testing.assert_allclose(results,
                                   [[np.array([2.])], [np.array([2.])],
                                    [np.array([2.])], [np.array([2.])]])
Beispiel #3
0
    def test_configure_learning(self):

        o = pnl.TransferMechanism()
        m = pnl.ContrastiveHebbianMechanism(input_size=2,
                                            hidden_size=0,
                                            target_size=2,
                                            mode=pnl.SIMPLE_HEBBIAN,
                                            separated=False,
                                            matrix=[[0, -.5], [-.5, 0]])

        with pytest.warns(UserWarning) as record:
            m.learning_enabled = True

        correct_message_found = False
        for warning in record:
            if ("Learning cannot be enabled" in str(warning.message)
                    and "because it has no LearningMechanism" in str(
                        warning.message)):
                correct_message_found = True
                break
        assert correct_message_found

        m.configure_learning()
        m.reinitialize_when = pnl.Never()
        s = pnl.sys(m, o)

        ms = pnl.Scheduler(system=s)
        ms.add_condition(o, pnl.WhenFinished(m))
        s.scheduler_processing = ms
        results = s.run(inputs=[2, 2], num_trials=4)

        np.testing.assert_allclose(
            results,
            [[[2.671875]], [[2.84093837]], [[3.0510183]], [[3.35234623]]])
    def test_configure_learning(self):

        o = pnl.TransferMechanism()
        m = pnl.ContrastiveHebbianMechanism(input_size=2,
                                            hidden_size=0,
                                            target_size=2,
                                            mode=pnl.SIMPLE_HEBBIAN,
                                            separated=False,
                                            matrix=[[0, -.5], [-.5, 0]])

        regexp = r"Learning cannot be enabled for .* because it has no LearningMechanism"
        with pytest.warns(UserWarning, match=regexp):
            m.learning_enabled = True

        m.configure_learning()
        m.reset_stateful_function_when = pnl.Never()

        c = pnl.Composition()
        c.add_linear_processing_pathway([m, o])
        c.scheduler.add_condition(o, pnl.WhenFinished(m))
        c.learn(inputs={m: [2, 2]}, num_trials=4)
        results = c.parameters.results.get(c)
        np.testing.assert_allclose(
            results,
            [[[2.671875]], [[2.84093837]], [[3.0510183]], [[3.35234623]]])