【问题标题】:How can I use Lime to classify my time series如何使用 Lime 对时间序列进行分类
【发布时间】:2020-08-14 03:16:05
【问题描述】:

我的简化模型如下所示:

model = Sequential()
model.add(LSTM(12, input_shape=(1000,12)))
model.add(Dense(9, activation='sigmoid'))

我的训练数据是这样的:

(900,1000,12)

从输出层可以看出,我有 9 个输出,所以每个信号(长度为 1000)都会被分类为一个或多个输出(这是一个多标签分类)

我这样训练我的模型:

history = model.fit(X_train,y_train, batch_size=32, epochs=10,validation_data=(X_val,y_val),verbose=2)

所以到目前为止一切正常,但现在我想用 Lime 来解释分类

explainer = lime_tabular.RecurrentTabularExplainer(X_train, training_labels=y_train,feature_names=['1','2','3','4','5','6','7','8','9','10','11','12'],
                                                   discretize_continuous=True,
                                                   class_names=['a','b','c','d','e','f','g','h','i'],
                                                   discretizer='decile')

我在定义解释器时没有收到任何错误,但是当我尝试运行下面的代码时,它运行了很长时间才给我一个错误

exp=explainer.explain_instance(data_row=X[0].reshape(1,1000,12),classifier_fn= model)
exp.show_in_notebook()
NotImplementedError: LIME does not currently support classifier models without probability scores. 
If this conflicts with your use case, please let us know: https://github.com/datascienceinc/lime/issues/16

任何人都可以识别这个错误或看看有什么问题吗?

【问题讨论】:

    标签: python tensorflow keras time-series lime


    【解决方案1】:

    您应该在explainer.explain_instance 中将分类器预测概率函数传递给classifier_fn,该函数接受一个numpy 数组并输出预测概率:在您的情况下为model.predict_proba(如果它产生概率,model.predict 也可以工作)。

    还要注意,在您的情况下,预测概率总和不等于 1,因为您在最后一层应用了sigmoid 激活。考虑切换到softmax 以产生总和为 1 的概率

    这里是完整的例子:

    拟合一个虚拟模型

    X = np.random.uniform(0,1, (50, 10, 12))
    y = np.random.randint(0,1, (50, 9))
    
    model = Sequential()
    model.add(LSTM(12, input_shape=(10, 12)))
    model.add(Dense(9, activation='softmax'))
    model.compile('adam', 'categorical_crossentropy')
    history = model.fit(X, y, epochs=3)
    

    初始化解释器

    from lime import lime_tabular
    
    explainer = lime_tabular.RecurrentTabularExplainer(
        X, training_labels = y,
        feature_names = ['1','2','3','4','5','6','7','8','9','10','11','12'],
        discretize_continuous = True,
        class_names = ['a','b','c','d','e','f','g','h','i'],
        discretizer = 'decile')
    

    解释实例:

    exp = explainer.explain_instance(
        data_row = X[0].reshape(1,10,12),
        classifier_fn = model.predict)
    
    exp.show_in_notebook()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2019-12-13
      • 2018-01-14
      相关资源
      最近更新 更多