Get started with synaptic CDN

MIT licensed

Synaptic is a lightweight library for building and training artificial neural networks.

Tags:
  • neural network
  • machine learning
  • long short term memory
  • perceptron
  • architecture free

Stable version

Copied!

How to start using synaptic CDN


// Include Synaptic library via CDN
const synaptic = require('synaptic');

// Create input layer with 2 neurons
const inputLayer = new synaptic.LayerSize(2);

// Create hidden layer with 4 neurons
const hiddenLayer = new synaptic.LayerSize(4);

// Create output layer with 1 neuron
const outputLayer = new synaptic.LayerSize(1);

// Create network with input, hidden, and output layers
const network = new synaptic.Network();
network.addLayer(inputLayer);
network.addLayer(hiddenLayer);
network.addLayer(outputLayer);

// Initialize weights randomly
network.initWeights();

// Add sigmoid activation function for hidden and output layers
network.sigmoid();

// Connect input layer to hidden layer
const inputToHidden = new synaptic.Connection({
  input: inputLayer,
  output: hiddenLayer,
});
network.addConnection(inputToHidden);

// Connect hidden layer to output layer
const hiddenToOutput = new synaptic.Connection({
  input: hiddenLayer,
  output: outputLayer,
});
network.addConnection(hiddenToOutput);

// Set input values for XOR operation
network.setInput([0, 0]);
network.compute();
const xorResult1 = network.output[0];

// Set input values for XOR operation
network.setInput([0, 1]);
network.compute();
const xorResult2 = network.output[0];

// Set input values for XOR operation
network.setInput([1, 0]);
network.compute();
const xorResult3 = network.output[0];

// Set input values for XOR operation
network.setInput([1, 1]);
network.compute();
const xorResult4 = network.output[0];

console.log('XOR results:');
console.log(`0 XOR 0 = ${xorResult1}`);
console.log(`0 XOR 1 = ${xorResult2}`);
console.log(`1 XOR 0 = ${xorResult3}`);
console.log(`1 XOR 1 = ${xorResult4}`);
Copied!
Copied!

All versions