使用tf.keras.MaxPooling1D出現(xiàn)錯(cuò)誤問題及解決
使用tf.keras.MaxPooling1D出現(xiàn)錯(cuò)誤
錯(cuò)誤如下
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'pool_2/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,32].
首先了解MaxPooling1D
tf.layers.max_pooling1d( ? ? inputs, ? ? pool_size, ? ? strides, ? ? padding='valid', ? ? data_format='channels_last', ? ? name=None )
用于1維輸入的MaxPooling層
pool_size:表示pooling window的大小strides:指定pooling操作的步長padding:一個(gè)字符串。padding的方法:string,valid或same,大小寫不敏感。data_format:一個(gè)字符串,channels_last(默認(rèn))或channels_first中的一個(gè),輸入中維度的排序,channels_last對應(yīng)于具有形狀(batch, length, channels)的輸入,而channels_first對應(yīng)于具有形狀(batch, channels, length)的輸入。name:一個(gè)字符串,表示層的名稱。
出現(xiàn)錯(cuò)誤原因
是圖片通道的問題,也就是”channels_last”和”channels_first”數(shù)據(jù)格式的問題。
input_shape=(3,28,28)是theano的寫法,而tensorflow需要寫出:(28,28,3)
其他人的處理方法
查了很多方法我的問題都沒有解決:
法一:配置.keras下的keras.json文件,將channels_last修改為channels_first
{
"image_data_format" : "channels_first",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}法二:在運(yùn)行代碼前面加兩行代碼:
from keras import backend as K ?
K.set_image_dim_ordering('tf')?我的處理方法
直接在出現(xiàn)錯(cuò)誤的代碼中補(bǔ)充一個(gè)參數(shù),加上data_format='channels_first'就可以啦,,
pool_4 = MaxPooling1D(pool_size=2, name='pool_4',data_format='channels_first')(conv_4)
注:此方法適用MaxPooling2D
MaxPooling1D和GlobalMaxPooling1D區(qū)別
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 不指定 默認(rèn)等于 pool_size
print("*"*20)
print(y)
輸出如下圖 上圖GlobalMaxPool1D 相當(dāng)于給每一個(gè)樣本每列的最大值

而MaxPool1D就是普通的對每一個(gè)樣本進(jìn)行一個(gè)窗口(1D是一維列窗口)滑動(dòng)取最大值。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python如何實(shí)現(xiàn)遠(yuǎn)程方法調(diào)用
這篇文章主要介紹了Python如何實(shí)現(xiàn)遠(yuǎn)程方法調(diào)用,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08
解決Python設(shè)置函數(shù)調(diào)用超時(shí),進(jìn)程卡住的問題
今天小編就為大家分享一篇解決Python設(shè)置函數(shù)調(diào)用超時(shí),進(jìn)程卡住的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧詳解(3)
這篇文章主要為大家詳細(xì)介紹了Python中的棧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Python利用Selenium進(jìn)行網(wǎng)頁自動(dòng)化與動(dòng)態(tài)內(nèi)容抓取操作
Selenium是一個(gè)自動(dòng)化測試工具,它允許開發(fā)者模擬用戶的瀏覽器行為,執(zhí)行各種交互操作,下面就跟隨小編一起了解下如何使用Python和Selenium進(jìn)行網(wǎng)頁自動(dòng)化與動(dòng)態(tài)內(nèi)容抓取吧2025-03-03
Python 稀疏矩陣-sparse 存儲(chǔ)和轉(zhuǎn)換
這篇文章主要介紹了Python 稀疏矩陣-sparse 存儲(chǔ)和轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2017-05-05
Python?格式化輸出字符串的方法(輸出字符串+數(shù)字的幾種方法)
字符串格式化輸出是python非常重要的基礎(chǔ)語法,這篇文章主要介紹了Python?格式化輸出字符串(輸出字符串+數(shù)字的幾種方法)的方法,需要的朋友可以參考下2023-03-03

