I am trying to implement a deep learning model in the browser and this requires porting some custom layers, one of them is an instant layer normalization. Below the piece of code that is supposed to work but it's a bit old. I get this error:
Uncaught (in promise) ReferenceError: initializer is not defined at InstantLayerNormalization.build
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
<script>
class InstantLayerNormalization extends tf.layers.Layer
{
static className = 'InstantLayerNormalization';
epsilon = 1e-7
gamma;
beta;
constructor(config)
{
super(config);
}
getConfig()
{
const config = super.getConfig();
return config;
}
build(input_shape)
{
let shape = tf.tensor(input_shape);
// initialize gamma
self.gamma = self.add_weight(shape=shape,
initializer='ones',
trainable=true,
name='gamma')
// initialize beta
self.beta = self.add_weight(shape=shape,
initializer='zeros',
trainable=true,
name='beta')
}
call(inputs){
mean = tf.math.reduce_mean(inputs, axis=[-1], keepdims=True)
variance = tf.math.reduce_mean(tf.math.square(inputs - mean), axis=[-1], keepdims=True)
std = tf.math.sqrt(variance + self.epsilon)
outputs = (inputs - mean) / std
outputs = outputs * self.gamma
outputs = outputs + self.beta
return outputs
}
static get className() {
console.log(className);
return className;
}
}
tf.serialization.registerClass(InstantLayerNormalization);
</script>