-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjonas.js
59 lines (49 loc) · 1.98 KB
/
jonas.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
console.log('jonas.js loaded')
jonas=function(){
console.log('jonas fun initialized at '+Date())
}
jonas.getIris=async function(){ // await jonas.getIris() will place data at jonas.irisData and also return it
jonas.irisData=await (await fetch('https://episphere.github.io/ai/data/iris.json')).json()
jonas.irisData=jonas.dt2tab(jonas.irisData)
console.log('iris data retrieved and also stored at jonas.irisData')
return jonas.irisData
}
jonas.getTrainTest=async function(dt,p){ // returns [xtrain, ytrain, ytrain, ytest] with a 0-1 sampling fraction
dt = dt || jonas.irisData
if(!dt){
dt = await jonas.getIris()
}
debugger
return '[xtrain, ytrain, ytrain, ytest]'
}
jonas.dt2tab=function(dt){ // creates a tabular data frame
dt=dt || jonas.irisData
const attrs = Object.keys(dt[0])
const tb={parmsIn:attrs.slice(0,-1)}
tb.x=dt.map(x=>{
return tb.parmsIn.map(lb=>x[lb])
})
tb.parmOut=Object.keys(dt[0]).slice(-1)[0]
tb.y=dt.map(x=>x[tb.parmOut])
tb.labels=[...new Set(tb.y)] // unique labels
tb.y=tb.y.map(y=>tb.labels.map(yi=>(yi==y)))
return tb
}
jonas.layers=async function(){ // testing layers as keras
// https://www.tensorflow.org/js/guide/layers_for_keras_users
// Build and compile model.
const model = tf.sequential();
model.add(tf.layers.dense({units: 2, inputShape: [1]}));
//model.add(tf.layers.dense({units: 2, inputShape: [2]}));
model.add(tf.layers.dense({units: 1, inputShape: [2]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([[1], [2], [3], [4], [5]], [5, 1]);
const ys = tf.tensor2d([[1], [3], [5], [6], [6.5]], [5, 1]);
// Train model with fit().
await model.fit(xs, ys, {epochs: 1000});
// Run inference with predict().
model.predict(tf.tensor2d([[1],[1.5],[2],[2.5],[3],[3.5],[4],[4.5],[5]], [9, 1])).print();
//debugger
jonas.model=model
}