LoginSignup
0
0

More than 5 years have passed since last update.

Toy-Neural-Network-JSでxor

Posted at

概要

Toy-Neural-Network-JSでxor問題やってみた。

写真

image.png

サンプルコード

let nn;
let lr_slider;
let training_data = [{
    inputs: [0, 0],
    outputs: [0]
}, {
    inputs: [0, 1],
    outputs: [1]
}, {
    inputs: [1, 0],
    outputs: [1]
}, {
    inputs: [1, 1],
    outputs: [0]
}];
let sigmoid = new ActivationFunction(x => 1 / (1 + Math.exp(-x)), y => y * (1 - y));
function setup() {
    createCanvas(400, 400);
    nn = new NeuralNetwork(2, 4, 1);
    lr_slider = createSlider(0.01, 0.5, 0.1, 0.01);

}
function draw() {
    background(0);
    for (let i = 0; i < 10; i++) 
    {
        let data = random(training_data);
        nn.train(data.inputs, data.outputs);
    }
    nn.setLearningRate(lr_slider.value());
    let resolution = 10;
    let cols = width / resolution;
    let rows = height / resolution;
    for (let i = 0; i < cols; i++) 
    {
        for (let j = 0; j < rows; j++) 
        {
            let x1 = i / cols;
            let x2 = j / rows;
            let inputs = [x1, x2];
            let y = nn.predict(inputs);
            noStroke();
            fill(y * 255);
            rect(i * resolution, j * resolution, resolution, resolution);
        }
    }
}


成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0