-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai.js
297 lines (254 loc) · 9.07 KB
/
ai.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
console.log('ai.js loaded');
(async function(){
const ai={}
ai.created_at=Date.now()
ai.getIris = async function(){
return (await fetch('https://episphere.github.io/ai/data/iris.json')).json()
}
ai.getCars = async function(){
return (await fetch('https://storage.googleapis.com/tfjs-tutorials/carsData.json')).json()
}
ai.getScript=async function(url){
url=url||'https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js'
return new Promise(function(resolve, reject) {
let s = document.createElement('script')
s.src=url
s.onload=resolve
s.onerror=reject
document.head.appendChild(s)
})
}
ai.codeLabCars=async function(div){ // https://codelabs.developers.google.com/codelabs/tfjs-training-regression
// Plotly so we have a persistent UI
ai.codeLabIris.div=div||document.getElementById('codeLabCarsDiv')||document.createElement('div')
ai.codeLabIris.div.innerHTML='<p>Loading data ...</p>'
ai.codeLabCars.data=await (await fetch('https://storage.googleapis.com/tfjs-tutorials/carsData.json')).json()
ai.codeLabIris.div.innerHTML='<div id="codeLabCarsDataPlot" style="width:500;height:500"></div>'
ai.codeLabIris.divDataPlot=ai.codeLabIris.div.querySelector('#codeLabCarsDataPlot')
// ploting MPG vs horsepower uing tfvis
let trace = {
x:ai.codeLabCars.data.map(d=>d.Horsepower),
y:ai.codeLabCars.data.map(d=>d.Miles_per_Gallon),
type:'scatter',
mode:'markers',
}
let layout={
title:'MPG vs HorsePower',
xaxis: {
title: 'Horsepower'
},
yaxis: {
title: 'Miles_per_Gallon'
}
}
ai.plot(ai.codeLabIris.divDataPlot,[trace],layout)
// plot it also with tfVis
let values = ai.codeLabCars.data.map(c=>{return{
//debugger
x:c.Horsepower,
y:c.Miles_per_Gallon
}})
tfvis.render.scatterplot(
{name:'MPG vs HorsePower'},
{values},
{
xLabel: 'Horsepower',
yLabel: 'MPG',
height: 300
}
)
// Model architecture
// https://codelabs.developers.google.com/codelabs/tfjs-training-regression/#3
const model = ai.codeLabCars.createModel();
tfvis.show.modelSummary({name: 'Model Summary'}, model);
// https://codelabs.developers.google.com/codelabs/tfjs-training-regression/#5
// Convert the data to a form we can use for training.
const tensorData = ai.codeLabCars.convertToTensor(ai.codeLabCars.data)
const {inputs, labels} = tensorData;
// Train the model
await ai.codeLabCars.trainModel(model, inputs, labels);
console.log('Done Training');
return ai.codeLabIris.div // in case this is a module being required in another env, such as an observable notebook
}
ai.codeLabCars.createModel=function() {
// https://codelabs.developers.google.com/codelabs/tfjs-training-regression/#3
// Create a sequential model
const model = tf.sequential();
// Add a single hidden layer
model.add(tf.layers.dense({inputShape: [1], units: 1, useBias: true}));
// Add an output layer
model.add(tf.layers.dense({units: 1, useBias: true}));
return model;
}
ai.codeLabCars.convertToTensor=function(data) {
// Wrapping these calculations in a tidy will dispose any
// intermediate tensors.
return tf.tidy(() => { // clean memory use https://js.tensorflow.org/api/0.11.7/#tidy
// Step 1. Shuffle the data
tf.util.shuffle(data);
// Step 2. Convert data to Tensor
const inputs = data.map(d => d.horsepower)
const labels = data.map(d => d.mpg);
const inputTensor = tf.tensor2d(inputs, [inputs.length, 1]);
const labelTensor = tf.tensor2d(labels, [labels.length, 1]);
//Step 3. Normalize the data to the range 0 - 1 using min-max scaling
const inputMax = inputTensor.max();
const inputMin = inputTensor.min();
const labelMax = labelTensor.max();
const labelMin = labelTensor.min();
const normalizedInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin));
const normalizedLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin));
return {
inputs: normalizedInputs,
labels: normalizedLabels,
// Return the min/max bounds so we can use them later.
inputMax,
inputMin,
labelMax,
labelMin,
}
});
}
ai.codeLabCars.trainModel = async function (model, inputs, labels) {
// Prepare the model for training.
model.compile({
optimizer: tf.train.adam(),
loss: tf.losses.meanSquaredError,
//metrics: ['mse'], // <-- needed?
});
const batchSize = 32; // <-- discuss how this could lead to a federated learning implementation
const epochs = 50;
return await model.fit(inputs, labels, {
batchSize,
epochs,
shuffle: true,
callbacks:{
onEpochEnd: async (epoch,logs) => {
console.log("Epoch: "+epoch+" Loss: "+logs.loss)
await tf.nextFrame()
}
}
/*
callbacks: tfvis.show.fitCallbacks(
{ name: 'Training Performance' },
['loss', 'mse'],
//['loss'],
{
height: 200,
callbacks: ['onEpochEnd']
}
)
*/
});
}
ai.codeLabIris=async function(div){
ai.codeLabIris.div=div||document.getElementById('codeLabIrisDiv')||document.createElement('div')
ai.codeLabIris.div.innerHTML='<div id="codeLabIrisMsg"><p>iris data loading ...</p></div>'
ai.codeLabIris.data=await (await fetch('https://episphere.github.io/ai/data/iris.json')).json()
ai.codeLabIris.parms = Object.keys(ai.codeLabIris.data[0]).slice(0,-1)
let n = ai.codeLabIris.parms.length
ai.codeLabIris.table=document.createElement('table')
ai.codeLabIris.div.appendChild(ai.codeLabIris.table)
// wrangle the data
let sp={};ai.codeLabIris.data.forEach(d=>{
if(!sp[d.species]){sp[d.species]=0}
sp[d.species]+=1
})
// for each species generate a trace
ai.codeLabIris.speciesData={}
ai.codeLabIris.species=Object.keys(sp)
ai.codeLabIris.species.forEach(s=>{
ai.codeLabIris.speciesData[s]=ai.codeLabIris.data.filter(d=>(d.species==s))
})
for(var i = 0; i<n ; i++){
let tr = document.createElement('tr')
ai.codeLabIris.table.appendChild(tr)
for(var j=0 ; j<n ; j++){
let td = document.createElement('td')
let div = document.createElement('div')
div.i=i
div.j=i
div.id=`${i}_${j}`
//div.innerHTML=`div(${i},${j})`
// preparind dt for ploting
if(j<i){
let traces = []
ai.codeLabIris.species.forEach(s=>{
traces.push({
x:ai.codeLabIris.speciesData[s].map(d=>d[ai.codeLabIris.parms[i]]),
y:ai.codeLabIris.speciesData[s].map(d=>d[ai.codeLabIris.parms[j]]),
type:'scatter',
mode:'markers',
name: s
})
})
// debugger
// ai.plot(div)
// style="width:600px;height:250px;"
div.style.width=400
div.style.height=400
td.appendChild(div)
tr.appendChild(td)
layout={
xaxis: {
title: ai.codeLabIris.parms[j]
},
yaxis: {
title: ai.codeLabIris.parms[i]
}
}
ai.plot(div,traces,layout)
}
}
}
//return ai.codeLabIris.table
setTimeout(_=>{codeLabIrisMsg.innerHTML=''},2000)
return ai.codeLabIris.div
}
ai.cleanIrisData
ai.plot=async function(div,traces,layout){
if(typeof(Plotly)=='undefined'){
await ai.getScript('https://cdn.plot.ly/plotly-latest.min.js')
}
div=div||document.createElement('div')
traces=traces||[{x: [1, 2, 3, 4, 5],y: [1, 2, 4, 8, 16]}]
layout=layout||{margin: { t: 0 } };
Plotly.plot(div,traces,layout)
return div
}
if(typeof(window)=='object'){ // regular web browser application
window.onload=async function(){
await ai.getScript('https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js')
ai.tf=tf
await ai.getScript('https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tfjs-vis.umd.min.js')
ai.tfvis=tfvis
window.ai=ai
// check if intro is in order
console.log('window loaded')
try{
document.getElementById('codeLabCars').disabled=false
document.getElementById('codeLabCars').click() // run https://codelabs.developers.google.com/codelabs/tfjs-training-regression if available
}catch (err){
}
//ai.codeLab()
//define(ai)
}
}
if(typeof(define)!=='undefined'){ // loaded as a required object
define(ai)
}
})()
// MIS
/*
(async function(){
cars = await ai.getCars()
trace={
x:[],
y:[]
}
cars.filter(c=>c.Miles_per_Gallon).forEach((c,i)=>{
trace.x[i]=c.Horsepower;
trace.y[i]=c.Miles_per_Gallon
})
})()
*/