Write a javascript function
- User
Write a javascript function
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Write a javascript function
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a JavaScript function that implements a simple neural network (a Perceptron) capable of learning basic logical operations like AND, OR, or NAND. This demonstrates core machine learning principles relevant to AGI development:
/**
* Perceptron class implementing a basic neural network unit
* @param {number} learningRate - Adjusts weight update sensitivity (0.1 by default)
*/
class Perceptron {
constructor(learningRate = 0.1) {
this.weights = [];
this.bias = 0;
this.learningRate = learningRate;
}
/**
* Initialize weights randomly
* @param {number} numInputs - Number of input features
*/
initialize(numInputs) {
this.weights = Array.from({length: numInputs},
() => (Math.random() * 2) - 1); // Weights between -1 and 1
this.bias = (Math.random() * 2) - 1;
}
/**
* Activation function (Step function)
* @param {number} value - Weighted sum of inputs
* @returns {number} Binary output (0 or 1)
*/
activate(value) {
return value > 0 ? 1 : 0;
}
/**
* Make prediction based on inputs
* @param {number[]} inputs - Array of input values
* @returns {number} Prediction result (0 or 1)
*/
predict(inputs) {
const weightedSum = inputs.reduce(
(sum, input, i) => sum + input * this.weights[i],
this.bias
);
return this.activate(weightedSum);
}
/**
* Train the perceptron on a single data point
* @param {number[]} inputs - Training inputs
* @param {number} target - Expected output
*/
train(inputs, target) {
const prediction = this.predict(inputs);
const error = target - prediction;
// Update weights and bias
this.weights = this.weights.map(
(weight, i) => weight + this.learningRate * error * inputs[i]
);
this.bias += this.learningRate * error;
}
}
// Example Usage: Training a logical AND gate
const andGate = new Perceptron(0.1);
andGate.initialize(2); // Two inputs
// Training data for AND gate
const trainingData = [
{inputs: [0, 0], target: 0},
{inputs: [0, 1], target: 0},
{inputs: [1, 0], target: 0},
{inputs: [1, 1], target: 1}
];
// Train for 100 epochs
for (let epoch = 0; epoch < 100; epoch++) {
trainingData.forEach(data => {
andGate.train(data.inputs, data.target);
});
}
// Test the trained perceptron
console.log('AND Gate Results:');
console.log([0,0], '=>', andGate.predict([0,0])); // 0
console.log([0,1], '=>', andGate.predict([0,1])); // 0
console.log([1,0], '=>', andGate.predict([1,0])); // 0
console.log([1,1], '=>', andGate.predict([1,1])); // 1
Initialization:
Activation Function:
Prediction:
Σ(input * weight) + biasTraining:
new weight = old weight + (learning rate * error * input)bias += learning rate * errorLearning Process:
This simple perceptron demonstrates three fundamental AGI-building concepts:
Connectionist Approach:
Learning Mechanisms:
Symbolic Representation:
For actual AGI development, this would need extensions like:
This implementation provides a foundational understanding of how neural computation and learning can form building blocks for more sophisticated AI systems moving toward AGI.