- Code to experiment with machine learning using Tensorflow.js, starting with recognising numbers. Tutorial code from Fireship (see ref in 'Inspiration') cloned then updated to Angular 13. All dependencies updated. Requires some fixes to work, due to updates. The plan is to use this to try new Tensorflow ideas.
- Note: to open web links in a new window use: ctrl+click on link
- "TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models."
- Angular v13
- @tensorflow/tfjs v3, javascript version of Tensorflow
- ng2-charts v3
-
npm i
to install dependenciesy -
ng serve
then navigate to port 4200.
- tba
async trainNewModel() {
// Define a model for linear regression.
this.linearModel = tf.sequential();
this.linearModel.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
this.linearModel.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Training data, completely random stuff
const xs = tf.tensor1d([3.2, 4.4, 5.5, 6.71, 6.98, 7.168, 9.779, 6.182, 7.59, 2.16, 7.042, 10.71, 5.313, 7.97, 5.654, 9.7, 3.11]);
const ys = tf.tensor1d([1.6, 2.7, 2.9, 3.19, 1.684, 2.53, 3.366, 2.596, 2.53, 1.22, 2.87, 3.45, 1.65, 2.904, 2.42, 2.4, 1.31]);
// Train
await this.linearModel.fit(xs, ys)
console.log('model trained')
}
- to follow
- Status: not compiling due to errors from tensorflow update to v3
- To-Do: Correct: Namespace has no exported member, loadModel does not exist.., fromPixels does not exist.
- This project is licensed under the terms of the MIT license.
- Repo created by ABateman, email: gomezbateman@yahoo.com