ValueError:检查输入时出错:期望的dense_1_input有4个维度,但是有形状的数组(20593,4,1)

时间:2017-12-15 16:29:03

标签: deep-learning keras keras-layer keras-2

我正在尝试关注sentdex的游戏ai bot教程(https://www.youtube.com/watch?v=G-KvpNGudLw),但我没有尝试使用keras进行相同的实现。

模型功能

def neural_network_model(input_size):
    network = Sequential()

    network.add(Dense(units = 128, activation='relu', kernel_initializer = 'uniform', input_shape = [None, input_size, 1]))
    network.add(Dropout(0.2))

    network.add(Dense(units = 256, activation='relu', kernel_initializer = 'uniform'))
    network.add(Dropout(0.2))

    network.add(Dense(units = 512, activation='relu', kernel_initializer = 'uniform'))
    network.add(Dropout(0.2))

    network.add(Dense(units = 256, activation='relu', kernel_initializer = 'uniform'))
    network.add(Dropout(0.2))

    network.add(Dense(units = 128, activation='relu', kernel_initializer = 'uniform'))
    network.add(Dropout(0.2))

    network.add(Dense(units = 2, activation = 'softmax', kernel_initializer = 'uniform'))
    adam = optimizers.Adam(lr=LR, decay=0.0)
    network.compile(optimizer=adam, loss='categorical_crossentropy', metrics = ['accuracy'])
    return network

模型训练功能

def train_model(training_data, model=False):
    X = np.array([i[0] for i in training_data]).reshape(-1, len(training_data[0][0]), 1)
    Y = [i[1] for i in training_data]

    if not model:
        model = neural_network_model(len(X[0]))

    model.fit(X,Y, epochs = 5) 
    return model

培训数据为:

def initial_population():
    training_data = [] # Observations and the move made, append to only when score > 50
    scores = []
    accepted_scores = []
    for x in range(initial_games):
        score = 0
        game_memory = []
        prev_observation = []
        for x in range(goal_steps):
            action = random.randrange(0,2) # 0's and 1's
            observation, reward, done, info = env.step(action)

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

            prev_observation = observation
            score += reward
            if done:
                break

        if score >= score_requirement:
            accepted_scores.append(score)
            for data in game_memory:
                if data[1] == 1:
                    output = [0,1]
                if data[1] == 0:
                    output = [1,0]

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

        env.reset()
        scores.append(score)

    training_data_save = np.array(training_data)
    np.save('saved.npy', training_data_save)

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

    return training_data
training_data = initial_population()

我得到的错误在标题中。我是深度学习的新手,我对重塑部分还没有很好的掌握。

1 个答案:

答案 0 :(得分:0)

所以经过一些调整后我终于让网络工作了。如果有人有兴趣,我通过执行以下操作来修复它:

我将第一个Dense图层更改为:

network.add(Dense(units = 128, activation='relu', kernel_initializer = 'uniform', input_dim = input_size))

模型训练功能中,我将输入的形状更改为2D而不是3D:

def train_model(training_data, model=False):
X = np.array([i[0] for i in training_data]).reshape(-1, len(training_data[0][0]))
Y = np.array([i[1] for i in training_data])

if not model:
    model = neural_network_model(len(X[0]))

model.fit(X,Y, epochs = 5) 
return model