Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFJS example of tf.layers.globalAveragePooling2d()

I am creating my first algorithm in TFJS Layers by translating this tutorial https://colab.research.google.com/drive/1lWUGZarlbORaHYUZlF9muCgpPl8pEvve#scrollTo=nhYhP30NKlAp

Can I get an example of globalAveragePooling2d in TFJS? Thank you

this.model = tf.sequential();
this.model.add(tf.layers.conv2d({
  inputShape: [224, 224 , 3],
  kernelSize: 3,
  activation: 'relu',
  filters: 8
}));
this.model.add(
  tf.layers.maxPooling2d({poolSize: 3})
);
this.model.add(tf.layers.conv2d({
  inputShape: [16, 16],
  kernelSize: 3,
  activation: 'relu',
  filters: 8
}));
this.model.add(
  tf.layers.maxPooling2d({poolSize: 3})
);

// How to use this?
this.model.add(tf.layers.globalAveragePooling2d());

this.model.add(tf.layers.timeDistributed(
  {layer: tf.layers.dense({units: this.outputCount})}));
this.model.add(tf.layers.activation({activation: 'softmax'}));
this.model.compile({
  loss: 'categoricalCrossentropy',
  optimizer: 'sgd',
  metrics: ['accuracy']
});
like image 815
Eddie Avatar asked Dec 22 '25 02:12

Eddie


1 Answers

tf.layers.timeDistributed cannot be used after tf.layers.globalAveragePooling2d for the first return a 2d layer whereas the latter expects at least a 3d layer. They can be combined only if the layer shape is reshaped before.

Currently an empty object needs to be passed to tf.layers.globalAveragePooling2d, for it tries to access arg.name. It will throw an error if arg is not an object

let model = tf.sequential();
model.add(tf.layers.conv2d({
  inputShape: [224, 224 , 3],
  kernelSize: 3,
  activation: 'relu',
  filters: 8
}));
model.add(
  tf.layers.maxPooling2d({poolSize: 3})
);
model.add(tf.layers.conv2d({
  inputShape: [16, 16],
  kernelSize: 3,
  activation: 'relu',
  filters: 8
}));
model.add(
  tf.layers.maxPooling2d({poolSize: 3})
);
// How to use this?
model.add(tf.layers.globalAveragePooling2d({}));

/*model.add(tf.layers.timeDistributed(
  {layer: tf.layers.dense({units: 3, activation: 'softmax'})}));*/
model.compile({
  loss: 'categoricalCrossentropy',
  optimizer: 'sgd',
  metrics: ['accuracy']
});

model.summary()
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
  </head>

  <body>
  </body>
</html>
like image 143
edkeveked Avatar answered Dec 24 '25 23:12

edkeveked



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!