網(wǎng)站首頁 編程語言 正文
使用tf.keras.MaxPooling1D出現(xiàn)錯誤
錯誤如下
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
:一個字符串。padding的方法:string,valid或same,大小寫不敏感。 -
data_format
:一個字符串,channels_last(默認(rèn))或channels_first中的一個,輸入中維度的排序,channels_last對應(yīng)于具有形狀(batch, length, channels)的輸入,而channels_first對應(yīng)于具有形狀(batch, channels, length)的輸入。 -
name
:一個字符串,表示層的名稱。
出現(xiàn)錯誤原因
是圖片通道的問題,也就是”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)錯誤的代碼中補(bǔ)充一個參數(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)于給每一個樣本每列的最大值
而MaxPool1D就是普通的對每一個樣本進(jìn)行一個窗口(1D是一維列窗口)滑動取最大值。
總結(jié)
原文鏈接:https://blog.csdn.net/IMWTJ123/article/details/111505651
相關(guān)推薦
- 2022-05-17 ubuntu No package ‘libzmq‘ found
- 2022-04-18 pyinstaller打包后,配置文件無法正常讀取的解決_python
- 2022-10-01 sql語法中的concat()函數(shù)詳解_MsSql
- 2023-02-15 Python函數(shù)常見幾種return返回值類型_python
- 2022-10-24 React中父子組件通信詳解_React
- 2022-02-18 Redis - Redis command timed out nested exception i
- 2022-10-04 C語言實(shí)現(xiàn)倒置字符串的兩種方法分享_C 語言
- 2022-05-26 openwrt安裝docker并啟動的操作方法_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支