首页 > 解决方案 > 用 Eli5 库解释 LSTM keras

问题描述

我正在尝试使用 Eli5 来解释用于时间序列预测的 LSTM keras 模型。keras 模型接收具有形状(nsamples、timesteps、nfeatures)的数组作为输入。

这是我的代码:

def baseline_model():
    model = Sequential()
    model.add(LSTM(32, input_shape=(X_train.shape[1], X_train.shape[2])))
    model.add(Dropout(0.2))
    model.add(Dense(1))
    model.compile(loss='logcosh', optimizer='adam')
return model

from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance

my_model = KerasRegressor(build_fn= baseline_model, nb_epoch= 30,   batch_size= 32, verbose= False)

history = my_model.fit(X_train, y_train)

到目前为止,一切正常。问题是当我执行以下启动错误的行时:

# X_train has a shape equal to (nsamples, timesteps, nfeatures) and y_train has a shape (nsamples)

perm = PermutationImportance(my_model, random_state=1).fit(X_train, y_train)

错误:

ValueError Traceback (most recent call last)
    in ()
    2 d2_train_dataset = X_train.reshape((nsamples, timesteps * features))
    3
    ----> 4 perm = PermutationImportance(my_model, random_state=1).fit(X_train, y_train)
    5 #eli5.show_weights(perm, feature_names = X.columns.tolist())

    ~/anaconda3/lib/python3.6/site-packages/eli5/sklearn/permutation_importance.py in fit(self, X, y, groups, **fit_params)
    183 self.estimator_.fit(X, y, **fit_params)
    184
    --> 185 X = check_array(X)
    186
    187 if self.cv not in (None, "prefit"):

    ~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    568 if not allow_nd and array.ndim >= 3:
    569 raise ValueError("Found array with dim %d. %s expected <= 2."
    --> 570 % (array.ndim, estimator_name))
    571 if force_all_finite:
    572 _assert_all_finite(array,

    ValueError: Found array with dim 3. Estimator expected <= 2.

我能做些什么来解决这个错误?如何将 eli5 与我的 LSTM Keras 模型一起使用?

标签: keras

解决方案


推荐阅读