Neural Networks
Expanded Technical Overview.
NeuralNetwork.ID as the identifier of the neural Network.
console.log(neurons.NeuralNetwork.map);
ID will only work with instances of NeuralNetwork
neurons.NeuralNetwork.map will show all instances created.
neuralNetwork.getLayers() - gets the layers in the neural network instance.
for example:
let nn = new neurons.NeuralNetwork();
nn.getLayers();
The neurons.me module provides a framework for creating and managing neurons, neural networks, and layers within neural networks.
Neural Networks
A neural network is a connected group of neurons, typically organized in layers. The neurons.me module represents a neural network with the NeuralNetwork class, which has methods to add layers and get all layers.
A neural network is created by instantiating a new NeuralNetwork.
// Create a new Neural Network
let neuralNetwork = new neurons.NeuralNetwork();
Layers
Layers are subdivisions within a neural network, containing a group of neurons. The neurons.me module represents a layer with the Layer class, which has methods to add neurons, remove neurons, and get neurons.
A layer is created by instantiating a new Layer, and neurons are added to the layer using the addNeuron method.
// Create three layers: input, hidden, and output
let inputLayer = new neurons.Layer();
let hiddenLayer = new neurons.Layer();
let outputLayer = new neurons.Layer();
// Now you can add multiple layers at once:
let layer = new neurons.Layer();
let layer2 = new neurons.Layer();
let layer3 = new neurons.Layer();
let neuralNetwork = new neurons.NeuralNetwork();
neuralNetwork.addLayer(layer, layer2, layer3);
Example Usage
In the provided code, a new neural network is created with three layers. Three neurons are created with the default modules and added to the layers of the neural network.
This framework provides flexibility in creating and organizing neurons, allowing you to customize the structure of your neural network to suit your specific needs. It also maintains a global map of all neurons, providing an overview of all created neurons. This can be useful for tracking and managing neurons in larger, more complex neural network structures.
const neurons = require('neurons.me');
// Create a new Neural Network
let neuralNetwork = new neurons.NeuralNetwork();
// Create three layers: input, hidden, and output
let inputLayer = new neurons.Layer();
let hiddenLayer = new neurons.Layer();
let outputLayer = new neurons.Layer();
// Add three neurons to each layer
for (let i = 0; i < 3; i++) {
let inputNeuron = neurons.createNeuron({ load: neurons.defacto });
let hiddenNeuron = neurons.createNeuron({ load: neurons.defacto });
let outputNeuron = neurons.createNeuron({ load: neurons.defacto });
inputLayer.addNeuron(inputNeuron);
hiddenLayer.addNeuron(hiddenNeuron);
outputLayer.addNeuron(outputNeuron);
}
// Add the layers to the Neural Network
neuralNetwork.addLayer(inputLayer);
neuralNetwork.addLayer(hiddenLayer);
neuralNetwork.addLayer(outputLayer);
// Now you have a simple feedforward neural network with 3 layers
console.log(neuralNetwork);
NeuralNetwork {
layers: [
Layer { neurons: [Map] },
Layer { neurons: [Map] },
Layer { neurons: [Map] }
]
}
The Map Object
The Map objects inside each Layer object contain the neurons that belong to that layer. A Map is a built-in JavaScript object that holds key-value pairs and remembers the original insertion order of the keys. In the case of the Layer object, the key is the processID of the neuron and the value is the neuron object itself.
You can access the Map object and its values in several ways:
getNeuron method: Each Layer object has a getNeuron method that allows you to access a neuron by its processID. For example, if you want to access the neuron with processID 1 in the first layer of the neural network, you would do:
let neuron = neuralNetwork.layers[0].getNeuron(1);
getNeurons method: Each Layer object also has a getNeurons method that returns the Map object with all the neurons in that layer. You can iterate over the neurons in a layer like this:
for (let [processID, neuron] of neuralNetwork.layers[0].getNeurons()) {
console.log(processID, neuron);
}
Directly accessing the Map: You can also directly access the Map object in a Layer via its neurons property. However, this is generally not recommended because it bypasses the layer's interface and potentially allows for the map to be modified directly, which could lead to inconsistencies.
let neuronsMap = neuralNetwork.layers[0].neurons;
for (let [processID, neuron] of neuronsMap) {
console.log(processID, neuron);
}
Please replace the [0] with the index of the layer you want to access. The indices start from 0, so the first layer is at index 0, the second layer at index 1, and so on
Here are a few reasons why this design works well:
Modularity: The design is modular, meaning each part (Module, Neuron, Layer, and NeuralNetwork) is separate and can be developed, tested, and understood independently. This is good for maintainability and scalability.
Encapsulation: Each class hides its internal state and requires interaction through its methods. This is a fundamental principle of object-oriented programming, and it helps ensure that the object maintains a valid state.
Flexibility: The design allows for flexibility in creating neurons with different modules, adding them to different layers, and combining those layers into neural networks. This enables a wide range of configurations.
Ease of Use: The API is quite straightforward, which makes it easy for users to create, manage, and manipulate neurons, layers, and neural networks.
The order of layers
The order in which you create the neurons, layers, and neural network doesn't generally affect the final outcome, but it does influence the structure and readability of your code. Here's a brief comparison:
Creating the Neural Network First
When you create the neural network first, you're effectively setting up the structure of the neural network in advance. This can make your code easier to read and understand, as the structure of the neural network is clearly defined from the start. However, this approach requires you to know in advance how many layers your neural network will have, which may not always be the case, especially in more complex or dynamic scenarios.
Creating the Neurons and Layers First
When you create the neurons and layers first, you have more flexibility to build and modify your neural network dynamically. You can create, modify, and test individual neurons and layers before adding them to the neural network. This approach can be beneficial when you're experimenting with different configurations or when the structure of the neural network needs to change dynamically based on certain conditions.
In practice, you might find yourself using a combination of both approaches. For example, you might create the neural network and some initial layers first, then create and add more neurons and layers as needed. Ultimately, the best approach depends on the specific requirements and constraints of your project.
The order of layers in a neural network is typically determined by the order in which they are added to the network.
When you add a layer to the neural network, it's usually appended to the end of the list of layers. This means the first layer you add will be the input layer, the last layer you add will be the output layer, and any layers you add in between will be hidden layers.
Here's an example of how you might create a neural network with an input layer, two hidden layers, and an output layer:
const neurons = require('neurons.me');
// Create the neurons
const neuron1 = neurons.createNeuron({ weights: [0.1, 0.2, 0.3], bias: 0.4, activationFunction: x => 1 / (1 + Math.exp(-x)) });
const neuron2 = neurons.createNeuron({ weights: [0.3, 0.2, 0.1], bias: 0.3, activationFunction: x => 1 / (1 + Math.exp(-x)) });
const neuron3 = neurons.createNeuron({ weights: [0.2, 0.1, 0.3], bias: 0.2, activationFunction: x => 1 / (1 + Math.exp(-x)) });
const neuron4 = neurons.createNeuron({ weights: [0.1, 0.3, 0.2], bias: 0.1, activationFunction: x => 1 / (1 + Math.exp(-x)) });
// Create the layers and add the neurons
const layer1 = new neurons.Layer();
layer1.addNeuron(neuron1);
const layer2 = new neurons.Layer();
layer2.addNeuron(neuron2);
const layer3 = new neurons.Layer();
layer3.addNeuron(neuron3);
const layer4 = new neurons.Layer();
layer4.addNeuron(neuron4);
// Create the neural network and add the layers
const neuralNetwork = new neurons.NeuralNetwork();
neuralNetwork.addLayer(layer1); // input layer
neuralNetwork.addLayer(layer2); // hidden layer 1
neuralNetwork.addLayer(layer3); // hidden layer 2
neuralNetwork.addLayer(layer4); // output layer
In this example, layer1 is the input layer because it's the first layer added to the neural network, layer2 and layer3 are hidden layers because they're added after the input layer but before the output layer, and layer4 is the output layer because it's the last layer added to the network.