2025-08-29 22:07:59 -04:00

59 lines
1.6 KiB
JavaScript

document.addEventListener('readystatechange', event => {
if (event.target.readyState === "interactive") {
highlightFightHistory();
let train = false;
if (train) {
setTimeout(() => {
trainTheBrain();
}, 5000);
}
}
}, { passive: true });
function highlightFightHistory() {
let historyTable = document.querySelector('table.cols-2');
if (!historyTable) {
return;
}
const fighterName = document.querySelector('#fighter h1').innerText;
let winnerCols = historyTable.querySelectorAll('tbody tr td:last-child');
for (let winnerCol of winnerCols) {
if (winnerCol.innerText == fighterName) {
winnerCol.classList.add("win");
}
else {
winnerCol.classList.add("loss");
}
}
}
function trainTheBrain() {
const config = {
iterations: 500,
log: true,
logPeriod: 100,
binaryThresh: 0.5,
hiddenLayers: [40, 20], // array of ints for the sizes of the hidden layers in the network
activation: 'sigmoid', // supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'],
learningRate: 0.05,
momentum: 0.2,
decayRate: 0.999,
};
// create a simple feed-forward neural network with backpropagation
const net = new brain.NeuralNetwork(config);
// Get contents from http://localhost/api/v1/training-data
fetch("/api/v1/training-data").then(res => res.json()).then(data => {
obj = data
})
.then(() => {
console.log("Training...");
net.train(obj);
localStorage.setItem('neuralNetwork', JSON.stringify(net.toJSON()));
console.log("Neural Network stored in LocalStorage");
});
}