-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrencyPredictionTS2.js
More file actions
52 lines (44 loc) · 1.58 KB
/
currencyPredictionTS2.js
File metadata and controls
52 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Script 3
const tf = require('@tensorflow/tfjs');
// Load the data. This should be an array of objects, with each object representing a data point
// and having properties for the various features you want to use for prediction (e.g. 'open', 'close', etc.)
const data = require('./data.json');
// Split the data into training and test sets
const trainTestSplit = tf.util.createTrainTestSplit(data, 0.8);
const trainData = trainTestSplit.train;
const testData = trainTestSplit.test;
// Convert the data to tensors
const inputs = trainData.map((datum) => [
datum.open,
datum.close,
datum.high,
datum.low,
datum.volume,
datum.marketCap,
]);
const labels = trainData.map((datum) => datum.close);
const inputTensor = tf.tensor2d(inputs);
const labelTensor = tf.tensor1d(labels);
// Create the model
const model = tf.sequential();
model.add(tf.layers.dense({units: 32, inputShape: [6], activation: 'relu'}));
model.add(tf.layers.dense({units: 16, activation: 'relu'}));
model.add(tf.layers.dense({units: 1, activation: 'linear'}));
// Compile the model
model.compile({optimizer: 'adam', loss: 'meanSquaredError'});
// Train the model
await model.fit(inputTensor, labelTensor, {epochs: 100});
// Test the model
const testInputs = testData.map((datum) => [
datum.open,
datum.close,
datum.high,
datum.low,
datum.volume,
datum.marketCap,
]);
const testLabels = testData.map((datum) => datum.close);
const testInputTensor = tf.tensor2d(testInputs);
const testLabelTensor = tf.tensor1d(testLabels);
const testResults = model.evaluate(testInputTensor, testLabelTensor);
console.log(testResults);