简体   繁体   English

ValueError:层“max_pooling2d”的输入 0 与层不兼容:预期 ndim=4,发现 ndim=5。 收到的完整形状:(无、3、51、39、32)

[英]ValueError: Input 0 of layer "max_pooling2d" is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: (None, 3, 51, 39, 32)

I have two different problems occurs at the same time.我有两个不同的问题同时发生。

I am having dimensionality problems with MaxPooling2d and having same dimensionality problem with DQNAgent.我有 MaxPooling2d 的维度问题,并且有 DQNAgent 的相同维度问题。

The thing is, I can fix them seperately but cannot at the same time.问题是,我可以单独修复它们,但不能同时修复。

First Problem第一个问题

I am trying to build a CNN network with several layers.我正在尝试构建一个具有多层的 CNN 网络。 After I build my model, when I try to run it, it gives me an error.在我构建 model 后,当我尝试运行它时,它给了我一个错误。

!pip install PyOpenGL==3.1.* PyOpenGL-accelerate==3.1.*
!pip install tensorflow gym keras-rl2 gym[atari] keras pyvirtualdisplay 

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Convolution2D, MaxPooling2D, Activation
from keras_visualizer import visualizer 
from tensorflow.keras.optimizers import Adam
env = gym.make('Boxing-v0')
height, width, channels = env.observation_space.shape
actions = env.action_space.n
input_shape = (3, 210, 160, 3)   ## input_shape = (batch_size, height, width, channels)
def build_model(height, width, channels, actions):
  model = Sequential()
  model.add(Convolution2D(32, (8,8), strides=(4,4), activation="relu", input_shape=input_shape, data_format="channels_last"))
  model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_last"))
  model.add(Convolution2D(64, (4,4), strides=(1,1), activation="relu"))
  model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_last"))
  model.add(Convolution2D(64, (3,3), activation="relu"))
  model.add(Flatten())
  model.add(Dense(512, activation="relu"))
  model.add(Dense(256, activation="relu"))
  model.add(Dense(actions, activation="linear"))
  return model
model = build_model(height, width, channels, actions)

It gives below error:它给出以下错误:

ValueError: Input 0 of layer "max_pooling2d_12" is incompatible with the layer: expected ndim=4, found ndim=5. ValueError:层“max_pooling2d_12”的输入 0 与层不兼容:预期 ndim=4,发现 ndim=5。 Full shape received: (None, 3, 51, 39, 32)收到的完整形状:(无、3、51、39、32)

Second Problem第二个问题

My input_shape is (3, 210, 160, 3) .我的input_shape(3, 210, 160, 3) I am using the first 3 on purpose due to I have to specify the batch_size before.我故意使用前 3 个,因为我必须在之前指定batch_size If I do not specify it before and pass it as (210, 160, 3) to the build_model function, below build_agent function gives me an another error:如果我之前没有指定它并将其作为(210, 160, 3)传递给build_model function,在build_agent function 下面会给我另一个错误:

def build_agent(model, actions):
  policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr="eps", value_max=1., value_min=.1, value_test=.2, nb_steps=10000)
  memory = SequentialMemory(limit=1000, window_length=3)
  dqn = DQNAgent(model=model, memory=memory, policy=policy,
                 enable_dueling_network=True, dueling_type="avg",
                 nb_actions=actions, nb_steps_warmup=1000)
  return dqn
dqn = build_agent(model, actions)
dqn.compile(Adam(learning_rate=1e-4))

dqn.fit(env, nb_steps=10000, visualize=False, verbose=1)

ValueError: Error when checking input: expected conv2d_11_input to have 4 dimensions, but got array with shape (1, 3, 210, 160, 3) ValueError:检查输入时出错:预期 conv2d_11_input 有 4 个维度,但得到了形状为 (1, 3, 210, 160, 3) 的数组

Deleting batch size number in the model construction phase, removes the MaxPooling2D incompatibility error but throws DQNAgent dimensionality error.在 model 构建阶段删除批大小号,消除 MaxPooling2D 不兼容错误但会引发 DQNAgent 维度错误。 Adding the batch size to the model construction phase removes DQNAgent dimensionality error but throws the MaxPooling2D incompatibility error.将批量大小添加到 model 构造阶段会消除 DQNAgent 维度错误,但会引发 MaxPooling2D 不兼容错误。

I am really stucked.我真的被困住了。

Issue is with input_shape.问题在于 input_shape。 input_shape=input_shape[1:]输入形状=输入形状[1:]

Working sample code工作示例代码

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Convolution2D, MaxPooling2D, Activation
from tensorflow.keras.optimizers import Adam

input_shape = (3, 210, 160, 3)

model = Sequential()
model.add(Convolution2D(32, (8,8), strides=(4,4), activation="relu", input_shape=input_shape[1:], data_format="channels_last"))
model.add(MaxPooling2D(pool_size=(2,2), data_format="channels_last"))
model.add(Convolution2D(64, (4,4), strides=(1,1), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_last"))
model.add(Convolution2D(64, (3,3), activation="relu"))
model.add(Flatten())
model.add(Dense(512, activation="relu"))
model.add(Dense(256, activation="relu"))
model.add(Dense(2, activation="linear"))

model.summary()

Output Output

Model: "sequential_7"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_9 (Conv2D)           (None, 51, 39, 32)        6176      
                                                                 
 max_pooling2d_5 (MaxPooling  (None, 25, 19, 32)       0         
 2D)                                                             
                                                                 
 conv2d_10 (Conv2D)          (None, 22, 16, 64)        32832     
                                                                 
 max_pooling2d_6 (MaxPooling  (None, 11, 8, 64)        0         
 2D)                                                             
                                                                 
 conv2d_11 (Conv2D)          (None, 9, 6, 64)          36928     
                                                                 
 flatten_1 (Flatten)         (None, 3456)              0         
                                                                 
 dense_4 (Dense)             (None, 512)               1769984   
                                                                 
 dense_5 (Dense)             (None, 256)               131328    
                                                                 
 dense_6 (Dense)             (None, 2)                 514       
                                                                 
=================================================================
Total params: 1,977,762
Trainable params: 1,977,762
Non-trainable params: 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 层 max_pooling2d 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=5。 收到完整形状:[无、4、10、8、32] - Input 0 of layer max_pooling2d is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 4, 10, 8, 32] ValueError: 层 max_pooling1d 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、128、1、32) - ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32) ValueError:层“max_pooling1d”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到完整形状:(无,51644、29、32) - ValueError: Input 0 of layer "max_pooling1d" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 51644, 29, 32) ValueError:global_average_pooling2d 层的输入 0 与该层不兼容:预期 ndim=4,发现 ndim=2。 收到的完整形状:[无,128] - ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128] Keras 尺寸错误 - (层“max_pooling2d”的输入 0 与层不兼容:预期 ndim=4,发现 ndim=6。) - Keras Dimension error - (Input 0 of layer "max_pooling2d" is incompatible with the layer: expected ndim=4, found ndim=6.) 层 conv1d_39 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:(无,64) - Input 0 of layer conv1d_39 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 64) ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[None, 32, 32] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32] ValueError: 层 lstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,18] - ValueError : Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 18] ValueError: 层序的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,1) - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1) ValueError:层顺序的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[32, 64, 3] - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 64, 3]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM