解读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实现图像的随机增强变换

    python实现图像的随机增强变换

    这篇文章主要为大家介绍了如何利用pythons制作一个小工具工具,可以实现图像的随机增强变换,可用于分类训练数据的增强,有需要的可以参考下
    2024-11-11
  • Python中如何判断是否为AJAX请求

    Python中如何判断是否为AJAX请求

    在Web开发中,AJAX请求是一种非常常见的与服务器进行数据交互的方式,本文将深度解析如何在Python中判断AJAX请求,有需要的小伙伴可以参考下
    2024-12-12
  • python自动重试第三方包retrying模块的方法

    python自动重试第三方包retrying模块的方法

    retrying是一个python的重试包,可以用来自动重试一些可能运行失败的程序段。这篇文章主要介绍了python自动重试第三方包retrying的方法,需要的朋友参考下吧
    2018-04-04
  • Python count()函数实例详解

    Python count()函数实例详解

    count() 是Python的内置函数,可以「统计」字符串里指定「字符」或指定字符串出现的「次数」,这篇文章主要介绍了Python count()函数详解,需要的朋友可以参考下
    2023-07-07
  • Python中的反射知识点总结

    Python中的反射知识点总结

    在本篇文章里小编给大家整理了一篇关于Python中的反射知识点总结内容,有需要的朋友们可以跟着学习参考下。
    2021-11-11
  • Python自动检测requests所获得html文档的编码

    Python自动检测requests所获得html文档的编码

    这篇文章主要为大家详细介绍了如何通过Python自动检测requests实现获得html文档的编码,文中的示例代码讲解详细,感兴趣的可以了解下
    2024-11-11
  • TensorFlow损失函数专题详解

    TensorFlow损失函数专题详解

    本篇文章主要介绍了TensorFlow损失函数专题详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • python 简单搭建阻塞式单进程,多进程,多线程服务的实例

    python 简单搭建阻塞式单进程,多进程,多线程服务的实例

    下面小编就为大家带来一篇python 简单搭建阻塞式单进程,多进程,多线程服务的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • python自定义分页器的实现

    python自定义分页器的实现

    这篇文章主要介绍了python自定义分页器的实现,通过自定义分页器封装展开主题并对其实用方法简单介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-04-04
  • python人工智能TensorFlow自定义层及模型保存

    python人工智能TensorFlow自定义层及模型保存

    这篇文章主要为大家介绍了python人工智能TensorFlow自定义层及模型保存示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11

最新评论