I try to predict stock prices with a LSTM layer.
Here's the code:
var options = {
task: "regression",
debug: true,
inputs: ["date", "timevalue"],
outputs: ["price", "timevalue"],
layers: [
{
type: 'lstm',
units: 1,
inputShape: [10048, 2],
activation: 'tanh',
useBias: true,
return_sequences: true,
},
{
type: 'dense',
units: 1,
inputShape: [1],
activation: 'tanh',
useBias: true,
},
],
};
var nn = ml5.neuralNetwork(options);
setData();
async function getData(){
var data = await fetch("apple_stock.json");
data = await data.json();
var cleaned = await data.map( (entry, i) => {
var date = entry.Date.split("-");
date = new Date(date[0],date[1],date[2]).getTime();
var result = {
"date": date,
"price": entry.High,
"tval": i,
};
return result;
}).filter( result => (result.date != "" || result.date != undefined) && (result.price != "" || result.price != undefined) );
return cleaned;
}
async function setData() {
var obj = await getData();
var tval = 1;
obj.forEach(item => {
var input = { "date": parseInt(item.date), "timevalue": ++item.tval };
var output = { "price": parseInt(item.price), "timevalue": item.tval };
nn.addData(input, output);
});
console.log(obj);
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();
console.log("end");
}
I expect it to start the model training but instead it doesn't do anything. I don't get any error outputs on the console, also the data from the json is loaded correctly. Can someone help?
I tried to play around with different values for inputShape
but it didn't help. I know for sure that all the code is running from the start to the end.