簡體   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)

我正在嘗試使用卷積層和密集層創建 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)

我在互聯網上找到了一個僅使用密集層的示例(版權所有 (c) 2020 Gabriel Nogueira (Talendar))。 我想用 Conv2D 和 Dense Layers 構建一個網絡,但有些東西似乎不適合。

代碼構建如下:

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

--> 在最后一行發生錯誤

有誰知道我做錯了什么或我需要改變什么?

錯誤來自您的輸入數據。

如您所見,第一層期望數據具有維度(None, 1, 2, 32) (None 只是數組中的樣本數)。 關鍵是您的數據具有形狀(1,2,2)而不是(1, 2, 32) 如果您向我們展示您的數據或者可能是哪種數據,我們可能會在如何正確重塑數據以使錯誤消失方面提供更多幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM