在 Keras 中输入形状

Input shape in Keras

我正在使用 Keras 使用来自 Open AI 的 Gym 库中的图像创建一个深度神经网络。

我尝试使用以下代码重塑图像:

def reshape_dimensions(observation):
    processed = np.mean(observation,2,keepdims = False)
    cropped = processed[35:195]
    result = cropped[::2,::2]

    return result

这给了我一个形状为 (80,80) 的图像,但每次我尝试在 Keras 网络的第一层输入该形状时,它都不起作用。

我应该使用什么形状才能进一步发展网络?

附上全部代码:

第 I 部分 检索训练数据

import gym
import random
import numpy as np
from statistics import mean, median
from collections import Counter


### GAME VARIABLE SETTINGS ###
env = gym.make('MsPacman-v0')
env.reset()

goal_steps = 2000
score_requirement = 250
initial_games = 200

print('Options to play: ',env.unwrapped.get_action_meanings())


### DEFINE FUNCTIONS ####

def reshape_dimensions(observation):
    processed = np.mean(observation,2,keepdims = False)
    cropped = processed[35:195]
    result = cropped[::2,::2]

    return result

def initial_population():
    training_data = []
    scores = []
    accepted_scores = []

    for _ in range(initial_games):
        score = 0
        game_memory = []
        prev_obvservation = []
        for _ in range(goal_steps):
            #env.render()
            action = env.action_space.sample() #Take random action in the env
            observation, reward, done, info = env.step(action)

            reshape_observation = reshape_dimensions(observation)

            if len(prev_obvservation) > 0:
                game_memory.append([prev_obvservation, action])

            prev_obvservation = reshape_observation

            score = score + reward
            if done: 
                break

        if score >= score_requirement:
            accepted_scores.append(score)

            for data in game_memory: 
                if data[1] == 0:
                    output = [1,0,0,0,0,0,0,0,0]
                elif data[1] == 1:
                    output = [0,1,0,0,0,0,0,0,0]
                elif data[1] == 2:
                    output = [0,0,1,0,0,0,0,0,0]
                elif data[1] == 3:
                    output = [0,0,0,1,0,0,0,0,0]
                elif data[1] == 4:
                    output = [0,0,0,0,1,0,0,0,0]
                elif data[1] == 5:
                    output = [0,0,0,0,0,1,0,0,0]
                elif data[1] == 6:
                    output = [0,0,0,0,0,0,1,0,0]
                elif data[1] == 7:
                    output = [0,0,0,0,0,0,0,1,0]
                elif data[1] == 8:
                    output = [0,0,0,0,0,0,0,0,1]

                training_data.append([data[0],output])



        env.reset()
        scores.append(score)


    print('Average accepted scores:', mean(accepted_scores))
    print('Median accepted scores:', median(accepted_scores))
    print(Counter(accepted_scores))

    return training_data 



### RUN CODE ###

training_data = initial_population()
np.save('data_for_training_200.npy', training_data)

第二部分训练模型

import gym
import random
import numpy as np
import keras
from statistics import mean, median
from collections import Counter
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam


### LOAD DATA ###

raw_training_data = np.load("data_for_training_200.npy")
training_data = [i[0:2] for i in raw_training_data]

print(np.shape(training_data))

### DEFINE FUNCTIONS ###


def neural_network_model():

    network = Sequential()
    network.add(Dense(100, activation = 'relu', input_shape = (80,80)))
    network.add(Dense(9,activation = 'softmax'))

    optimizer = Adam(lr = 0.001)

    network.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics=['accuracy'])

    return network

def train_model(training_data):

    X = [i[0] for i in training_data]
    y = [i[1] for i in training_data]

    #X = np.array([i[0] for i in training_data])
    #y = np.array([i[1] for i in training_data])

    print('shape of X: ', np.shape(X))
    print('shape of y: ', np.shape(y))

    early_stopping_monitor = EarlyStopping(patience = 3)

    model = neural_network_model()

    model.fit(X, y, epochs = 20, callbacks = [early_stopping_monitor])

    return model

train_model(training_data = training_data)

您似乎正确地预处理了单个图像,但将它们放在列表中而不是输入张量中。从错误消息中,您有一个包含 36859 (80,80) 个数组的列表,而您想要一个形状为 (36859, 80, 80) 的数组。您已将执行此操作的代码注释掉 X = np.array([i[0] for i in training_data]),您必须确保每个 i[0] 的形状相同 (80,80) 才能正常工作。