Open iEfimoff opened 7 years ago
if you're defining perceptron with 20 in, and 10 out neurons, your training dataset should match
// input.length === 20 [0..1]
// output.length === 10 [0..1]
You are trying to train it in the wrong way. A neural network is not going to input a whole triangle as you want. You could however teach the neural network the formula:
I don't know if it's the correct one but just study the wikipedia page
If you really want it to output the whole triangle, you could make something as follows (which just memorizes the layers), this is for a 6 layer triangle:
var network = new synaptic.Architect.Perceptron(1, 3, 6);
var trainer = new synaptic.Trainer(network);
// input: layer x divided by total amount of layers
// output: all the numbers of the layer in order, however not contered (there is no middle of 6)
// divide output by 10 because output should be between 0 and 1
var trainingSet = [
{
input: [1 / 6],
output: [1/10,0,0,0,0,0]
},
{
input: [2 / 6],
output: [1/10,1/10,0,0,0,0]
},
{
input: [3 / 6],
output: [1/10,2/10,1/10,0,0,0]
},
{
input: [4 / 6],
output: [1/10,3/10,3/10,1/10,0,0]
},
{
input: [5 / 6],
output: [1/10,4/10,6/10,4/10,1/10,0]
},
{
input: [6 / 6],
output: [1/10,5/10,10/10,10/10,5/10,1/10]
},
];
trainer.train(trainingSet,{
rate: .1,
iterations: 20000,
error: .0005,
shuffle: true,
cost: synaptic.Trainer.cost.MSE
});
for(var i = 0; i < 6; i++){
var result = network.activate([(i+1)/6]);
for(var j = 0; j < result.length; j++){
result[j] = Math.round(result[j] * 10);
}
console.log('Layer ', i+1, ' results ', result);
}
Note that the above code is not very useful. I advise you to try to teach the neural network to actually memorize the formula so you could input the layer n and receive that layers numbers.
Thing is you're trying to build model, which is answering "what will this triangle look like", however, it will not work in that way. This triangle is, actually
(height: number) => OrderedGraph<number>
,
and inside this fn - there is something like
(height, top = triangle(height - 1)) => height <= 1 ? [1] : [...top, computeLayer(height, top)]
. Also, layer internals (in computeLayer
) is also function of height and index within this layer.
So, you probably would like to predict the value inside some specific cell, and fill array with this.
This means your model should predict the following function: (layer: number, positionInLayer: number) => number
.
If you decompose the NN purpose in that way, keeping in mind that NN is, actually "scalar | fixed array | fixed n-dimensional array in, scalar | fixed array | fixed n-dimensional array out" type, everything will get extremely simple.
https://en.wikipedia.org/wiki/Pascal's_triangle
I've never used neural network before.