cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              const ROWS = 6, COLS = 4;
var grid = [
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0
];

var trained = false, training_started = false;

const network = new brain.NeuralNetwork();

function setup() {
  createCanvas(300,300);
  background(0);
  fill(255);
  textSize(24);
  text("Click to train\nthe Neural Network", 10, 100);
}

function draw() {
  if (trained) {
    draw_main();
  } else if (!training_started && mouseIsPressed) {
    training_started = true;
    background(255); fill(0);
    text("Training...", 100, 100);
    training();
  }
}

function training() {
  network.trainAsync([
    { input: [1,1,1,1, 1,0,0,1, 1,0,0,1, 1,0,0,1, 1,0,0,1, 1,1,1,1], output: { 0: 1 } },
    { input: [0,0,0,0, 0,1,1,1, 1,0,0,1, 1,0,0,1, 1,1,1,1, 0,0,0,0], output: { 0: 1 } },
    { input: [0,0,0,0, 1,1,1,0, 1,0,1,1, 1,0,0,1, 1,1,1,1, 0,1,1,0], output: { 0: 1 } },
    { input: [0,0,0,0, 1,1,1,0, 1,0,1,0, 1,0,1,0, 1,0,1,0, 1,1,1,0], output: { 0: 1 } },
    { input: [0,0,0,0, 0,1,0,0, 1,0,1,0, 1,0,1,0, 1,0,1,0, 0,1,0,0], output: { 0: 1 } },
    { input: [0,1,1,1, 0,1,0,1, 0,1,0,1, 0,1,1,1, 0,0,0,0, 0,0,0,0], output: { 0: 1 } },
    { input: [0,1,1,0, 1,0,0,1, 1,0,0,1, 1,0,0,1, 1,0,0,1, 0,1,1,0], output: { 0: 1 } },
    { input: [0,0,0,0, 0,1,1,1, 0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,1,1], output: { 0: 1 } },
    { input: [0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0], output: { 1: 1 } },
    { input: [1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0], output: { 1: 1 } },
    { input: [0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,0,0], output: { 1: 1 } },
    { input: [1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0], output: { 1: 1 } },
    { input: [0,1,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,1,1,1], output: { 1: 1 } },
    { input: [0,0,1,1, 0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1], output: { 1: 1 } },
    { input: [0,1,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0], output: { 1: 1 } },
    { input: [0,0,0,0, 0,1,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0], output: { 1: 1 } },
    { input: [0,0,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0], output: { 1: 1 } },
    { input: [0,1,1,0, 1,0,0,1, 0,0,0,1, 0,0,1,0, 0,1,0,0, 1,1,1,1], output: { 2: 1 } },
    { input: [1,1,1,1, 1,0,0,1, 0,0,0,1, 1,1,1,1, 1,0,0,0, 1,1,1,1], output: { 2: 1 } },
    { input: [0,0,0,0, 1,1,1,0, 0,0,1,0, 0,1,0,0, 1,0,0,0, 1,1,1,1], output: { 2: 1 } },
    { input: [1,1,1,0, 0,0,1,1, 0,0,0,1, 0,1,1,1, 1,0,0,0, 1,1,1,1], output: { 2: 1 } },
    { input: [0,0,0,0, 0,1,1,0, 0,0,1,0, 0,1,1,0, 0,1,0,0, 0,1,1,0], output: { 2: 1 } },
    { input: [1,1,1,0, 0,0,1,0, 1,1,1,0, 1,0,0,0, 1,1,1,0, 0,0,0,0], output: { 2: 1 } },
    { input: [1,1,1,1, 0,0,0,1, 1,1,1,0, 1,0,0,0, 1,0,0,0, 1,1,1,1], output: { 2: 1 } },
    { input: [1,1,1,0, 0,0,0,1, 0,0,0,1, 0,0,1,0, 0,0,0,1, 1,1,1,1], output: { 3: 1 } },
    { input: [1,1,1,1, 0,0,0,1, 0,1,1,1, 0,0,0,1, 0,0,0,1, 1,1,1,1], output: { 3: 1 } },
    { input: [0,0,0,0, 1,1,1,1, 0,0,0,1, 0,1,1,1, 0,0,0,1, 1,1,1,1], output: { 3: 1 } },
    { input: [1,1,1,0, 1,0,1,0, 0,0,1,0, 1,1,1,0, 0,0,1,0, 1,1,1,0], output: { 3: 1 } },
    { input: [0,1,1,1, 0,0,0,1, 0,1,1,1, 0,0,0,1, 0,1,1,1, 0,0,0,0], output: { 3: 1 } },
    { input: [1,1,1,0, 0,0,1,0, 1,1,1,0, 0,0,1,0, 1,1,1,0, 0,0,0,0], output: { 3: 1 } },
    { input: [0,1,1,1, 0,0,0,1, 0,1,1,1, 0,0,0,1, 0,0,0,1, 0,1,1,1], output: { 3: 1 } },
    { input: [1,1,0,0, 0,1,0,0, 1,1,0,0, 0,1,0,0, 0,1,0,0, 1,1,0,0], output: { 3: 1 } },

  ]).then(res => {
    console.log("Finished training");
    trained = true;
  });
}

function draw_main() {
  var guess;
  background(100);
  fill(255);
  var button_x = 120, button_y = 200, button_w = 60, button_h = 30
  rect(button_x, button_y, button_w, button_h);
  if (mouseIsPressed && mouseX > button_x && mouseX < button_x + button_w && mouseY > button_y && mouseY < button_y + button_h) {
    for (var row=0; row<ROWS; row++) {
      for (var col=0; col<COLS; col++) {
        grid[row * COLS + col] = 0;
      }
    }
  }
  fill(0);
  noStroke();
  textSize(18);
  text("Reset", button_x + 5, button_y + 20);
  stroke(0);
  strokeWeight(1);
  fill(255);
  var start_x = 110, start_y = 50, box_size = 20;
  
    for (var row=0; row<ROWS; row++) {
      for (var col=0; col<COLS; col++) {
      if (grid[row * COLS + col] == 1) {
        fill(0);
      } else {
        fill(255);
      }
      rect(start_x + box_size * col,
           start_y + box_size * row,
           box_size, box_size);
    }
  }
  if (mouseX > start_x && mouseX < start_x + box_size * COLS
   && mouseY > start_y && mouseY < start_y + box_size * ROWS
   && mouseIsPressed) {
    noStroke();
    fill(0,255,0);
    var row = Math.floor((mouseY - start_y) / box_size);
    var col = Math.floor((mouseX - start_x) / box_size);
    grid[row * COLS + col] = 1;
    var output = network.run(grid);
  }
  
  fill(255);
  noStroke();
  var guess = brain.likely(grid, network);
  text("guess: " + guess, 120, 250);
}
            
          
!
999px
Loading ..................

Console