More Related Content Similar to Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12 (20) More from OdessaFrontend (20) Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #1229. Learning flow
1. Get Labeled data
2. Get results for labeled data (Forward
propagation)
3. Compare with expected results (Fe - Fa)
4. If error is big, update weights (Back propagation),
repeat 2-4
5. If error is low (E < 0.001), try on unlabeled data
not cat
cat
29
30. Learning flow
not cat
cat
Input
[ 0.24, 0.56, … 0.567 ],
[ 0.87, 0.122, … 0.78 ],
[ 0.911, 0.796, … 0.24 ],
...
Expected Output
[ 1, 0 ],
[ 0, 1 ],
[ 0, 1 ],
...
30
32. Learning flow
Error = expected - actual
= [ 1, 0 ] - [ 0.23, 0.77 ]
= [0.77, -0.77,]
32
f(∑)
f(∑)
f(∑)
f(∑)
f(∑)
f(∑)
f(∑)
40. Wait! Who is “Tensor”?
[1.0, 2.0, 3.0],
[10.0, 20.0, 30.0]
tensor =
40
41. Wait! Who is “Tensor”?
const b = tf.tensor([[1.0, 2.0, 3.0], [10.0, 20.0, 30.0]]);
const c = tf.tensor2d([[1.0, 2.0, 3.0], [10.0, 20.0, 30.0]]);
const e = tf.tensor2d([[1.0, 2.0], [3.0, 4.0]]);
const f = tf.tensor2d([[5.0, 6.0], [7.0, 8.0]]);
41
42. Ops API
const e = tf.tensor([[1.0, 2.0], [20.0, 30.0]]);
const f = tf.tensor2d([[2.0, 3.0], [10.0, 20.0]]);
const e_plus_f = e.add(f);
const f_plus_e = tf.add(e, f);
const e_sub_f = e.sub(f);
const e_mul_f = e.mul(f);
const square_f = tf.square(f);
42
43. Memory
Dispose
const e = tf.tensor([[1.0, 2.0], [20.0, 30.0]]);
const x_squared = x.square();
x.dispose();
x_squared.dispose();
43
44. Memory
Tidy
const average = tf.tidy(() => {
const y = tf.tensor1d([1.0, 2.0, 3.0, 4.0]);
const z = tf.ones([4]);
return y.sub(z).square().mean();
});
average.print()
44
47. Layers API
import * as tf from '@tensorlowjs/tfjs';
// Build and compile model.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Generate data for training.
const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);
const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);
// Train model
await model.fit(xs, ys, {epochs: 1000}); 47
48. Load pretrained model
import * as tf from '@tensorlowjs/tfjs';
// Load model.
const model = await tf.loadModel('https://foo.bar/model.json');
48
50. Capture from camera
public capture(): Tensor {
return tf.tidy(() => {
const croppedImage = this.cropImage(tf.fromPixels(this.video));
const batchedImage = croppedImage.expandDims(0);
return batchedImage.toFloat().div(tf.scalar(127)).sub(tf.scalar(1));
});
}
50
51. Load pre-trained model
private loadTruncatedMobileNet$(): Observable<Model> {
return Observable.create(observer => {
tf.loadModel(
'https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json'
).then(
(mobilenet: Model) => {
const layer = mobilenet.getLayer('conv_pw_13_relu');
observer.next(tf.model({ inputs: mobilenet.inputs, outputs: layer.output }));
observer.complete();
}
);
});
}
51
52. Create own model
tf.sequential({
layers: [
tf.layers.flatten({
inputShape: this.truncatedMobileNet.outputs[0].shape.slice(1)
}),
tf.layers.dense({
units: Config.NUM_HIDDEN_LAYERS,
activation: 'relu',
kernelInitializer: 'varianceScaling',
useBias: true
}),
tf.layers.dense({
units: Config.NUM_CLASSES,
kernelInitializer: 'varianceScaling',
useBias: false,
activation: 'softmax'
})
]
});
52
53. Train
public train$(): Observable<number> {
return Observable.create((observer: Observer<number>) => {
this.model = this.getModel();
const optimizer = tf.train.adam(Config.LEARNING_RATE);
this.model.compile({
optimizer: optimizer,
loss: 'categoricalCrossentropy'
});
this.actualModelTrain(observer);
});
}
53
// ...
this.model.fit(this.datasetController.savedX,
this.datasetController.savedY, {
batchSize,
epochs: Config.NUM_EPOCHS,
callbacks: {
onBatchEnd: async (batch, logs) => observer.next(logs.loss),
}
})
.then(() => observer.complete())
54. Predict image class
public predictFromCameraCapture$(): Observable<number> {
return Observable.create(observer => {
const predict = () => {
const predictedClass = tf.tidy(() => {
const img = this.webcam.capture();
const embeddings = this.truncatedMobileNet.predict(img);
const predictions: Tensor<Rank> | Tensor<Rank>[] = this.model.predict(embeddings);
return (predictions as Tensor<Rank>).as1D().argMax();
});
predictedClass.data()
.then((data: Int32Array) => observer.next(data[0]))
};
predict();
})
}
54
60. Lazy Roadmap
60
instal ml5.js done!
const classifier = ml5.imageClassifier('MobileNet', onModelReady);
// Make a prediction
let prediction = classifier.predict(img, (err, results) => {
console.log(results);
});