使用tf.keras.MaxPooling1D出现错误问题及解决
作者:IMWTJ 时间:2021-02-07 05:01:28
使用tf.keras.MaxPooling1D出现错误
错误如下
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(默认)或channels_first中的一个,输入中维度的排序,channels_last对应于具有形状(batch, length, channels)的输入,而channels_first对应于具有形状(batch, channels, length)的输入。name
:一个字符串,表示层的名称。
出现错误原因
是图片通道的问题,也就是”channels_last”和”channels_first”数据格式的问题。
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"
}
法二:在运行代码前面加两行代码:
from keras import backend as K
K.set_image_dim_ordering('tf')
我的处理方法
直接在出现错误的代码中补充一个参数,加上data_format='channels_first'就可以啦,,
pool_4 = MaxPooling1D(pool_size=2, name='pool_4',data_format='channels_first')(conv_4)
注:此方法适用MaxPooling2D
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是一维列窗口)滑动取最大值。
来源:https://blog.csdn.net/IMWTJ123/article/details/111505651
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
开发Web应用程序的结构化过程
![](https://img.aspxhome.com/file/UploadPic/20096/1/01-47s.png)
微信小程序实现日期时间筛选器
![](https://img.aspxhome.com/file/2023/0/56000_0s.jpg)
数字人组件反写[asp组件开发实例1]
![](https://img.aspxhome.com/file/UploadPic/20096/9/vb-34s.gif)
Python字符串hashlib加密模块使用案例
Python的爬虫包Beautiful Soup中用正则表达式来搜索
利用PyQt5模拟实现网页鼠标移动特效
![](https://img.aspxhome.com/file/2023/9/92009_0s.png)
Python标准库之typing的用法(类型标注)
![](https://img.aspxhome.com/file/2023/0/65100_0s.png)
Python如何测试stdout输出
Django自定义模板过滤器和标签的实现方法
![](https://img.aspxhome.com/file/2023/0/71370_0s.jpg)
python实现图像边缘检测
![](https://img.aspxhome.com/file/2023/9/64339_0s.jpg)
SQL SQL Server 2008中的新日期数据类型
python中使用iterrows()对dataframe进行遍历的实例
![](https://img.aspxhome.com/file/2023/0/77790_0s.jpg)
TensorFlow深度学习之卷积神经网络CNN
![](https://img.aspxhome.com/file/2023/5/91885_0s.jpg)
Python实现的爬虫刷回复功能示例
![](https://img.aspxhome.com/file/2023/3/87943_0s.jpg)
SQL Server服务器内存升级后的烦恼
MySQL五个查询优化方法
14 个Python小游戏 源码分享
![](https://img.aspxhome.com/file/2023/9/76079_0s.gif)
Python实现的简单计算器功能详解
![](https://img.aspxhome.com/file/2023/1/64361_0s.jpg)
谈谈网页一屏有多大?
![](https://img.aspxhome.com/file/UploadPic/200712/21/2007122112356981s.jpg)
opencv实现图像缩放效果
![](https://img.aspxhome.com/file/2023/8/92188_0s.jpg)