コード例 #1
0
    def test_deriv(self):
        vars = np.arange(-100, 100, 2.5)
        test_fcns = [
            TanSig(),
            PureLin(),
            LogSig(),
            HardLim(),
            HardLims(),
            SatLin(),
            SatLins(),
            SatLinPrm(1, 0, 1),
            SatLinPrm(1, -1, 1)
        ]

        def diff(f, x, h=1E-6):
            x1 = np.array([x - h])
            x2 = np.array([x])
            r = (f(x2)[0] - f(x1)[0]) / h
            return r

        for test_fcn in test_fcns:
            for var in vars:
                m = diff(test_fcn, var)
                v = test_fcn(np.array([var]))
                t = test_fcn.deriv(np.array([var]), v)[0]
                self.assertAlmostEqual(m, t, 5)
コード例 #2
0
 def test_satlinprm(self):
     # like SatLin
     test_fcn = SatLinPrm(1, 0, 1)
     vars = [-2.5, -0.5, 0.0, 0.1, 3.0]
     m_res = [0, 0, 0, 0.1, 1.0]
     t_res = test_fcn(np.array(vars)).tolist()
     for m, t in zip(m_res, t_res):
         self.assertEqual(m, t)
     # like SatLins
     test_fcn = SatLinPrm(1, -1, 1)
     vars = [-2.5, -0.5, 0.0, 0.1, 3.0]
     m_res = [-1.0, -0.5, 0.0, 0.1, 1.0]
     t_res = test_fcn(np.array(vars)).tolist()
     for m, t in zip(m_res, t_res):
         self.assertEqual(m, t)