Ejemplo n.º 1
0
    class TrainingResultsTest( unittest.TestCase ):
        def setUp( self ):
            self.tr = TrainingResults()

            from opencl import OpenCL
            from layer import InputLayer, Layer, OutputLayer, ExecutionContext

            self.ocl = OpenCL( pyopencl.create_some_context() )

            self.i = InputLayer( 2, self.ocl )
            self.h = Layer( 3, self.ocl )
            self.o = OutputLayer( 1, self.ocl )

            self.i.link_next( self.h )
            self.h.link_next( self.o, 0, 3 )

            self.nnc = ExecutionContext( self.i, self.o, allow_training = True )

            self.i.set_weights( numpy.array( [ 0.1 ] * self.i.weights_count, numpy.float32 ) )
            self.h.set_weights( numpy.array( [ 0.2 ] * self.h.weights_count, numpy.float32 ) )
            self.o.set_weights( numpy.array( [ 0.3 ] * self.o.weights_count, numpy.float32 ) )

        def assertArrayEqual( self, ar1, ar2 ):
            self.assertEqual( len( ar1 ), len( ar2 ) )
            for x, y in zip( numpy.array( ar1, numpy.float32 ), numpy.array( ar2, numpy.float32 ) ):
                self.assertAlmostEqual( x, y, places = 5 )

        def test_store( self ):
            self.tr.reset()
            self.assertEqual( self.tr.iterations, numpy.int32( 0 ) )
            self.assertGreater( self.tr.minimal_error, numpy.float32( 1e6 ) )
            self.assertIsNone( self.tr.optimal_weights )
            self.assertAlmostEqual( self.tr.total_time, 0.0 )
            self.assertAlmostEqual( self.tr.opencl_time, 0.0 )

            self.i.set_inputs( numpy.array( [1.0, 1.0], numpy.float32 ), is_blocking = True )
            self.i.process()
            initial_result = self.o.get_outputs()

            self.tr.store_weights( self.nnc )
            self.i.set_weights( numpy.array( [ 0.4 ] * self.i.weights_count, numpy.float32 ) )
            self.i.process()

            self.assertNotEqual( initial_result, self.o.get_outputs() )

            self.tr.apply_weights( self.nnc )
            self.i.process()
            self.assertArrayEqual( initial_result , self.o.get_outputs() )
Ejemplo n.º 2
0
    class TrainingMethodTest( unittest.TestCase ):
        @classmethod
        def setUpClass( self ):
            from opencl import OpenCL
            from layer import InputLayer, OutputLayer, ExecutionContext

            self.ocl = OpenCL( pyopencl.create_some_context() )

            self.i = InputLayer( 2, self.ocl )
            self.o = OutputLayer( 1, self.ocl )

            self.i.link_next( self.o )

            self.nnc = ExecutionContext( self.i, self.o, allow_training = True )

            self.i.set_weights( numpy.array( [ 0.1 ] * self.i.weights_count, numpy.float32 ) )
            self.o.set_weights( numpy.array( [ 0.3 ] * self.o.weights_count, numpy.float32 ) )

            self.tr = TrainingResults()

            self._create_method()

        @classmethod
        def _create_method( self ):
            pass

        def assertArrayEqual( self, ar1, ar2 ):
            self.assertEqual( len( ar1 ), len( ar2 ) )
            for x, y in zip( numpy.array( ar1, numpy.float32 ), numpy.array( ar2, numpy.float32 ) ):
                self.assertAlmostEqual( x, y, places = 5 )

        def test_create( self ):
            self.setUpClass()

            if not getattr( self, 'method', None ):
                return

            self.assertAlmostEqual( self.method.n, 0.5 )
            self.assertAlmostEqual( self.method.alpha, 0.2 )
            self.assertAlmostEqual( self.method.kw, 1.03 )
            self.assertAlmostEqual( self.method.pd, 0.7 )
            self.assertAlmostEqual( self.method.pi, 1.02 )
            self.assertAlmostEqual( self.method.last_error, 0.0 )
            self.assertEqual( self.method.offline, False )

        def test_randomize_weights( self ):
            if not getattr( self, 'method', None ):
                return

            self.i.set_weights( numpy.array( [ 0.1 ] * self.i.weights_count, numpy.float32 ) )
            self.assertTrue( all( map( lambda x: abs( x - 0.1 ) < 0.0001, self.i.get_weights() ) ) )

            self.method.randomize_weights( self.nnc )
            w1 = self.i.get_weights()
            self.assertFalse( all( map( lambda x: abs( x - 0.1 ) < 0.0001, self.i.get_weights() ) ) )
            self.method.randomize_weights( self.nnc )
            self.assertFalse( all( map( lambda x: abs( x - 0.1 ) < 0.0001, self.i.get_weights() ) ) )
            self.assertNotAlmostEqual( ( w1 - self.i.get_weights() ).sum(), 0.0 )

        def test_adjust_weights( self ):
            if not getattr( self, 'method', None ):
                return

            self.method.last_error = numpy.float32( 1.0 )
            self.method.n = numpy.float32( 0.5 )
            self.method.kw = numpy.float32( 1.03 )
            self.method.pd = numpy.float32( 0.5 )
            self.method.pi = numpy.float32( 1.5 )

            self.method.adjust_training_parameters( 1.2 )
            self.assertAlmostEqual( self.method.n, 0.25 )
            self.assertAlmostEqual( self.method.last_error, 1.2 )

            self.method.adjust_training_parameters( 1.0 )
            self.assertAlmostEqual( self.method.n, 0.375 )
            self.assertAlmostEqual( self.method.last_error, 1.0 )

            self.method.adjust_training_parameters( 1.0 )
            self.assertAlmostEqual( self.method.n, 0.5625 )
            self.assertAlmostEqual( self.method.last_error, 1.0 )

        def test_prepare_training( self ):
            if not getattr( self, 'method', None ):
                return

            self.method.prepare_training( self.nnc )
            self.assertIsInstance( self.method._weights_delta_buf, pyopencl.Buffer )