代码之家  ›  专栏  ›  技术社区  ›  Minions

keras不收敛的神经网络

  •  2
  • Minions  · 技术社区  · 6 年前

    我正在Keras建立一个简单的神经网络,如下所示:

    # create model
    model = Sequential()
    model.add(Dense(1000, input_dim=x_train.shape[1], activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    
    # Compile model
    model.compile(loss='mean_squared_error', metrics=['accuracy'], optimizer='RMSprop')
    # Fit the model
    model.fit(x_train, y_train, epochs=20, batch_size=700, verbose=2)
    # evaluate the model
    scores = model.evaluate(x_test, y_test, verbose=0)
    print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    

    所用数据的形状为:

    x_train = (49972, 601) 
    y_train = (49972, 1)
    

    我的问题是网络没有收敛,精度固定在0.0168,如下所示:

    Epoch 1/20
     - 1s - loss: 3.2222 - acc: 0.0174
    Epoch 2/20
     - 1s - loss: 3.1757 - acc: 0.0187
    Epoch 3/20
     - 1s - loss: 3.1731 - acc: 0.0212
    Epoch 4/20
     - 1s - loss: 3.1721 - acc: 0.0220
    Epoch 5/20
     - 1s - loss: 3.1716 - acc: 0.0225
    Epoch 6/20
     - 1s - loss: 3.1711 - acc: 0.0235
    Epoch 7/20
     - 1s - loss: 3.1698 - acc: 0.0245
    Epoch 8/20
     - 1s - loss: 3.1690 - acc: 0.0251
    Epoch 9/20
     - 1s - loss: 3.1686 - acc: 0.0257
    Epoch 10/20
     - 1s - loss: 3.1679 - acc: 0.0261
    Epoch 11/20
     - 1s - loss: 3.1674 - acc: 0.0267
    Epoch 12/20
     - 1s - loss: 3.1667 - acc: 0.0277
    Epoch 13/20
     - 1s - loss: 3.1656 - acc: 0.0285
    Epoch 14/20
     - 1s - loss: 3.1653 - acc: 0.0288
    Epoch 15/20
     - 1s - loss: 3.1653 - acc: 0.0291
    

    我使用sklearn库用相同的数据构建了相同的结构,它工作得很好,向我展示了高于0.5的精度:

    model = Pipeline([
            ('classifier', MLPClassifier(hidden_layer_sizes=(1000), activation='relu',
                                         max_iter=20, verbose=2, batch_size=700, random_state=0))
        ])
    

    我完全确定我对两个模型使用了相同的数据,这就是我准备的方法:

    def load_data():
        le = preprocessing.LabelEncoder()
        with open('_DATA_train.txt', 'rb') as fp:
            train = pickle.load(fp)
        with open('_DATA_test.txt', 'rb') as fp:
            test = pickle.load(fp)
    
        x_train = train[:,0:(train.shape[1]-1)]
        y_train = train[:,(train.shape[1]-1)]
        y_train = le.fit_transform(y_train).reshape([-1,1])
    
        x_test = test[:,0:(test.shape[1]-1)]
        y_test = test[:,(test.shape[1]-1)]
        y_test = le.fit_transform(y_test).reshape([-1,1])
    
        print(x_train.shape, '  ' , y_train.shape)
        print(x_test.shape, '  ' , y_test.shape)
        return x_train, y_train, x_test, y_test
    

    路缘石结构有什么问题?

    编辑:

    这是一个多类分类问题:y_训练[0,1,2,3]

    1 回复  |  直到 6 年前
        1
  •  2
  •   Jeremy Bare    6 年前

    对于多类问题,您的标签应该是一个热编码。例如,如果选项是[0,1,2,3],标签是1,则应该是[0,1,0,0]。

    你的最后一层应该是一个有4个单位的密集层,并且激活了SoftMax。

    model.add(Dense(4, activation='softmax'))
    

    你的损失应该是绝对熵

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