Vectorflow noob

BitR via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 11 07:35:50 PDT 2017


On Thursday, 10 August 2017 at 19:10:05 UTC, Jiyan wrote:
> Hey,
> wanted to the following simple thing with vectorflow:
> ...

You'll want to end your stack with your wanted output size (1 - 
being the sum).
Training it with the "square" function seems to give the best 
result for simple additions.

Hope you can use it:

import std.stdio;
import std.random;
import std.algorithm;
import std.range;
import std.math;

import vectorflow;

struct Obs
{
     float label;
     float[] features;
}

void main()
{
     const
           learning_rate = 0.01,
           epochs = 200,
           verbose = true,
           cores = 3;
     auto net = NeuralNet()
         .stack(DenseData(2))
         .stack(Linear(1));
     net.initialize(0.1);

     Obs [] data;
     foreach(i; iota(20000))
     {
         auto features = [uniform(0.0f, 100.0f), uniform(0.0f, 
100.0f)];
         data ~= Obs(features.sum, features);
     }

     net.learn(data, "square", AdaGrad(epochs, learning_rate), 
verbose, cores);

     auto val = net.predict([50.0f, 200.0f]);
     val.writeln;
     assert(fabs(250.0f - val[0]) < 0.1);
}


More information about the Digitalmars-d-learn mailing list