Keras模型的输出相同

时间:2018-10-17 15:01:31

标签: python machine-learning neural-network keras classification

我有一个Keras模型,可以预测游戏中的移动。我的输入形状为(160,120 ,1)。我有以下模型,其输出为9个节点:

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
from keras.regularizers import l2
from keras import optimizers
def alexnet_model(n_classes=9, l2_reg=0.,
    weights=None):

    # Initialize model
    alexnet = Sequential()
    alexnet.add(Conv2D(24, (11, 11), input_shape=(160,120,1), activation ='relu'))
    alexnet.add(MaxPooling2D(pool_size=(2, 2)))
    alexnet.add(BatchNormalization())
    alexnet.add(Conv2D(36, (5, 5), activation ='relu'))
    alexnet.add(MaxPooling2D(pool_size=(2, 2)))
    alexnet.add(Conv2D(48, (3, 3),  activation ='relu'))
    alexnet.add(Conv2D(54, (3, 3),  activation ='relu'))
    alexnet.add(MaxPooling2D(pool_size=(2, 2)))
    alexnet.add(Flatten())
    alexnet.add(Dense(300,   activation ='tanh'))
    alexnet.add(Dropout(0.5))
    alexnet.add(Dense(200,   activation ='tanh'))
    alexnet.add(Dropout(0.5))
    alexnet.add(Dense(100,   activation ='tanh'))
    alexnet.add(Dropout(0.5))


    alexnet.add(Dense(n_classes , activation = 'softmax'))

    optimizer = Adam(lr=1e-3)

    alexnet.compile(loss='categorical_crossentropy', optimizer=optimizer)


    alexnet.summary()


    return alexnet

然后,我运行一个训练脚本。我的X的形状为(12862, 160, 120, 1)y的形状为(1000,9)

import numpy as np
import tensorflow as tf
from random import shuffle
import pandas as pd
from tensorflow.keras import layers,models
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
# what to start at
START_NUMBER = 60

# what to end at
hm_data = 111

# use a previous model to begin?
START_FRESH = False
WIDTH = 160
HEIGHT = 120
LR = 1e-3
EPOCHS = 1

MODEL_NAME = 'model_new.h5'
EXISTING_MODEL_NAME = ''

model = alexnet_model()

X=[]

Y=[]
for i in range(EPOCHS):
    train_data = np.load('training_data_1.npy')
    print(len(train_data))
    train = train_data[0:12862]
    test = train_data[-1000:]

    X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,1)
    Y = np.array([i[1] for i in train])

    test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,1)
    test_y = np.array([i[1] for i in test])
    print(X.shape)
    model.fit(X, Y , batch_size = 16, epochs = 10 , validation_data = (test_x, test_y), verbose=1)
    model.save(MODEL_NAME)

# tensorboard --logdir=foo:C:/Users/H/Desktop/ai-gaming-phase5/log

测试模型后,我得到一个输出:

array([[2.8518048e-01, 5.5075828e-03, 7.3730588e-02, 5.3255934e-02,
        1.0635615e-01, 6.4690344e-02, 9.1519929e-08, 7.0413840e-08,
        4.1127869e-01]], dtype=float32)

具有以下代码行:

model.predict(X[100].reshape(-1,160,120,1)) 

我知道在X上测试模型不是很好,但是我使用哪张图片都没关系,但是我得到的输出相同。仅供参考(我的Y值):

w = [1,0,0,0,0,0,0,0,0]
s = [0,1,0,0,0,0,0,0,0]
a = [0,0,1,0,0,0,0,0,0]
d = [0,0,0,1,0,0,0,0,0]
wa = [0,0,0,0,1,0,0,0,0]
wd = [0,0,0,0,0,1,0,0,0]
sa = [0,0,0,0,0,0,1,0,0]
sd = [0,0,0,0,0,0,0,1,0]
nk = [0,0,0,0,0,0,0,0,1]

我尝试了另一种模型,但仍然无法正常工作。这是每个课程的培训数据量:

Counter({'[1, 0, 0, 0, 0, 0, 0, 0, 0]': 5000,
         '[0, 0, 0, 0, 0, 0, 0, 0, 1]': 5000,
         '[0, 0, 0, 0, 1, 0, 0, 0, 0]': 1183,
         '[0, 0, 0, 0, 0, 1, 0, 0, 0]': 982,
         '[0, 0, 1, 0, 0, 0, 0, 0, 0]': 832,
         '[0, 0, 0, 1, 0, 0, 0, 0, 0]': 764,
         '[0, 1, 0, 0, 0, 0, 0, 0, 0]': 101})

我认为问题出在模型上,但我不知道如何更改。可能是培训数据少的问题吗?损失价值也没有下降:loss: 1.7416 - val_loss: 1.4639。它只会减少几位小数,有时甚至还会增加。

2 个答案:

答案 0 :(得分:0)

根据代码中的内容,由于您提到损耗的降低非常缓慢,因此最好的猜测是输入数据(我认为是图像)没有被标准化,因此这会阻止平滑的梯度流。尝试将它们标准化。一种简单的方法是这样的:

X = X.astype('float32') / 255.0
test_x = test_x.astype('float32') / 255.0

此外,您可能需要考虑训练数据中的班级失衡并通过在fit方法中使用class_weights自变量来解决(请查看文档以了解如何使用它)

答案 1 :(得分:0)

解决了! 仅仅标准化训练数据是行不通的。我减少了节点和层的数量,一切正常。我想这是一个过拟合的问题。