简体   繁体   English

ValueError:检查输入时出错:预期 Input_input 有 4 个维度,但得到了形状为 (1, 1, 2) 的数组

[英]ValueError: Error when checking input: expected Input_input to have 4 dimensions, but got array with shape (1, 1, 2)

I am trying to create a Flappy Bird AI with Convolutional Layers and Dense Layers, but at the "Train" step (Function fit()) I get the following error message:我正在尝试使用卷积层和密集层创建 Flappy Bird AI,但在“训练”步骤(函数拟合())我收到以下错误消息:

dqn.fit(env, nb_steps=500000, visualize=False, verbose=2)

Training for 500000 steps ...
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-53-e21cf8798454> in <module>()
----> 1 dqn.fit(env, nb_steps=500000, visualize=False, verbose=2) #fit = training, training for 5 Mio, timesteps eig bei 5000000
      2 #value's which are important: episode reward, mean reward

7 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training_utils_v1.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    634                            ': expected ' + names[i] + ' to have ' +
    635                            str(len(shape)) + ' dimensions, but got array '
--> 636                            'with shape ' + str(data_shape))
    637         if not check_batch_axis:
    638           data_shape = data_shape[1:]

ValueError: Error when checking input: expected Input_input to have 4 dimensions, but got array with shape (1, 1, 2)

I have found an example on the internet where only Dense Layers were used (Copyright (c) 2020 Gabriel Nogueira (Talendar)).我在互联网上找到了一个仅使用密集层的示例(版权所有 (c) 2020 Gabriel Nogueira (Talendar))。 I would like to build a network with Conv2D and Dense Layers, but something doesn't seem to fit.我想用 Conv2D 和 Dense Layers 构建一个网络,但有些东西似乎不适合。

The code is built as follows:代码构建如下:

import sys
import os

import flappy_bird_gym 
env = flappy_bird_gym.make("FlappyBird-v0") #greyscale format 

env.action_space #Discrete(2)
env.observation_space #Box(-inf, inf, (2,), float32)

actions = env.action_space.n #2
obs = env.observation_space.shape[0] #2

#Network:
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, Input

import numpy as np
from tensorflow.keras.optimizers import Adam
import tensorflow as tf

#build model
def build_model(obs, actions):

  model = Sequential() 

  model.add(Conv2D(32, (8,8), name='Input', padding='same',input_shape=(1,obs,1)))
  model.add(MaxPooling2D((2,2), padding='same', name='maxpooling1'))

  model.add(Conv2D(64, (4,4), padding='same', activation='relu', name='Conv1'))
  model.add(MaxPooling2D((2,2), padding='same', name='maxpooling2'))
 
  model.add(Conv2D(64, (3,3), padding='same', activation='relu', name='Conv2'))
  model.add(MaxPooling2D((2,2), padding='same', name='maxpooling3'))
  
  model.add(Flatten())
  
  model.add(Dense(256, activation='relu', name='Dense1')) 
  model.add(Dense(actions, activation='linear',name='Output'))
  
  return model

model = build_model(obs, actions)
model.summary()

Model: "sequential_15"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 Input (Conv2D)              (None, 1, 2, 32)          2080      
                                                                 
 maxpooling1 (MaxPooling2D)  (None, 1, 1, 32)          0         
                                                                 
 Conv1 (Conv2D)              (None, 1, 1, 64)          32832     
                                                                 
 maxpooling2 (MaxPooling2D)  (None, 1, 1, 64)          0         
                                                                 
 Conv2 (Conv2D)              (None, 1, 1, 64)          36928     
                                                                 
 maxpooling3 (MaxPooling2D)  (None, 1, 1, 64)          0         
                                                                 
 flatten_20 (Flatten)        (None, 64)                0         
                                                                 
 Dense1 (Dense)              (None, 256)               16640     
                                                                 
 Output (Dense)              (None, 2)                 514       
                                                                 
=================================================================
Total params: 88,994
Trainable params: 88,994
Non-trainable params: 0
_________________________________________________________________

#RL
from rl.agents import DQNAgent
from rl.memory import SequentialMemory
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy

#build agent:
def build_agent(): 
  policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr='eps', value_max=0.5, value_min=.0001, value_test=.0, nb_steps=6000000)
  memory = SequentialMemory(limit=100000, window_length=1)
  dqn = DQNAgent(model=model, memory=memory, policy=policy, #RL Algorithm
                enable_dueling_network=True, dueling_type='avg', #technique you use 
                nb_actions=actions, nb_steps_warmup=5000)
  return dqn
  
dqn = build_agent() 

#train:
from tensorflow.keras.optimizers import Adam
dqn.compile(Adam(lr=0.00025)) 

dqn.fit(env, nb_steps=500000, visualize=False, verbose=2) #here the error occurs

--> in the last line the error occurs --> 在最后一行发生错误

Does anyone know what I am doing wrong or what I need to change?有谁知道我做错了什么或我需要改变什么?

The error is coming from your input data.错误来自您的输入数据。

As you can see the first layer is expecting the data to have dimension (None, 1, 2, 32) (The None is just the number of samples in the array).如您所见,第一层期望数据具有维度(None, 1, 2, 32) (None 只是数组中的样本数)。 The key thing is your data has shape (1,2,2) and not (1, 2, 32) .关键是您的数据具有形状(1,2,2)而不是(1, 2, 32) If you show us your data or maybe the what kind of data we can probably help a bit more on how to reshape it properly in order for the error to disappear.如果您向我们展示您的数据或者可能是哪种数据,我们可能会在如何正确重塑数据以使错误消失方面提供更多帮助。

暂无
暂无

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

相关问题 ValueError:检查输入时出错:预期 input_1 有 5 个维度,但得到形状为 (1221, 50, 50, 1) 的数组 - ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (1221, 50, 50, 1) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的形状为数组(8020,1) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1) ValueError:检查输入时出错:预期flatten_input具有3个维,但数组的形状为(22,12) - ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (22, 12) ValueError:检查输入时出错:预期density_11_input具有3维,但数组的形状为(0,1) - ValueError: Error when checking input: expected dense_11_input to have 3 dimensions, but got array with shape (0, 1) ValueError:检查输入时出错:预期density_6_input具有3维,但数组的形状 - ValueError: Error when checking input: expected dense_6_input to have 3 dimensions, but got array with shape ValueError:检查输入时出错:预期 input_1 有 4 个维度,但得到了形状为(无、无、无)的数组 - ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (None, None, None) ValueError:检查输入时出错:预期dense_input有2维,但得到形状为(1,1,2)的数组 - ValueError: Error when checking input: expected dense_input to have 2 dimensions, but got array with shape (1, 1, 2) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1) - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(393613,50) - ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (393613, 50)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM