Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How exactly one goes about learning XOR in with this tool? #59

Closed
mvrozanti opened this issue Jul 17, 2017 · 4 comments
Closed

How exactly one goes about learning XOR in with this tool? #59

mvrozanti opened this issue Jul 17, 2017 · 4 comments

Comments

@mvrozanti
Copy link

mvrozanti commented Jul 17, 2017

I don't get the concept of 'classes' of the README example.

I know the purpose of this tool is to deal with multidimensional input but I'm a bit stuck

@elamaunt
Copy link

Hi!
I tried to implement same code with convnetjs from https://gist.github.com/willguitaradmfar/12f7efb2ab9534755619ba4dce72f988

Js sample works fine:
image

But same code on sharp doesnt give same result:

        var net = new Net<double>();
        
        net.AddLayer(new InputLayer(1, 1, 2));

        net.AddLayer(new FullyConnLayer(3));
        net.AddLayer(new TanhLayer());

        net.AddLayer(new FullyConnLayer(2));
        net.AddLayer(new SoftmaxLayer(2));


        var x0 = new Volume(new double[] { 1, 1 }, new Shape(2));
        var x1 = new Volume(new double[] { 0, 1 }, new Shape(2));
        var x2 = new Volume(new double[] { 1, 0 }, new Shape(2));
        var x3 = new Volume(new double[] { 0, 0 }, new Shape(2));
        
        Console.WriteLine("x0: " + net.Forward(x0).Get(0));
        Console.WriteLine("x1: " + net.Forward(x1).Get(0));
        Console.WriteLine("x2: " + net.Forward(x2).Get(0));
        Console.WriteLine("x3: " + net.Forward(x3).Get(0));

        Console.WriteLine();
        Console.WriteLine();
        
        var trainer = new SgdTrainer<double>(net) { LearningRate = 0.2, L2Decay = 0.001 };
        
        for (int i = 0; i < 2000; i++)
        {
            trainer.Train(x0, new Volume(new double[] { 0.0 }, new Shape(1)));
            trainer.Train(x1, new Volume(new double[] { 1.0 }, new Shape(1)));
            trainer.Train(x2, new Volume(new double[] { 1.0 }, new Shape(1)));
            trainer.Train(x3, new Volume(new double[] { 0.0 }, new Shape(1)));
        }

        Console.WriteLine("x0: " + net.Forward(x0).Get(0));
        Console.WriteLine("x1: " + net.Forward(x1).Get(0));
        Console.WriteLine("x2: " + net.Forward(x2).Get(0));
        Console.WriteLine("x3: " + net.Forward(x3).Get(0));

image

Perhaps there are some errors in my code or in the project code.

@elamaunt
Copy link

Changing of shape changes results. I could not understand how does shapes work =/

@mvrozanti
Copy link
Author

mvrozanti commented Nov 10, 2017

I've given up on C# approaches. Python is much more readable and while it may not be as 'debuggable' it is easier to understand where you're screwing up. Also so many good libraries.

@cbovar
Copy link
Owner

cbovar commented Nov 18, 2017

From issue #74

var net = new Net<double>();
net.AddLayer(new InputLayer(1, 1, 2)); // Two inputs
net.AddLayer(new FullyConnLayer(3));
net.AddLayer(new TanhLayer());
net.AddLayer(new FullyConnLayer(2));
net.AddLayer(new SoftmaxLayer(2)); // Two possible outcome/class (0 and 1)

// All possible inputs
var x0 = new Volume(new double[] { 1, 1 }, new Shape(2));
var x1 = new Volume(new double[] { 0, 1 }, new Shape(2));
var x2 = new Volume(new double[] { 1, 0 }, new Shape(2));
var x3 = new Volume(new double[] { 0, 0 }, new Shape(2));

var trainer = new SgdTrainer<double>(net) { LearningRate = 0.2 };

for (var i = 0; i < 2000; i++)
{
    trainer.Train(x0, new Volume(new[] { 1.0, 0.0 }, new Shape(1, 1, 2, 1)));
    trainer.Train(x1, new Volume(new[] { 0.0, 1.0 }, new Shape(1, 1, 2, 1)));
    trainer.Train(x2, new Volume(new[] { 0.0, 1.0 }, new Shape(1, 1, 2, 1)));
    trainer.Train(x3, new Volume(new[] { 1.0, 0.0 }, new Shape(1, 1, 2, 1)));

    Console.WriteLine(trainer.Loss); // Should decrease
}

Console.WriteLine("x0: " + net.Forward(x0).Get(0));
Console.WriteLine("x1: " + net.Forward(x1).Get(0));
Console.WriteLine("x2: " + net.Forward(x2).Get(0));
Console.WriteLine("x3: " + net.Forward(x3).Get(0));

Console.ReadLine();

@cbovar cbovar closed this as completed Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants