https://www.tensorflow.org/js/
https://github.com/tensorflow/tfjs
https://www.tensorflow.org/js/tutorials
TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models.
APIs:
TensorFlow.js Core, a flexible low-level API for neural networks and numerical computation.TensorFlow.js Layers, a high-level API which implements functionality similar to Keras.TensorFlow.js Data, a simple API to load and prepare data analogous to tf.data.TensorFlow.js Converter, tools to import a TensorFlow SavedModel to TensorFlow.jsTensorFlow.js Vis, in-browser visualization for TensorFlow.js modelsTensorFlow.js AutoML, Set of APIs to load and run models produced by AutoML Edge.Backends/Platforms:
TensorFlow.js CPU Backend, pure-JS backend for Node.js and the browser.TensorFlow.js WebGL Backend, WebGL backend for the browser.TensorFlow.js WASM Backend, WebAssembly backend for the browser.TensorFlow.js WebGPU, WebGPU backend for the browser.TensorFlow.js Node, Node.js platform via TensorFlow C++ adapter.TensorFlow.js React Native, React Native platform via expo-gl adapter.<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
<!-- Place your code in the script tag below. You can also use an external .js file -->
<script>
// Notice there is no 'import' statement. 'tf' is available on the index-page
// because of the script tag above.
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
model.predict(tf.tensor2d([5], [1, 1])).print();
});
</script>
</head>
<body>
</body>
</html>
Add TensorFlow.js to your project using yarn or npm.
Note: Because we use ES2017 syntax (such as import), this workflow assumes you are using a modern browser or a bundler/transpiler to convert your code to something older browsers understand. See our examples to see how we use Parcel to build our code. However, you are free to use any build tool that you prefer.
import * as tf from '@tensorflow/tfjs';
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
model.predict(tf.tensor2d([5], [1, 1])).print();
});
Tensors are the core datastructure of TensorFlow.js They are a generalization of vectors and matrices to potentially higher dimensions.
We have utility functions for common cases like Scalar, 1D, 2D, 3D and 4D tensors, as well a number of functions to initialize tensors in ways useful for machine learning.
Creates a tf.Tensor with the provided values, shape and dtype. Parameters:
values
| TypedArray | Array |
TypedArray.Uint8Array[].shape
Optionaldtype
| ‘float32’ | ‘int32’ | ‘bool’ | ‘complex64’ | ‘string’ |
OptionalReturns: tf.Tensor
// Pass an array of values to create a vector.
tf.tensor([1, 2, 3, 4]);
// Pass a nested array of values to make a matrix or a higher
// dimensional tensor.
tf.tensor([[1, 2], [3, 4]])
// Pass a flat array and specify a shape yourself.
tf.tensor([1, 2, 3, 4], [2, 2])
Creates rank-0 tf.Tensor (scalar) with the provided value and dtype.
values
| number | boolean | string | Uint8Array |
dtype
| ‘float32’ | ‘int32’ | ‘bool’ | ‘complex64’ | ‘string’ |
OptionalReturns: tf.Scalar
tf.scalar(3.14);
텐서들은 기본적으로 메모리 공간에 따로 저장되고, 따라서 비동기로 동작하게 됨. 위의 tf.Tensor.data()가 Promise를 리턴하는 것을 보면 알 수 있음.
const values = [];
for (let i = 0; i < 30; i++) {
values[i] = Math.random(0, 100);
}
const shape = [2, 5, 3];
const ts = tf.tensor3d(values, shape, 'int32');
ts.data().then(data => {
console.log(data); // -> Int32Array(30)
});
위 예제에서 보듯, 실제 데이터의 저장은 3D tensor임에도 1D로 저장됨을 알 수 있음. 그냥 출력을 원하면 ts.print()를 사용하면 콘솔 창에서 3차원 배열 형태로 볼 수 있음. 혹은 ts.dataSync()를 사용할 수도 있음.
메모리를 해제하고 싶다면 ts.dispose()와 같은 식으로 할당을 해제할 수 있음.
텐서는 기본적으로 immutable이며, 값을 바꾸고 싶다면 tf.Variable을 사용한다.
const tv = ts.variable(0);
Similar to Keras (Keras API를 모델로 함)
You can architect NeuralNetwork using Layers API.
https://www.tensorflow.org/js/guide/models_and_layers
모델은 입력을 출력에 매핑하는 학습 가능한 매개변수가 있는 함수.TensorFlow.js는 기계 학습 모델을 만드는 두 가지 방법을 제공
Layers API 사용tf.matMul()이나 tf.add() 등과 같은 하위 수준 연산과 함께 Core API 사용순차형 모델과 기능적 모델 두 가지가 있음 // tf.sequential({layers: [inputLayers, outputLayers, ...]})
const model = tf.sequential({
layers: [
tf.layers.dense({inputShape: [784], units: 32, activation: 'relu'}),
tf.layers.dense({units: 10, activation: 'softmax'}),
]
});
// tf.sequential().add()
const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [784], units: 32, activation: 'relu'}));
model.add(tf.layers.dense({units: 10, activation: 'softmax'}));
inputShape가 필요inputShape에 배치 크기는 제외 // Create an arbitrary graph of layers,
// by connecting them via the apply() method.
const input = tf.input({shape: [784]});
const dense1 = tf.layers.dense({units: 32, activation: 'relu'}).apply(input);
const dense2 = tf.layers.dense({units: 10, activation: 'softmax'}).apply(dense1);
const model = tf.model({inputs: input, outputs: dense2});
tf.model() 함수를 통해 직접 생성tf.apply()를 이용해 위 순차형과 동일한 모델 구조 생성inputShape 대신 tf.input()으로 input layer를 정의함LayersModel 클래스의 인스턴스임LayersModel은 데이터가 레이어를 통과할 때 자동 형상 추론을 수행함.model.summary()를 호출하면 아래와 같은 모델 요약 정보를 알 수 있음
LayersModel을 사용하면 모델의 저장과 로드를 할 수 있음
const saveResult = await model.save('localstorage://my-model-1');
const model = await tf.loadLayersModel('localstorage://my-model-1');
class SquaredSumLayer extends tf.layers.Layer {
constructor() {
super({});
}
// In this case, the output is a scalar.
computeOutputShape(inputShape) { return []; }
// call() is where we do the computation.
call(input, kwargs) { return input.square().sum();}
// Every layer needs a unique name.
getClassName() { return 'SquaredSum'; }
}
const t = tf.tensor([-2, 1, 0, 5]);
const o = new SquaredSumLayer().apply(t);
o.print(); // prints 30
// The weights and biases for the two dense layers.
const w1 = tf.variable(tf.randomNormal([784, 32]));
const b1 = tf.variable(tf.randomNormal([32]));
const w2 = tf.variable(tf.randomNormal([32, 10]));
const b2 = tf.variable(tf.randomNormal([10]));
function model(x) {
return x.matMul(w1).add(b1).relu().matMul(w2).add(b2).softmax();
}
tf.Variable()과 input tensor를 사용.