Giter Club home page Giter Club logo

Comments (4)

elamaunt avatar elamaunt commented on August 21, 2024

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.

from convnetsharp.

elamaunt avatar elamaunt commented on August 21, 2024

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

from convnetsharp.

mvrozanti avatar mvrozanti commented on August 21, 2024

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.

from convnetsharp.

cbovar avatar cbovar commented on August 21, 2024

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();

from convnetsharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.