【问题标题】:Neural network model predicting unexpected results预测意外结果的神经网络模型
【发布时间】:2017-10-31 14:17:43
【问题描述】:

我想创建一个能够自己玩赛车游戏的 CNN。 我已经用 30.000 个样本训练了模型,我的特征(x_test)是帧,我的标签(y_test)是 w d 键(one-hot 编码) 但我的模型是预测这个而不是预测 三个概率:

 [ 1.  0.  0.]
 [ 1.  0.  0.]
 [ 1.  0.  0.]
 [ 1.  0.  0.]
 [ 1.  0.  0.]
 [ 1.  0.  0.]

是我的模型结构有问题还是我需要更大的数据集?

我的代码是:

  1. 培训

        classifier = create_model()
            train_data=np.load('data1/final_data.npy')
            for i in range(52):
                file_name = 'data2/training_data-{}.npy'.format(i)
                train_data_2=np.load(file_name)
                train_data=np.concatenate((train_data,train_data_2))    
            shuffle(train_data)
            x=train_data[:,0]
            y=train_data[:,1]
            train_data=[]
            x=x.tolist()
            x=np.array(x)
            y=y.tolist()
            y=np.array(y)
            classifier.fit(x,y, epochs=5, batch_size=32)
            classifier.save('/output/model.model') 
    
  2. 型号

    def create_model():
        classifier = Sequential()
        classifier.add(Conv2D(96,(3,3),input_shape=(120,160,3),activation='relu'))
        classifier.add(MaxPooling2D(pool_size=(2,2)))
        classifier.add(Conv2D(256,(3,3),activation='relu'))
        classifier.add(MaxPooling2D(pool_size=(2,2)))
        classifier.add(Conv2D(384,(3,3),activation='relu'))
        classifier.add(Conv2D(384,(3,3),activation='relu'))  
        classifier.add(Conv2D(256,(3,3),activation='relu'))
        classifier.add(MaxPooling2D(pool_size=(2,2)))
        classifier.add(Flatten())
        classifier.add(Dense(units = 2048, activation = 'relu'))
        classifier.add(Dropout(0.5))
        classifier.add(Dense(units = 2048, activation = 'relu'))
        classifier.add(Dropout(0.5))
        classifier.add(Dense(units = 3, activation = 'softmax'))
        classifier.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', 
        metrics = ['accuracy'])  
        return classifier
    
  3. 测试员

            def straight():
                ReleaseKey(A)
                ReleaseKey(D)
                PressKey(W)
            def left():
                ReleaseKey(D)
                PressKey(W)
                PressKey(A)
    
            def right():
                ReleaseKey(A)
                PressKey(W)
                PressKey(D)
    
            def main():
                model=load_model('model.model')
    
                for i in list(range(5))[::-1]:
                    print(i+1)
                    time.sleep(1)
    
                paused = False
                while(True):
    
                    if not paused:
                        screen = grab_screen(region=(0,64,640,480))
                        screen = cv2.resize(screen, (160,120))
                        screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
                        screen = np.expand_dims(screen, axis = 0)
                        prediction = model.predict(screen)[0]
                        print(prediction)
    
                        maxval=max(prediction)
    
                        if prediction[1] == maxval:
            #                straight()
                            print('straigh',maxval)
                        elif prediction[2] == maxval:
            #                right()
                            print('right',maxval)
                        elif prediction[0] == maxval:
            #                left()
                            print('left',maxval)
            #
            #        keys = key_check()
            #
            #        if 'P' in keys:
            #            if paused:
            #                paused = False
            #                time.sleep(1)
            #            else:
            #                paused = True
            #                ReleaseKey(A)
            #                ReleaseKey(W)
            #                ReleaseKey(D)
            #                time.sleep(1)
    
            main()
    

【问题讨论】:

  • 你为什么决定训练这个 5 个 epoch?这 5 个 epoch 之后的训练和验证/测试准确度是多少?数据集是否平衡?
  • 损失:10.6982 - 累积:0.3363。我的数据集是平衡的。训练时是否需要提供训练集以外的验证集?
  • 你应该有一个验证集,以检测过拟合。损失高且准确率低,您要么必须训练模型更长时间,要么可能制作更深的模型。在查看预测之前,您应该调整时期和学习率。
  • 但至少我应该给我一些数值(概率),而不是为每个测试样本预测 [1. 0. 0.]。即使没有加载重量来建模,它也给了我类似 [0.45156 0.32545 0.195455]
  • [1 0 0 ] 实际上是概率,只是所有质量都集中在第一个元素上。

标签: tensorflow neural-network keras


【解决方案1】:

可以通过创建更深层次的神经网络来解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 2015-04-15
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2019-02-06
    相关资源
    最近更新 更多