JSFiddle - React, Tailwind, and code Playground
by alexml
HTML
<script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script>
<html>
<head>
<meta charset="UTF-8" >
<title></title>
</head>
<body>
</body>
</html>
JavaScript
var options = {
task: "regression",
debug: true,
inputs: ["date"],
outputs: ["price"],
optimizer: "adam",
loss: "meanSquaredError",
layers: [{
type: 'dense',
units: 1,
inputShape: [10048, 1],
activation: 'tanh',
useBias: true,
},
{
type: 'lstm',
units: 1,
inputShape: [1, 1, 1],
activation: 'tanh',
useBias: true,
return_sequences: true,
},
{
type: 'dense',
units: 1,
inputShape: [1],
activation: 'tanh',
useBias: false,
},
],
};
var nn = ml5.neuralNetwork(options);
setData();
async function getData() {
var data = await fetch("https://raw.githubusercontent.com/cryptnotehq/filestorage/main/apple_stock.json");
data = await data.json();
var cleaned = await data.map((entry) => {
var date = entry.Date.split("-");
date = new Date(date[0], date[1], date[2]).getTime();
var result = {
"date": date,
"price": entry.High,
};
return result;
}).filter(result => (result.date != "" || result.date != undefined) && (result.price != "" || result.price != undefined));
return cleaned;
}
async function setData() {
var obj = await getData();
obj.forEach(item => {
var input = {
"date": parseInt(item.date)
};
var output = {
"price": parseInt(item.price)
};
nn.addData(input, output);
});
nn.normalizeData();
train();
}
function train() {
var trainingOptions = {
epochs: 256,
batchSize: 1024,
};
nn.train(trainingOptions, predict);
console.log(nn.data);
}
function predict() {
nn.predict([parseInt(new Date(2020, 10, 17).getTime())]).then((result) => {
console.log(result);
});
//nn.save();
}