解读MaxPooling1D和GlobalMaxPooling1D的区别

 更新时间:2022年12月17日 09:43:38   作者:zhangztSky  
这篇文章主要介绍了MaxPooling1D和GlobalMaxPooling1D的区别及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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)作用相同

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python第三方库undetected_chromedriver的使用

    Python第三方库undetected_chromedriver的使用

    这篇文章主要给大家介绍了关于Python第三方库undetected_chromedriver的使用方法,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-01-01
  • Pandas过滤dataframe中包含特定字符串的数据方法

    Pandas过滤dataframe中包含特定字符串的数据方法

    今天小编就为大家分享一篇Pandas过滤dataframe中包含特定字符串的数据方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • Python切换pip安装源的方法详解

    Python切换pip安装源的方法详解

    众所周知pip是Python中非常方便易用的安装包管理器,但是在实际安装中,却是非常的慢,该如何解决呢?那么下面这篇文章就给大家介绍了Python切换pip安装源的方法,文中介绍的很详细,对大家学习或者理解具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
    2016-11-11
  • python读取Android permission文件

    python读取Android permission文件

    python解析json文件读取Android permission,同时可以学习到json的知识。
    2013-11-11
  • python脚本实现统计日志文件中的ip访问次数代码分享

    python脚本实现统计日志文件中的ip访问次数代码分享

    这篇文章主要介绍了python脚本实现统计日志文件中的ip访问次数代码分享,注意此脚本只适用ip在每行开头的日志文件,需要的朋友可以参考下
    2014-08-08
  • caffe的python接口caffemodel参数及特征抽取示例

    caffe的python接口caffemodel参数及特征抽取示例

    这篇文章主要介绍了caffe的python接口caffemodel参数及特征抽取示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Django Admin后台添加数据库视图过程解析

    Django Admin后台添加数据库视图过程解析

    这篇文章主要介绍了Django Admin后台添加数据库视图过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • python协程之动态添加任务的方法

    python协程之动态添加任务的方法

    今天小编就为大家分享一篇python协程之动态添加任务的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • Python pass 语句使用示例

    Python pass 语句使用示例

    这篇文章主要介绍了Python pass 语句的使用方法示例,需要的朋友可以参考下
    2014-03-03
  • python3使用迭代生成器实现减少内存占用

    python3使用迭代生成器实现减少内存占用

    这篇文章主要介绍了python3使用迭代生成器实现减少内存占用的相关资料,需要的朋友可以参考下
    2021-05-05

最新评论