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是一维列窗口)滑动取最大值。

参考:maxpooling

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于 TextCNN 和 LSTM 的招聘简历自动筛选系统的 Python 代码示例: ```python import numpy as np from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, Conv1D, GRU, CuDNNGRU, CuDNNLSTM, BatchNormalization from tensorflow.keras.layers import Bidirectional, GlobalMaxPool1D, MaxPooling1D, Add, Flatten from tensorflow.keras.layers import GlobalAveragePooling1D, GlobalMaxPooling1D, concatenate, SpatialDropout1D from tensorflow.keras.models import Model, load_model from tensorflow.keras import initializers, regularizers, constraints, optimizers, layers, callbacks from tensorflow.keras import backend as K from tensorflow.keras.engine import InputSpec, Layer from tensorflow.keras.optimizers import Adam # 读取数据 resumes = [] labels = [] with open("resumes.txt", "r") as f: for line in f: resumes.append(line.strip().split("\t")[0]) labels.append(int(line.strip().split("\t")[1])) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(resumes, labels, test_size=0.2, random_state=42) # 数据预处理 max_features = 20000 maxlen = 100 tokenizer = Tokenizer(num_words=max_features) tokenizer.fit_on_texts(X_train) X_train = tokenizer.texts_to_sequences(X_train) X_test = tokenizer.texts_to_sequences(X_test) x_train = pad_sequences(X_train, maxlen=maxlen) x_test = pad_sequences(X_test, maxlen=maxlen) # 构建模型 def build_model(): inp = Input(shape=(maxlen,)) x = Embedding(max_features, 128)(inp) x = SpatialDropout1D(0.2)(x) x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x) y = Bidirectional(CuDNNGRU(64, return_sequences=True))(x) avg_pool1 = GlobalAveragePooling1D()(y) max_pool1 = GlobalMaxPooling1D()(y) conc = concatenate([avg_

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值