1

我是使用python的新手。我正在尝试为超过 10000 个样本运行石灰解释器。我正在使用 1D-CNN keras 模型进行解释。

import numpy as np
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Conv1D, Activation, Flatten, Dense
from keras.optimizers import Adam

a = np.array([[0,1,2,9,3], [0,5,1,33,6], [1, 12,1,8,9]])
train = np.reshape(a[:,1:],(a[:,1:].shape[0], a[:,1:].shape[1],1))
y_train = keras.utils.to_categorical(a[:,:1])

model = Sequential()
model.add(Conv1D(filters=2, kernel_size=2, strides=1, activation='relu', padding="same", input_shape=(train.shape[1], 1)))
model.add(Flatten())
model.add(Dense(2, activation='sigmoid'))

model.compile(loss=keras.losses.binary_crossentropy,
                 optimizer=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False),
                 metrics=['accuracy'])
model.fit(train, y_train, epochs=3, verbose=1)
import lime
import lime.lime_tabular
explainer = lime.lime_tabular.RecurrentTabularExplainer(train, training_labels=y_train ,
                                                        class_names=['0','1'], feature_names=['feat'])

result = []
def explain_lime(model_keras, input, numFeatures):
    for i in range(train.shape[0]):
        explanation = explainer.explain_instance(input[i], model.predict, num_samples=numFeatures)
        expMat = explanation.as_list()
        result.append(expMat)
    return print(f'{i} finished.')

explain_lime(model_keras=model,input=train , numFeatures=4) # works well

如何使用多处理库来做到这一点。我尝试了一些解决方案但没有成功

from joblib import Parallel, delayed
results = Parallel(n_jobs=-1, backend="threading")(map(delayed(explain_lime), model, train, 5))

results = Parallel(n_jobs=-1, backend="threading")(map(delayed(explain_lime), model, train, 5))
TypeError: 'Sequential' object is not iterable
import multiprocessing
pool = multiprocessing.Pool(processes=40)
results = pool.map(explain_lime(model, train, 5))

TypeError: map() missing 1 required positional argument: 'iterable'

我不明白错误是来自我的代码还是来自不兼容的石灰函数。

4

0 回答 0