在Keras输入形状

时间:2018-05-21 00:16:00

标签: python-3.x neural-network keras deep-learning openai-gym

我正在使用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网络的第一层输入该形状时,它都不起作用。

我应该使用什么样的形状,以便我可以进一步发展网络?

附上整个代码:

第一部分检索培训数据

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)

1 个答案:

答案 0 :(得分:0)

您似乎正在预先处理单个图像,但将它们放在列表中而不是输入张量中。从错误消息中,您有一个36859(80,80)数组的列表,而您希望有一个单一的数组(36859,80,80)。您有代码执行此注释X = np.array([i[0] for i in training_data]),您必须确保每个i[0]具有相同的形状(80,80)才能生效。