ValueError:检查输入时的错误:预期Input_Input具有4个维度,但具有形状的数组(1,1,2)

发布于 2025-02-05 06:39:13 字数 5291 浏览 4 评论 0原文

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:

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 (版权(C)2020 Gabriel Nogueira(Talendar))。我想建立一个具有Conv2D和致密层的网络,但似乎不合适。

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?

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:

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)). I would like to build a network with Conv2D and Dense Layers, but something doesn't seem to fit.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2025-02-12 06:39:13

错误来自您的输入数据。

如您所见,第一层期望数据具有尺寸(无,1,2,32)(无只是数组中的样本数)。关键是您的数据具有形状(1,2,2)而不是(1,2,32)。如果您向我们展示您的数据,或者也许是什么样的数据,我们可能可以帮助更多地重塑其正确重塑以使错误消失。

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). The key thing is your data has shape (1,2,2) and not (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文