-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBaseAIML.js
186 lines (167 loc) · 7.67 KB
/
BaseAIML.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* @License Starts
*
* Copyright © 2015 - present. MongoExpUser
*
* License: MIT - See: https://github.com/MongoExpUser/Shale-Reservoir-DNN-and-Drilling-Rare-Events-Graph/blob/master/README.md
*
* @License Ends
*
*
* Ecotert's BaseAIML.js (released as open-source under MIT License) implements the BASE class for constructing AIML applications.
*
* AIML applications (e.g Shale reservoir production prediction, drilling rare events prediction, rock image classification, etc.)
*
* inherits or extends the BASE class (BaseAIML) for their implementations.
*
* The BaseAIML class contains implementation of the following foundational functions for classification, regression and graph-based methods:
*
* 1) Modules loading
*
* 2) Results predictions, based on already saved, newly trained or evolving real-rime model
*
* 3) Formatting and printing of results to console in easily readable format
*
* 4) Run duration timing
*
* 5) etc i.e. other helpers or foundational functions are added as deem necessary
*
*
*/
class BaseAIML
{
constructor(modelingOption, fileOption, gpuOption, inputFromCSVFileX, inputFromCSVFileY, mongDBCollectionName, mongDBSpecifiedDataX, mongDBSpecifiedDataY)
{
this.modelingOption = modelingOption;
this.fileOption = fileOption;
this.gpuOption = gpuOption;
this.inputFromCSVFileX = inputFromCSVFileX;
this.inputFromCSVFileY = inputFromCSVFileY;
this.mongDBCollectionName = mongDBCollectionName;
this.mongDBSpecifiedDataX = mongDBSpecifiedDataX;
this.mongDBSpecifiedDataY = mongDBSpecifiedDataY;
}
commonModules(gpuOption)
{
const fs = require('fs');
const path = require('path');
const util = require('util');
const mongodb = require('mongodb');
const assert = require('assert');
let tf = require('@tensorflow/tfjs'); //pure JavaScript version
//replace pure JavaScript version with c/c++ back-end version, if node.js exist (is installed)
const cmdExists = require('command-exists').sync;
const nodeJsExist = cmdExists('node');
if(nodeJsExist === true)
{
console.log("================================================================>");
console.log("Node.js exists (installed successfully) on this machine.");
console.log("================================================================>");
switch(gpuOption)
{
case(true):
tf = require('@tensorflow/tfjs-node-gpu'); //c/c++ binding, gpu option
console.log("================================================================>");
console.log("Swaping pure JavaScript version with GPU version of TensorFlow");
console.log("================================================================>");
break;
case(false || null || undefined):
tf = require('@tensorflow/tfjs-node'); //c/c++ binding, cpu option
console.log("================================================================>");
console.log("Swaping pure JavaScript version with CPU version of TensorFlow");
console.log("================================================================>");
break;
}
}
return {fs:fs, path:path, util:util, mongodb:mongodb, assert:assert, tf:tf, model:tf.sequential()};
}
predictProductionAndPrintResults(_x, _y, _compiledModel, existingSavedModel=false)
{
//begin prediction: use the model to do inference on data points
var beginPredictingTime = new Date();
var predictY = _compiledModel.predict(_x);
//print "train" input and output tensors summary
if(existingSavedModel === false || existingSavedModel === null || existingSavedModel === undefined)
{
console.log("Input train tensor/data summary in JS and TF formats: ");
console.log("======================================================");
console.log("Input Train Properties: ")
console.log(_x);
console.log("Input Train Values: ");
_x.print(false);
console.log("======================================================");
//
console.log("Output train tensor/data summary in JS and TF formats:");
console.log("======================================================");
console.log("Output Train Properties: ")
console.log(_y);
console.log("Output Train Values: ")
_y.print(false);
console.log("======================================================");
console.log();
}
//print "test" output: expected vs actual
if(_y.dtype === "float32")
{
console.log("Expected 'test' output result in Tensor format: ");
console.log("======================================================");
console.log(_y.name);
console.log("Expected Test Values: ");
_y.print(false);
console.log("======================================================");
}
//
console.log("Actual 'test' output result in Tensor format: ")
console.log("======================================================");
if(_y.dtype === "float32")
{
predictY.name = _y.name;
console.log(predictY.name);
}
else
{
console.log("Output = Check_Assigned_Name_And_Unit");
}
//
console.log("Actual Test Values: ");
predictY.print(false);
console.log();
//print summary & prediction time
new BaseAIML().runTimeDNN(beginPredictingTime, "Predicting Time");
console.log("Final Model Summary");
_compiledModel.summary();
console.log();
}
predictProductionAndPrintResultsBasedOnExistingSavedModel(_x, _y, tf, pathToExistingSavedTrainedModel)
{
//load/open saved mode and re-use for predicting without training again
const loadModel = tf.loadLayersModel(pathToExistingSavedTrainedModel)
//load
loadModel.then(function(existingModel)
{
console.log();
console.log("........Prediction from loaded model Begins........................");
//then predict and print results
const bam = new BaseAIML();
var existingSavedModel = undefined;
bam.predictProductionAndPrintResults(_x, _y, existingModel, existingSavedModel=true);
console.log("........Prediction from loaded model Ends..........................");
console.log();
});
}
runTimeDNN(beginTime, timeOption)
{
console.log("========================================================>")
console.log(timeOption, " (seconds): ", (new Date() - beginTime)/1000);
console.log("=========================================================>")
}
getTensor(csvFileArrayOutput)
{
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
const inputDim = csvFileArrayOutput.length;
const inputSize = csvFileArrayOutput[0].length;
const csvFileArrayOutputToTensor = tf.tensor2d(csvFileArrayOutput);
return {csvFileArrayOutputToTensor:csvFileArrayOutputToTensor, inputDim:inputDim, inputSize:inputSize};
}
}
module.exports = { BaseAIML };