class App {
constructor() {}
static main() {
const app = new App();
app.trainAndPredict(28, -12);
}
makeAPrediction(num1, num2) {
const app = new App();
return app.trainAndPredict(num1, num2);
}
strongerPrediction(input1, input2) {
const network = new App();
const list = [];
for (let i = 0; i <= 10; i++) {
list.push(network.makeAPrediction(input1, input2));
}
const top3 = list.sort((a, b) => b - a).slice(0, 3);
const averageOfTop3 = top3.reduce((acc, val) => acc + val, 0) / top3.length;
return averageOfTop3;
}
trainAndPredict(num1, num2) {
const data = [
[5000000, 150], // volume, price
[4500000, 120],
[5000, 20],
[6000, 15]
];
const answers = [0.9, 0.8, 0.3, 0.2];
const network500 = new Network(500);
network500.train(data, answers);
const network1000 = new Network(1000);
network1000.train(data, answers);
const prediction = network500.predict(num1, num2);
return prediction;
}
}
class Network {
constructor(epochs, learnFactor = null) {
this.epochs = epochs;
this.learnFactor = learnFactor;
this.neurons = Array(6).fill().map(() => new Neuron());
}
predict(input1, input2) {
return this.neurons[5].compute(
this.neurons[4].compute(
this.neurons[2].compute(input1, input2),
this.neurons[1].compute(input1, input2)
),
this.neurons[3].compute(
this.neurons[1].compute(input1, input2),
this.neurons[0].compute(input1, input2)
)
);
}
train(data, answers) {
let bestEpochLoss = null;
for (let epoch = 0; epoch < this.epochs; epoch++) {
const epochNeuron = this.neurons[epoch % 6];
epochNeuron.mutate(this.learnFactor);
const predictions = data.map(d => this.predict(d[0], d[1]));
const thisEpochLoss = Util.meanSquareLoss(answers, predictions);
if (bestEpochLoss === null) {
bestEpochLoss = thisEpochLoss;
epochNeuron.remember();
} else {
if (thisEpochLoss < bestEpochLoss) {
bestEpochLoss = thisEpochLoss;
epochNeuron.remember();
} else {
epochNeuron.forget();
}
}
}
}
}
class Neuron {
constructor() {
this.random = Math.random;
this.oldBias = this.random() * 2 - 1;
this.bias = this.random() * 2 - 1;
this.oldWeight1 = this.random() * 2 - 1;
this.weight1 = this.random() * 2 - 1;
this.oldWeight2 = this.random() * 2 - 1;
this.weight2 = this.random() * 2 - 1;
}
mutate(learnFactor) {
const changeFactor = learnFactor ? learnFactor * (this.random() * 2 - 1) : this.random() * 2 - 1;
const propertyToChange = Math.floor(this.random() * 3);
if (propertyToChange === 0) {
this.bias += changeFactor;
} else if (propertyToChange === 1) {
this.weight1 += changeFactor;
} else {
this.weight2 += changeFactor;
}
}
forget() {
this.bias = this.oldBias;
this.weight1 = this.oldWeight1;
this.weight2 = this.oldWeight2;
}
remember() {
this.oldBias = this.bias;
this.oldWeight1 = this.weight1;
this.oldWeight2 = this.weight2;
}
compute(input1, input2) {
const preActivation = (this.weight1 * input1) + (this.weight2 * input2) + this.bias;
const output = Util.sigmoid(preActivation);
return output;
}
}
class Util {
static sigmoid(inVal) {
return 1 / (1 + Math.exp(-inVal));
}
static meanSquareLoss(correctAnswers, predictedAnswers) {
const sumSquare = correctAnswers.reduce((acc, val, idx) => {
const error = val - predictedAnswers[idx];
return acc + (error * error);
}, 0);
return sumSquare / correctAnswers.length;
}
}
function executePrediction() {
const num1 = parseInt(document.getElementById('num1').value, 10);
const num2 = parseInt(document.getElementById('num2').value, 10);
const app = new App();
const result = app.trainAndPredict(num1, num2);
document.getElementById('output').textContent = `Prediction Result: ${result}`;
}
function executePrediction() {
const num1 = parseInt(document.getElementById('num1').value, 10);
const num2 = parseInt(document.getElementById('num2').value, 10);
const app = new App();
const predictions = [];
for (let i = 0; i < 10; i++) {
predictions.push(app.trainAndPredict(num1, num2));
}
const top3 = predictions.sort((a, b) => b - a).slice(0, 3);
const averageOfTop3 = top3.reduce((acc, val) => acc + val, 0) / top3.length;
document.getElementById('output').textContent = `Prediction Result: ${averageOfTop3.toFixed(2)}`;
// Update the progress bar
const progressBar = document.getElementById('progressBar');
progressBar.style.width = `${averageOfTop3 * 100}%`;
}