Why I'm getting bad result with Keras vs random forest or knn?

前端 未结 2 464
一向
一向 2020-12-21 17:03

I\'m learning deep learning with keras and trying to compare the results (accuracy) with machine learning algorithms (sklearn) (i.e random fo

相关标签:
2条回答
  • 2020-12-21 17:32

    In short, you need:

    1. ReLU activations
    2. Simpler model
    3. Data mormalization
    4. More epochs

    In detail:

    The first issue here is that nowadays we never use activation='tanh' for the intermediate network layers. In such problems, we practically always use activation='relu'.

    The second issue is that you have build quite a large Keras model, and it might very well be the case that with only 100 iris samples in your training set you have too few data to effectively train such a large model. Try reducing drastically both the number of layers and the number of nodes per layer. Start simpler.

    Large neural networks really thrive when we have lots of data, but in cases of small datasets, like here, their expressiveness and flexibility may become a liability instead, compared with simpler algorithms, like RF or k-nn.

    The third issue is that, in contrast to tree-based models, like Random Forests, neural networks generally require normalizing the data, which you don't do. Truth is that knn also requires normalized data, but in this special case, since all iris features are in the same scale, it does not affect the performance negatively.

    Last but not least, you seem to run your Keras model for only one epoch (the default value if you don't specify anything in model.fit); this is somewhat equivalent to building a random forest with a single tree (which, BTW, is still much better than a single decision tree).

    All in all, with the following changes in your code:

    from sklearn.preprocessing import StandardScaler
    sc = StandardScaler()
    X_train = sc.fit_transform(X_train)
    X_test = sc.transform(X_test)
    
    model = Sequential()
    model.add(Dense(150, activation='relu', input_shape = ((df.shape[1]-1),)))
    model.add(Dense(150, activation='relu'))
    model.add(Dense(y.shape[1], activation='softmax'))
    
    model.fit(X_train, y_train, epochs=100)
    

    and everything else as is, we get:

    score, acc = model.evaluate(X_test, y_test, verbose=0)
    acc
    # 0.9333333373069763
    

    We can do better: use slightly more training data and stratify them, i.e.

    X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                        test_size = 0.20, # a few more samples for training
                                                        stratify=y)
    

    And with the same model & training epochs you can get a perfect accuracy of 1.0 in the test set:

    score, acc = model.evaluate(X_test, y_test, verbose=0)
    acc
    # 1.0
    

    (Details might differ due to some randomness imposed by default in such experiments).

    0 讨论(0)
  • 2020-12-21 17:53

    Adding some dropout might help you improve accuracy. See Tensorflow's documentation for more information.

    Essentially how you add a Dropout layer is just very similar to how you added those Dense() layers.

    model.add(Dropout(0.2)
    

    Note: The parameter '0.2 implies that 20% of the connections in the layer is randomly omitted to reduce the interdependencies between them, which reduces overfitting.

    0 讨论(0)
提交回复
热议问题