兩種開源聊天機器人的效能測試(二)——基於tensorflow的chatbot

CopperDong發表於2017-11-16

   這次測試的作業系統依然是Ubuntu14.04(64位)

      開源專案連結:https://github.com/dennybritz/chatbot-retrieval/

      它實現一個檢索式的機器人。採用檢索式架構,有預定好的語料答覆庫。檢索式模型的輸入是上下文潛在的答覆。模型輸出對這些答覆的打分,選擇最高分的答案作為回覆。

      下面進入正題。

      1.環境配置

      首先此專案需要的基本條件是使用Python3(我用的是Python3.4),tensorflow版本為0.11.0。關於Python這裡不多說,網上很多修改Python預設值的文章。後續內容我都將採用python3或者pip3指令,在Python3下進行操作。tensorflow在我測試時,過低版本或者新版本都會出現一些問題,所以建議和我採用一樣的版本(因為我的電腦是AMD的顯示卡,所以我沒有選擇GPU版本的tensorflow,有條件的可以選擇)。如果不是可以採用以下命令修改:

sudo pip3 uninstall tensorflow

sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0-cp34-cp34m-linux_x86_64.whl

      好了,最基本的條件準備好了。

      下面我們將安裝一系列的依賴包,因為裡面依賴關係比較複雜,所以需要注意一下先後順序。我們在命令列中依次輸入以下指令:

sudo apt-get upgrade python3-pip

sudo pip3 install numpy scipy scikit-learn pandas pillow jupyter

sudo pip3 install backports.weakref==1.0rc1

sudo apt-get build-dep Python-imaging

sudo pip3 install tflearn

      到這裡,我們的環境基本配置好了,但是因為我是在配置過程中一個一個去解決的這些問題,中間碰到的問題也比較多,這是重新整理過的,每個人的環境也都有所差異,我也不能確保這樣就完全正確。一般這裡面碰到問題無非就是兩種,一是缺包,二是tensorflow版本的問題,往這兩方面考慮就可以解決。

      最後,檢查一下這些工具是否都安裝,我們開始匯入資料。

      首先執行git clone https://github.com/dennybritz/chatbot-retrieval/

      然後到此連結https://drive.google.com/file/d/0B_bZck-ksdkpVEtVc1R6Y01HMWM/view(需要fanqiang)去下載資料,頁面如果顯示“糟糕,出現預覽問題,正在重新載入”,不要管它,點選下載即可。將下載到的資料解壓到剛才clone的資料夾chatbot-retrieval的data中,如圖所示:


      2.訓練與測試

      在chatbot-retrieval資料夾中開啟終端,或者cd到該資料夾下,執行以下指令:

python3 udc_train.py

      即可開始訓練,我採用CPU訓練了三個多小時,這個視個人情況而定,可以按Ctrl+Z提前結束。在GPU上訓練2萬次大約一個多小時。正常訓練過程中如圖所示:


      訓練完後仍然在剛才的路徑下可以執行以下命令對模型進行評估(可以跳過):

python3 udc_test.py –model_dir=./runs/1504264339/

其中後面的數字名稱的資料夾名因人而異,不同的訓練批次名稱也不一樣。這個名稱在訓練的那張截圖裡也可以發現。

      最後,進入我們的測試環節。

      找到chatbot-retrieval資料夾下的udc_predict.py檔案,將30行INPUT_CONTEXT =後的內容改成自己想要問的內容,將31行POTENTIAL_RESPONSES = []中的內容替換成機器人的候選答案。因為這個專案沒有實現一問一答的模式,每次只是給不同的答案進行打分,分數最高的那個是它的期望回答,所以下面我們都將以其回答中打分最高的回答作為標準判斷正確率。仍然在chatbot-retrieval資料夾路徑下執行python3udc_predict.py --model_dir=./runs/1504221361/指令進行測試。

      下面是測試情況:

  • 中文閒聊型:

      (1)INPUT_CONTEXT = "你好"

      POTENTIAL_RESPONSES = ["你好", "早上好","中午好","晚上好","好啊","好久不見","很高興認識你","初次見面請多多指教","我不好","你是誰",]

      測試效果如圖,我們將每個結果的打分篩選出來。


Context: 你好

你好: 0.501835

早上好: 0.501835

中午好: 0.501835

晚上好: 0.501835

好啊: 0.501835

好久不見: 0.501835

很高興認識你: 0.501835

初次見面請多多指教: 0.501835

我不好: 0.501835

你是誰: 0.501835

      可以看到所有回答的打分都是一樣的,這其實是因為語料庫採用了Ubuntu對話資料集,無法處理中文。我們再測一組中文進行驗證。

 

      (2)INPUT_CONTEXT = "明天上午啥課?"

      POTENTIAL_RESPONSES = ["明天上午沒課", "計算機圖形學和形式與政策","明天上午有課嗎","還沒開學好不好","包子和稀飯","超市沒開門","明天下雨","那一年你正年輕","時間是讓人猝不及防的東西","瞎扯",]

測試結果:

Context: 明天上午啥課?

明天上午沒課: 0.501835

計算機圖形學和形式與政策: 0.501835

明天上午有課嗎: 0.501835

還沒開學好不好: 0.501835

包子和稀飯: 0.501835

超市沒開門: 0.501835

明天下雨: 0.501835

那一年你正年輕: 0.501835

時間是讓人猝不及防的東西: 0.501835

瞎扯: 0.501835

 

      這驗證了我們前面的結論,該機器人無法對中文進行判斷。接下來,我們測試英文話題。

  • 英文閒聊型

      (1)INPUT_CONTEXT = "hello"

       POTENTIAL_RESPONSES =["hi", "who are you","how are you","whereare you come from","how old are you"]

測試結果:

Context: hello

hi: 0.515187

who are you: 0.452673

how are you: 0.485824

where are you come from:0.462595

how old are you: 0.505551

      最高分結果為hi

      (2)INPUT_CONTEXT = "Where are you going?"

       POTENTIAL_RESPONSES =["The weather is nice today", "I love you","Are yousure?","Go home","My name is Watson."]

測試結果:

Context: Where are yougoing?

The weather is nice today:0.542136

I love you: 0.570628

Are you sure?: 0.564842

Go home: 0.528381

My name is Watson.:0.567629

      最高分結果為I love you

      (3)INPUT_CONTEXT = "What's your name?"

      POTENTIAL_RESPONSES =["No problem", "What a beautiful girlfriend youhave!","Look over there","My favorite basketball player isMichael Jordan","My name is Watson."]

測試結果:

Context: What's your name?

No problem: 0.433695

What a beautifulgirlfriend you have!: 0.53788

Look over there: 0.502499

My favorite basketballplayer is Michael Jordan: 0.557139

My name is Watson.:0.568081

      最高分結果為My name is Watson.

      (4)INPUT_CONTEXT = "Where are you come from?"

      POTENTIAL_RESPONSES =["China", "You're welcome","I am aboy","I've come up with a good idea","Don't go there"]

測試結果:

Context: Where are youcome from?

China: 0.500012

You're welcome: 0.505979

I am a boy: 0.498553

I've come up with a goodidea: 0.540985

Don't go there: 0.582593

      最高分結果為Don't go there

      (5)INPUT_CONTEXT = "How much is this dress?"

      POTENTIAL_RESPONSES =["15 dollars", "hello","I like dogs","It'sreally hot today","Don't worry"]

測試結果為:

Context: How much is thisdress?

15 dollars: 0.553268

hello: 0.500417

I like dogs: 0.493128

It's really hot today:0.665182

Don't worry: 0.531877

      最高分答案為It's really hot today

 

      由以上五組測試結果可以看到,回答正確的為(1)(3),正確率為40%。

  • 英文任務型

     (1)INPUT_CONTEXT = "Book me a ticket from Wuhan to Nanjingtomorrow."

      POTENTIAL_RESPONSES =["100 dollars", "There will be two flight tomorrow. Which flightwill you take?","You are handsome"]

測試結果:

Context: Book me a ticketfrom Wuhan to Nanjing tomorrow.

100 dollars: 0.460377

There will be two flighttomorrow. Which flight will you take?: 0.50801

You are handsome: 0.524509

      最高分答案為You are handsome

      (2)INPUT_CONTEXT = "Help me find out what the weather willbe like tomorrow"

      POTENTIAL_RESPONSES =["It's going to be sunny tomorrow", "I'm fine.Thankyou.","That's a cool car"]

測試結果:

Context: Help me find outwhat the weather will be like tomorrow

It's going to be sunnytomorrow: 0.480107

I'm fine.Thank you.:0.412907

That's a cool car:0.481291

      最高分答案為That's a cool car

(3)INPUT_CONTEXT = "Get me a KFC take out"

POTENTIAL_RESPONSES =["All right. I've already placed your order", "I need a bottleof mineral water","Deal"]

      測試結果:

Context: Get me a KFC takeout

All right. I've alreadyplaced your order: 0.468371

I need a bottle of mineralwater: 0.535811

Deal: 0.468816

      最高分答案為I need a bottle of mineral water

      (4)INPUT_CONTEXT = "Check out the nearest hotel"

      POTENTIAL_RESPONSES =["He looks so happy", "The hotel is 500 dollars anight","The nearest hotel is 500 meters away from you. Here's yournavigation route"]

測試結果:

Context: Check out thenearest hotel

He looks so happy:0.541198

The hotel is 500 dollars anight: 0.458123

The nearest hotel is 500meters away from you. Here's your navigation route: 0.433537

      最高分答案為He looks so happy

      (5)INPUT_CONTEXT = "Call a taxi for me"

      POTENTIAL_RESPONSES =["I've got a taxi for you. It will arrive in 5 minutes", "Thecar is worth 20 thousand dollars","Is that your car?"]

測試結果:

Context: Call a taxi forme

I've got a taxi for you.It will arrive in 5 minutes: 0.523356

The car is worth 20thousand dollars: 0.53095

Is that your car?:0.444959

      最高分答案為The car is worth 20 thousand dollars


      可以看到,正確率為0。

  • 英文知識型

      (1)INPUT_CONTEXT = "Is Shakespeare a male or afemale?"

      POTENTIAL_RESPONSES =["male", "female"]

測試結果:

Context: Is Shakespeare amale or a female?

male: 0.535856

female: 0.498352

     最高分答案為male

     (2)INPUT_CONTEXT = "Who wrote the lady of the camellias?"

      POTENTIAL_RESPONSES =["Alexandre Dumas.fils", "Shakespeare","leotolstoy"]

測試結果:

Context: Who wrote thelady of the camellias?

Alexandre Dumas.fils:0.587284

Shakespeare: 0.539719

leo tolstoy: 0.477065

      最高分答案為Alexandre Dumas.fils

      (3)INPUT_CONTEXT = "When British constitutional monarchywas established?"

      POTENTIAL_RESPONSES =["1688", "1840","1949"]

測試結果:

When Britishconstitutional monarchy was established?

1688: 0.517922

1840: 0.517922

1949: 0.517922

      得分都一樣,本題算錯

      (4)INPUT_CONTEXT = "What country does Shanghai belong to?"

     POTENTIAL_RESPONSES =["China", "USA","Spain"]

測試結果:

Context: What country doesShanghai belong to?

China: 0.503242

USA: 0.503242

Spain: 0.503242

      得分都一樣,本題算錯

      (5)INPUT_CONTEXT = "What's Michael Jordan's father's firstname?"

      POTENTIAL_RESPONSES =["Jordan", "Li","Michael"]

Context: What's MichaelJordan's father's first name?

Jordan: 0.5339

Li: 0.5339

Michael: 0.5339

      得分都一樣,本題算錯

 

      由上可知,(1)(2)為正確的,正確率為40%。

 

      綜上,chatbot對於閒聊和知識型問題準確率能做到40%,但是對於任務型問題存在很大的不足。

相關文章