Thread overview
Vectorflow noob
Aug 10
Jiyan
Aug 11
Michael
Aug 11
BitR
August 10
Hey,
wanted to the following simple thing with vectorflow:

I want to develop a simple MLP, which has 2 input neurons and one output neuron.
The network should simply add the input values together, so [1,2] predicts [3] i guess.
I started in a newbish way to build the following code:

import vectorflow;


struct Obs // The represeneted data
{
	float label; // Did i get that right that label would be the DESIRED output (3=1+2)
	float []features; // The features are the input i guess, so features = f.e. [1,2]
}

void main()
{
	
	auto net = NeuralNet()
	    .stack(DenseData(2))
	    .stack(Linear(10));   // Is this the right way to construct the Net?
	
	// The training data
	Obs []data;
	
	
	data.length = 10;
	
	import std.random;
	import std.algorithm;
	foreach(ref Obs n; data)
	{
		// The features are getting fille with random numbers between 0.5 and 5
		// The label becomes the sum of feature[0] and feature[1]
		n.features.length = 2;
		n.features[0] = uniform(0.5, 5);
		n.features[1] = uniform(0.5, 5);
		
		n.label = n.features.sum;
		writeln(n.features[0], " ", n.features[1], " ", n.label);
		assert (n.label == n.features[0] + n.features[1]);
	}
	
	net.learn(data, "logistic", AdaGrad(10, 0.1, 500), true, 3);
	
	auto val = net.predict(data[0]); // is this wrong?
	val.writeln;
}

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

I'm worried there might not be many on the forums who can help too much with vectorflow given how new it is. Maybe some in the community are more familiar with neural nets and have played wit vectorflow already, but I'm not sure. I hope somebody can drop in to give you a hand.
August 11
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);
}