首页 > 解决方案 > 从神经网络中找到最重要的输入

问题描述

我用 37 个输入训练了一个神经网络。它的准确率约为 85%。我是否有可能找出哪个 Input 效果最大。我试过这段代码,但我不知道如何找到最重要的输入

weights = model.layers[0].get_weights()[0]
biases = model.layers[0].get_weights()[1]

标签: python-3.xtensorflowkeras

解决方案


一种可能的解决方案是使用scikit-learn包装模型,keras.wrappers.scikit_learn然后使用递归特征消除:

def create_model():
    # create model
    model = Sequential()
    model.add(Dense(512, activation='relu'))
    model.add(Dense(512, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = KerasClassifier(build_fn=create_model, epochs=100, batch_size=128, verbose=0)
rfe = RFE(estimator=model, n_features_to_select=1, step=1)
rfe.fit(X, y)
ranking = rfe.ranking_.reshape(digits.images[0].shape)

# Plot pixel ranking
plt.matshow(ranking, cmap=plt.cm.Blues)
plt.colorbar()
plt.title("Ranking of pixels with RFE")
plt.show()

用 rfe 对像素进行排序

如果您需要可视化权重,请参见此处


推荐阅读