我正在嘗試使用 Keras 的模型制作一些程序,然后使用 AIX360 的 Lime 解釋器(它只是原始 LIME 的包裝器)對其進行解釋。所有數據都是 MNIST 灰度數字。但就我而言,我無法解釋這個實例,因為我不知道該向解釋者提供什么。我的代碼:!pip install aix360!pip install tensorflow==2.2.0from __future__ import print_functionimport warnings# Supress jupyter warnings if required for cleaner outputwarnings.simplefilter('ignore')import numpy as npimport pandas as pdimport kerasimport keras.layersfrom keras.utils.np_utils import to_categorical # convert to one-hot-encodingfrom keras.models import Sequential # Sequeantial layer additionfrom aix360.algorithms.lime import LimeImageExplainerprint('Using keras:', keras.__version__)# Load datasetfrom keras.datasets import mnist# Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test).(train, train_labels), (test, test_labels) = mnist.load_data()# save input image dimensionsimg_rows = train.shape[1]img_cols = train.shape[2]# Get classes and number of valuesvalue_counts = pd.value_counts(train_labels).sort_index()num_classes = value_counts.count()train = train/255test = test/255train = train.reshape(train.shape[0], img_rows, img_cols, 1)test = test.reshape(test.shape[0], img_rows, img_cols, 1)model = keras.Sequential([ keras.layers.Flatten(input_shape=(img_rows, img_cols,1)), keras.layers.Dense(128, activation='relu'), keras.layers.Dense(num_classes)])model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])batch_size = 128epochs = 1model.fit(train, train_labels, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(test, test_labels))score = model.evaluate(test, test_labels, verbose=0)print('Test loss:', score[0])print('Test accuracy:', score[1])limeExplainer = LimeImageExplainer()limeExplainer.explain_instance(test[0], model.predict_proba)最后一行是錯誤所在。不要關注模型是如何訓練的,那不是問題。編輯:編輯代碼,希望它可以在代碼實驗室中運行(添加第二行)EDIT2:要完成:tensorflow 2.2.0 keras 2.4.3 aix360 0.2.0
1 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
我添加了這個轉換并對 RGB 圖像進行了訓練:
def to_rgb(x):
x_rgb = np.zeros((x.shape[0], 28, 28, 3))
for i in range(3):
x_rgb[..., i] = x[..., 0]
return x_rgb
train_rgb = to_rgb(train)
test_rgb = to_rgb(test)
它起作用了:
limeExplainer.explain_instance(test_rgb[0], model.predict_proba)
100%
1000/1000 [00:00<00:00, 2598.51it/s]
<lime.lime_image.ImageExplanation at 0x7f8d20381f50>
添加回答
舉報
0/150
提交
取消