JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
<div>Open your console to see the actions :)))</div>
CSS
div {
font-size: 64px;
font-family: sans serif;
font-weight: bold;
}
JavaScript
/**
* Define a custom layer.
*
* This layer performs the following simple operation:
* output = input * (x ^ alpha);
* - x is a trainable scalar weight.
* - alpha is a configurable constant.
*
* This custom layer is written in a way that can be saved and loaded.
*/
class TimesXToThePowerOfAlphaLayer extends tf.layers.Layer {
constructor(config) {
super(config);
this.alpha = config.alpha;
}
/**
* build() is called when the custom layer object is connected to an
* upstream layer for the first time.
* This is where the weights (if any) are created.
*/
build(inputShape) {
const shape = []; // Because our weight (x) is scalar.
this.x = this.addWeight('x', shape, 'float32', tf.initializers.ones());
}
/**
* call() contains the actual numerical computation of the layer.
*
* It is "tensor-in-tensor-out". I.e., it receives one or more
* tensors as the input and should produce one or more tensors as
* the return value.
*
* Be sure to use tidy() to avoid WebGL memory leak.
*/
call(input) {
return tf.tidy(() => {
const k = tf.pow(this.x.read(), this.alpha);
return tf.add(input[0], k);
});
}
/**
* getConfig() generates the JSON object that is used
* when saving and loading the custom layer object.
*/
getConfig() {
const config = super.getConfig();
Object.assign(config, {alpha: this.alpha});
return config;
}
/**
* The static className getter is required by the
* registration step (see below).
*/
static get className() {
return 'TimesXToThePowerOfAlphaLayer';
}
}
/**
* Regsiter the custom layer, so TensorFlow.js knows what class constructor
* to call when deserializing an saved instance of the custom layer.
*/
tf.serialization.registerClass(TimesXToThePowerOfAlphaLayer);
(async function main() {
const input = tf.input({shape: [4]});
let x;
x = tf.layers.dense({units: 1, inputShape: [4]}).apply(input);
// Here...