Exemplo n.º 1
0
    public static float train(vector <layer_t> layers, tensor_t <float> data, tensor_t <float> expected)
    {
        for (int i = 0; i < layers.size(); i++)
        {
            if (i == 0)
            {
                activate(layers[i], data.functorMethod);
            }
            else
            {
                activate(layers[i], layers[i - 1].@out);
            }
        }

        tensor_t <float> grads = layers.back().@out - expected.functorMethod;

        for (int i = layers.size() - 1; i >= 0; i--)
        {
            if (i == layers.size() - 1)
            {
                calc_grads(layers[i], grads.functorMethod);
            }
            else
            {
                calc_grads(layers[i], layers[i + 1].grads_in);
            }
        }

        for (int i = 0; i < layers.size(); i++)
        {
            fix_weights(layers[i]);
        }

        float err = 0F;

        for (int i = 0; i < grads.size.x * grads.size.y * grads.size.z; i++)
        {
            float f = expected.data[i];
            if (f > 0.5F)
            {
                err += Math.Abs(grads.data[i]);
            }
        }
        return(err * 100);
    }
Exemplo n.º 2
0
        //C++ TO C# CONVERTER WARNING: 'const' methods are not available in C#:
        //ORIGINAL LINE: void itemize(const UInt16* string, int string_length, FontStyle style, ClassicVector<Run>* result) const;
        public void itemize(UInt16 @string, int string_size, FontStyle style, vector <Run> result)
        {
            uint       langListId = style.getLanguageListId();
            int        variant    = style.getVariant();
            FontFamily lastFamily = null;
            Run        run        = null;

            if (string_size == 0)
            {
                return;
            }

            const uint kEndOfString = 0xFFFFFFFF;

            uint nextCh       = 0;
            uint prevCh       = 0;
            int  nextUtf16Pos = 0;
            int  readLength   = 0;

            U16_NEXT(@string, readLength, string_size, nextCh);

            do
            {
                uint ch       = new uint(nextCh);
                int  utf16Pos = nextUtf16Pos;
                //C++ TO C# CONVERTER TODO TASK: The following line was determined to be a copy assignment (rather than a reference assignment) - this should be verified and a 'CopyFrom' method should be created:
                //ORIGINAL LINE: nextUtf16Pos = readLength;
                nextUtf16Pos.CopyFrom(readLength);
                if (readLength < string_size)
                {
                    U16_NEXT(@string, readLength, string_size, nextCh);
                }
                else
                {
                    //C++ TO C# CONVERTER TODO TASK: The following line was determined to be a copy assignment (rather than a reference assignment) - this should be verified and a 'CopyFrom' method should be created:
                    //ORIGINAL LINE: nextCh = kEndOfString;
                    nextCh = kEndOfString;
                }

                bool shouldContinueRun = false;
                if (lastFamily != null)
                {
                    if (isStickyWhitelisted(ch))
                    {
                        // Continue using existing font as long as it has coverage and is
                        // whitelisted
                        shouldContinueRun = lastFamily.getCoverage().get(ch);
                    }
                    else if (ch == SOFT_HYPHEN || isVariationSelector(ch))
                    {
                        // Always continue if the character is the soft hyphen or a variation
                        // selector.
                        shouldContinueRun = true;
                    }
                }

                if (!shouldContinueRun)
                {
                    FontFamily *family = getFamilyForChar(ch, isVariationSelector(new uint(nextCh)) ? nextCh : 0, langListId, variant);
                    if (utf16Pos == 0 || family.get() != lastFamily)
                    {
                        int start = utf16Pos;
                        // Workaround for combining marks and emoji modifiers until we implement
                        // per-cluster font selection: if a combining mark or an emoji modifier
                        // is found in a different font that also supports the previous
                        // character, attach previous character to the new run. U+20E3 COMBINING
                        // ENCLOSING KEYCAP, used in emoji, is handled properly by this since
                        // it's a combining mark too.
                        if (utf16Pos != 0 && ((U_GET_GC_MASK(ch) & U_GC_M_MASK) != 0 || (isEmojiModifier(ch) && isEmojiBase(prevCh))) && family != null && family.getCoverage().get(prevCh))
                        {
                            int prevChLength = U16_LENGTH(prevCh);
                            run.end -= prevChLength;
                            if (run.start == run.end)
                            {
                                result.pop_back();
                            }
                            start -= prevChLength;
                        }
                        result.push_back({ family.getClosestMatch(style), (int)start, 0 });
                        run        = result.back();
                        lastFamily = family.get();
                    }
Exemplo n.º 3
0
    static int Main()
    {
        vector <case_t> cases = read_test_cases();

        vector <layer_t> layers = new vector <layer_t>();

        conv_layer_t layer1 = new conv_layer_t(1, 5, 8, cases[0].data.size); // 28 * 28 * 1 -> 24 * 24 * 8
                                                                             //C++ TO C# CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
                                                                             //ORIGINAL LINE: relu_layer_t * layer2 = new relu_layer_t(layer1->out.size);
        relu_layer_t layer2 = new relu_layer_t(new point_t([email protected]));
        //C++ TO C# CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
        //ORIGINAL LINE: pool_layer_t * layer3 = new pool_layer_t(2, 2, layer2->out.size);
        pool_layer_t layer3 = new pool_layer_t(2, 2, new point_t([email protected])); // 24 * 24 * 8 -> 12 * 12 * 8

        //C++ TO C# CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
        //ORIGINAL LINE: conv_layer_t * layer4 = new conv_layer_t(1, 3, 10, layer3->out.size);
        conv_layer_t layer4 = new conv_layer_t(1, 3, 10, new point_t([email protected])); // 12 * 12 * 6 -> 10 * 10 * 10
                                                                                         //C++ TO C# CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
                                                                                         //ORIGINAL LINE: relu_layer_t * layer5 = new relu_layer_t(layer4->out.size);
        relu_layer_t layer5 = new relu_layer_t(new point_t([email protected]));
        //C++ TO C# CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
        //ORIGINAL LINE: pool_layer_t * layer6 = new pool_layer_t(2, 2, layer5->out.size);
        pool_layer_t layer6 = new pool_layer_t(2, 2, new point_t([email protected])); // 10 * 10 * 10 -> 5 * 5 * 10

        //C++ TO C# CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
        //ORIGINAL LINE: fc_layer_t * layer7 = new fc_layer_t(layer6->out.size, 10);
        fc_layer_t layer7 = new fc_layer_t(new point_t([email protected]), 10); // 4 * 4 * 16 -> 10

        layers.push_back((layer_t)layer1);
        layers.push_back((layer_t)layer2);
        layers.push_back((layer_t)layer3);
        layers.push_back((layer_t)layer4);
        layers.push_back((layer_t)layer5);
        layers.push_back((layer_t)layer6);
        layers.push_back((layer_t)layer7);



        float amse = 0F;
        int   ic   = 0;

        for (int ep = 0; ep < 100000;)
        {
            foreach (case_t t in cases)
            {
                float xerr = train(layers, t.data.functorMethod, [email protected]);
                amse += xerr;

                ep++;
                ic++;

                if (ep % 1000 == 0)
                {
                    Console.Write("case ");
                    Console.Write(ep);
                    Console.Write(" err=");
                    Console.Write(amse / ic);
                    Console.Write("\n");
                }

                // if ( GetAsyncKeyState( VK_F1 ) & 0x8000 )
                // {
                //     printf( "err=%.4f%\n", amse / ic  );
                //     goto end;
                // }
            }
        }
        // end:



        while (true)
        {
            uint8_t[] data = read_file("test.ppm");

            if (data != null)
            {
                //C++ TO C# CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
                //ORIGINAL LINE: uint8_t * usable = data;
                uint8_t[] usable = new uint8_t(data);

                while ((uint32_t)usable != 0x0A353532)
                {
                    usable++;
                }

                //C++ TO C# CONVERTER TODO TASK: There is no equivalent to most C++ 'pragma' directives in C#:
                //#pragma pack(push, 1)
                //C++ TO C# CONVERTER TODO TASK: C# does not allow declaring types within methods:
                //			struct RGB
                //			{
                //				uint8_t r, g, b;
                //			};
                //C++ TO C# CONVERTER TODO TASK: There is no equivalent to most C++ 'pragma' directives in C#:
                //#pragma pack(pop)

                RGB[] rgb = (RGB)usable;

                tensor_t <float> image = new tensor_t <float>(28, 28, 1);
                for (int i = 0; i < 28; i++)
                {
                    for (int j = 0; j < 28; j++)
                    {
                        RGB rgb_ij = rgb[i * 28 + j];
                        image.functorMethod(j, i, 0) = ((((float)rgb_ij.r + rgb_ij.g + rgb_ij.b) / (3.0f * 255.0f)));
                    }
                }

                forward(layers, image.functorMethod);
                tensor_t <float> @out = layers.back().@out;
                for (int i = 0; i < 10; i++)
                {
                    Console.Write("[{0:D}] {1:f}\n", i, @out.functorMethod(i, 0, 0) * 100.0f);
                }

                data = null;
            }

            timespec wait = new timespec();
            wait.tv_sec  = 1;
            wait.tv_nsec = 0;
            nanosleep(wait, null);
        }
        return(0);
    }