Exemple #1
0
    internal static void calc_grads(layer_t layer, tensor_t <float> grad_next_layer)
    {
        switch (layer.type)
        {
        case layer_type.conv:
            ((conv_layer_t)layer).calc_grads(grad_next_layer.functorMethod);
            return;

        case layer_type.relu:
            ((relu_layer_t)layer).calc_grads(grad_next_layer.functorMethod);
            return;

        case layer_type.fc:
            ((fc_layer_t)layer).calc_grads(grad_next_layer.functorMethod);
            return;

        case layer_type.pool:
            ((pool_layer_t)layer).calc_grads(grad_next_layer.functorMethod);
            return;

        case layer_type.dropout_layer:
            ((dropout_layer_t)layer).calc_grads(grad_next_layer.functorMethod);
            return;

        default:
            Debug.Assert(false);
            break;
        }
    }
    public tensor_t(tensor_t other)
    {
        data = Arrays.InitializeWithDefaultInstances <T>(other.size.x * other.size.y * other.size.z);
//C++ TO C# CONVERTER TODO TASK: The memory management function 'memcpy' has no equivalent in C#:
        memcpy(this.data, other.data, other.size.x * other.size.y * other.size.z * sizeof(T));
//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: this->size = other.size;
        this.size.CopyFrom(other.size);
    }
Exemple #3
0
 public static void forward(vector <layer_t> layers, tensor_t <float> data)
 {
     for (int i = 0; i < layers.size(); i++)
     {
         if (i == 0)
         {
             activate(layers[i], data.functorMethod);
         }
         else
         {
             activate(layers[i], layers[i - 1].@out);
         }
     }
 }
Exemple #4
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);
    }
Exemple #5
0
    internal static void print_tensor(tensor_t <float> data)
    {
        int mx = data.size.x;
        int my = data.size.y;
        int mz = data.size.z;

        for (int z = 0; z < mz; z++)
        {
            Console.Write("[Dim{0:D}]\n", z);
            for (int y = 0; y < my; y++)
            {
                for (int x = 0; x < mx; x++)
                {
                    Console.Write("{0:f2} \t", (float)data.get(x, y, z));
                }
                Console.Write("\n");
            }
        }
    }
Exemple #6
0
    internal static tensor_t <float> to_tensor(List <List <List <float> > > data)
    {
        int z = data.Count;
        int y = data[0].Count;
        int x = data[0][0].Count;


        tensor_t <float> t = new tensor_t <float>(x, y, z);

        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                for (int k = 0; k < z; k++)
                {
                    t.functorMethod(i, j, k) = data[k][j][i];
                }
            }
        }
        return(t.functorMethod);
    }
Exemple #7
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);
    }