解读MaxPooling1D和GlobalMaxPooling1D的区别
短信预约 -IT技能 免费直播动态提醒
MaxPooling1D和GlobalMaxPooling1D区别
import tensorflow as tf
from tensorflow import keras
input_shape = (2, 3, 4)
x = tf.random.normal(input_shape)
print(x)
y=keras.layers.GlobalMaxPool1D()(x)
print("*"*20)
print(y)
'''
"""Global average pooling operation for temporal data.
Examples:
>>> input_shape = (2, 3, 4)
>>> x = tf.random.normal(input_shape)
>>> y = tf.keras.layers.GlobalAveragePooling1D()(x)
>>> print(y.shape)
(2, 4)
Arguments:
data_format: A string,
one of `channels_last` (default) or `channels_first`.
The ordering of the dimensions in the inputs.
`channels_last` corresponds to inputs with shape
`(batch, steps, features)` while `channels_first`
corresponds to inputs with shape
`(batch, features, steps)`.
Call arguments:
inputs: A 3D tensor.
mask: Binary tensor of shape `(batch_size, steps)` indicating whether
a given step should be masked (excluded from the average).
Input shape:
- If `data_format='channels_last'`:
3D tensor with shape:
`(batch_size, steps, features)`
- If `data_format='channels_first'`:
3D tensor with shape:
`(batch_size, features, steps)`
Output shape:
2D tensor with shape `(batch_size, features)`.
"""
'''
print("--"*20)
input_shape = (2, 3, 4)
x = tf.random.normal(input_shape)
print(x)
y=keras.layers.MaxPool1D(pool_size=2,strides=1)(x) # strides 不指定 默认等于 pool_size
print("*"*20)
print(y)
输出如下图
上图GlobalMaxPool1D 相当于给每一个样本每列的最大值
而MaxPool1D就是普通的对每一个样本进行一个窗口(1D是一维列窗口)滑动取最大值。
tf.keras.layers.GlobalMaxPool1D()
与tf.keras.layers.Conv1D的输入一样,输入一个三维数据(batch_size,feature_size,output_dimension)
x = tf.constant([[1., 2., 3.], [4., 5., 6.]])
x = tf.reshape(x, [2, 3, 1])
max_pool_1d=tf.keras.layers.GlobalMaxPooling1D()
max_pool_1d(x)
其中max_pool_1d(x)和tf.math.reduce_max(x,axis=-2,keepdims=False)作用相同
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341