tf.layers.globalAveragePooling2d() 的 TFJS 示例
TFJS example of tf.layers.globalAveragePooling2d()
我正在通过翻译本教程在 TFJS Layers 中创建我的第一个算法
https://colab.research.google.com/drive/1lWUGZarlbORaHYUZlF9muCgpPl8pEvve#scrollTo=nhYhP30NKlAp
我可以在 TFJS 中获得 globalAveragePooling2d 的示例吗?谢谢
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']
});
tf.layers.timeDistributed
不能在 tf.layers.globalAveragePooling2d
之后用于第一个 return 2d 层,而后者至少需要 3d 层。只有在之前对图层形状进行整形后才能组合它们。
目前需要将一个空对象传递给 tf.layers.globalAveragePooling2d
,因为它试图访问 arg.name
。如果 arg 不是对象,它将抛出错误
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/tfjs@1.7.2"> </script>
</head>
<body>
</body>
</html>
我正在通过翻译本教程在 TFJS Layers 中创建我的第一个算法 https://colab.research.google.com/drive/1lWUGZarlbORaHYUZlF9muCgpPl8pEvve#scrollTo=nhYhP30NKlAp
我可以在 TFJS 中获得 globalAveragePooling2d 的示例吗?谢谢
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']
});
tf.layers.timeDistributed
不能在 tf.layers.globalAveragePooling2d
之后用于第一个 return 2d 层,而后者至少需要 3d 层。只有在之前对图层形状进行整形后才能组合它们。
目前需要将一个空对象传递给 tf.layers.globalAveragePooling2d
,因为它试图访问 arg.name
。如果 arg 不是对象,它将抛出错误
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/tfjs@1.7.2"> </script>
</head>
<body>
</body>
</html>