1

我的 Keras 模型如下:https
://i.stack.imgur.com/2Gfun.png 它需要两个短语,结果是它们之间的关系,可以是“攻击,支持,两者都不是”。

现在,我想用 LIME 解释这个模型的输出,我尝试了以下方法:

from lime.lime_text import LimeTextExplainer
explainer = LimeTextExplainer(class_names = ['Attack','Defence','Neither'])

def new_predict(testo):
  print(testo)
  testo_split = testo.split("/")
  print(testo_split)
  testo1 = testo_split[0]
  print(testo1)
  testo2 = testo_split[1]
  print(testo2)
  print("DATAFRAME")
  row_cani = {'testo1' : [testo1] , 'testo2' : [testo2]}
  df_cani = pd.DataFrame(row_cani)
  print(df_cani)
  df_cani['testo_token1'] = tokenizer.texts_to_sequences(df_cani['testo1'])
  df_cani['testo_token2']  = tokenizer.texts_to_sequences(df_cani['testo2'])
  df_cani['testo_token_padding1'] = pad_sequences(df_cani['testo_token1'], padding = "post", maxlen = max_len).tolist()
  df_cani['testo_token_padding2'] = pad_sequences(df_cani['testo_token2'], padding = "post", maxlen = max_len).tolist()
  valori_df_testo1_cani = df_cani['testo_token_padding1'].values 
  valori_df_testo2_cani = df_cani['testo_token_padding2'].values 
  valori_testo1_cani = np.array([item for item in valori_df_testo1_cani])
  valori_testo2_cani = np.array([item for item in valori_df_testo2_cani])
  return model_final.predict([valori_testo1_cani, valori_testo2_cani])

frase_cani_argomento = "You hear barking and howling in the distance."
frase_cani_claim = "there are dogs nearby."
testo = frase_cani_argomento + " /" + frase_cani_claim
exp = explainer.explain_instance(testo, new_predict)
exp.show_in_notebook(text = True)

testo现在,当我在函数内部传递字符串时,explainer.explain_instance(testo, new_predict)该字符串变为:

['You hear barking and howling in the distance. /there are dogs nearby.', '/', '/there are dogs nearby.', 'You hear barking and howling in the distance. /', '/', '/', '/there are dogs nearby.', '/', 'You hear barking and howling in the distance. /', 'You hear barking and howling in the distance. /', '/', 'You hear barking and howling in the distance. /', '/there are dogs nearby.', '/there are dogs nearby.', '/', 'You hear barking and howling in the distance. /', '/', '/there are dogs nearby.', '/', '/there are dogs nearby.', '/there are dogs nearby.', '/there are dogs nearby.', '/there are dogs nearby.', '/', '/', 'You hear barking and howling in the distance. /', '/', 'You hear barking and howling in the distance. /', '/there are dogs nearby.', '/there are dogs nearby.', '/', '/there are dogs nearby.', 'You hear barking and howling in the distance. /', '/', '/', '/there are dogs nearby.', 'You hear barking and howling in the distance. /', 'You hear barking and howling in the distance. /', 'You hear barking and howling in the distance. /', '/', '/there are dogs nearby.', '/', '/', '/', '/there are dogs nearby.', 'You hear barking and howling in the distance. /', 'You hear barking and howling in the distance. /', '/there are dogs nearby.', '/', '/there are dogs nearby.', ...]

你知道为什么会这样吗?我试图用一个文本输入来解释一个模型并且它有效,但是有多个输入我的程序有问题。

4

0 回答 0