推薦系統提供web服務的2種方式(二十四)

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"寫在前面:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家好,我是強哥,一個熱愛分享的技術狂。目前已有 12 年大數據與AI相關項目經驗, 10 年推薦系統研究及實踐經驗。平時喜歡讀書、暴走和寫作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"業餘時間專注於輸出大數據、AI等相關文章,目前已經輸出了40萬字的推薦系統系列精品文章,今年 6 月底會出版「構建企業級推薦系統:算法、工程實現與案例分析」一書。如果這些文章能夠幫助你快速入門,實現職場升職加薪,我將不勝歡喜。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想要獲得更多免費學習資料或內推信息,一定要看到文章最後喔。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"內推信息","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你正在看相關的招聘信息,請加我微信:liuq4360,我這裏有很多內推資源等着你,歡迎投遞簡歷。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"免費學習資料","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想獲得更多免費的學習資料,請關注同名公衆號【數據與智能】,輸入“資料”即可!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"學習交流羣","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想找到組織,和大家一起學習成長,交流經驗,也可以加入我們的學習成長羣。羣裏有老司機帶你飛,另有小哥哥、小姐姐等你來勾搭!加小姐姐微信:epsila,她會帶你入羣。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"推薦系統是一種信息過濾技術,通過從用戶行爲中挖掘用戶興趣偏好,爲用戶提供個性化的信息,減少用戶的找尋時間,降低用戶的決策成本,讓用戶更加被動地消費信息。推薦系統是伴隨着互聯網技術的發展及應用深入而出現的,並在當前得到廣泛的關注,它是一種軟件解決方案,是toC互聯網產品上的一個模塊。用戶通過與推薦模塊交互,推薦系統通過提供的web服務,將與用戶興趣匹配的標的物篩選出來,組裝成合適的數據結構,最終展示給用戶。推薦系統web服務是前端和後端溝通的橋樑,是推薦結果傳輸的最後通道。信息傳輸是否通暢,傳輸是否足夠快速,對用戶體驗是有極大影響的。本章我們就來講解推薦系統提供web服務的兩種主要方式,這兩種方式是企業級推薦系統最常採用的兩種形式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"具體來說,本章我們會從什麼是推薦系統web服務、推薦系統提供web服務的2種方式、事先計算型web服務、實時裝配型web服務、兩種web服務方式的優劣對比、影響web服務方案的因素及選擇原則等6個部分來講解。通過本章的介紹,期望讀者可以深刻理解這2種web服務方式的具體實現方案以及它們之間的差別,並具備結合具體的業務場景來決策採用哪種方式的能力。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.1 什麼是推薦系統web服務","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者在第《構建優質的推薦系統服務》中已經對推薦系統web服務進行了簡單介紹,這裏爲了讓讀者更好地理解本文的知識點,以及爲了內容的完整性,對推薦系統web服務進行簡略介紹。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用戶與推薦系統交互的服務流程見下面圖1,用戶在使用產品過程中與推薦模塊(產品上提供推薦能力的功能點)交互,前端(手機、PC、Pad、智能電視等)請求推薦web服務,推薦web服務獲取該用戶的推薦結果,將推薦結果返回給前端,前端通過適當的渲染將最終的推薦結果按照一定的樣式和排列規則在產品上展示出來,這時用戶就可以看到推薦系統給他的推薦結果了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e0/e042d65492cab0ac9dff6ff4c8ce75fa.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖1:用戶通過推薦web服務獲取推薦結果的數據交互流程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上圖中的綠色虛線框中的數據交互能力就是推薦web服務的範疇,它是前端(也叫終端)與後端的互動。圖中藍色方塊(推薦web服務模塊)是部署在服務器上的一類軟件服務,它提供HTTP接口,讓前端可以實時與之交互。用戶與終端的交互屬於視覺及交互設計範疇,雖然與推薦web服務無直接關係,但是是整個推薦服務能力完整實現必不可少的一環,也是用戶可以肉眼直接感知到的部分,在整個推薦系統中非常重要,對推薦系統發揮價值有極大影響,不過不在本章的討論範圍內,我們會在第24章《推薦系統的UI交互與視覺展示》詳細講解這方面的知識點。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了給前端提供個性化推薦服務,上圖中的推薦web服務模塊需要完成3件事情。首先需要獲得該用戶的推薦結果(直接獲得已經計算好的推薦結果,這就是21.3節要講的,或者通過臨時計算獲得推薦結果,這就是第21.4節要講的),其次是將結果組裝成前端最終需要的數據結構(第一步獲得的推薦結果一般是標的物id的列表,實際展示給前端還需要標的物的各種metadata信息,如名稱,價格,海報圖等,這些信息的組裝就是在這一步完成的,這些信息一般會存放到關係型數據庫中,或者採用json的形式存放到Redis、文檔型NoSQL中,所以這裏至少還有一次額外的數據庫訪問),最後是響應前端的HTTP請求(一般是GET請求),將最終推薦結果返回給前端。本文我們講解的推薦系統提供web服務的兩種方式,就是這裏講的第一件事情,即推薦web服務怎麼獲得給用戶的推薦結果。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"推薦web服務模塊是最終爲用戶提供推薦能力的部分,它設計得好不好直接影響用戶體驗,一般來說,該模塊需要滿足穩定、響應及時、容錯、可以隨着用戶規模線性擴容等多個條件,具體什麼是優質的推薦服務,我們在第20章《構建優質的推薦系統服務》已經做過深入講解。這裏提一下,隨着Docker等容器技術及kubernetes等容器管理軟件的發展和成熟,推薦web服務中的各個子模塊都可以分別部署在容器中,採用微服務的方式進行數據交互,這樣就可以高效管理這些服務,更好地進行服務的監控、錯誤恢復、線性擴容等。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上圖只是一種簡化的交互模型,在實際企業級服務中,往往比這個更加複雜,在前端和後端之間往往存在一層CDN層做緩存加速,以減輕前端服務對後端併發訪問的壓力(在用戶量大的情況下,推薦系統屬於高併發服務),並且一般推薦web服務中還存在一層Nginx代理層,通過Nginx代理,讓推薦web服務可以水平擴容,以滿足推薦系統高併發的要求。下面圖2就是一種可行的完整推薦系統服務方案。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9c/9ca13ab253abc45a92e2f8f4ae5d1cb9.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖2:完整的推薦系統業務架構圖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如前面所講,雖然推薦web服務包含前端與後端的交互,前端與後端一般還會有CDN層和Nginx代理層,但本文我們着重關注的是後端真正提供Web服務接口模塊及數據存儲模塊的實現方案,也即上圖中紅色模塊怎麼獲取推薦結果的架構實現方案。該模塊的實現方案可以多樣,主流的實現方式有兩種,我們在下面分三節來進行介紹。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.2 推薦系統提供web服務的兩種方式","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"推薦系統提供web服務一般有兩種方式,一種是事先計算型,另一種是實時裝配型。在具體介紹之前,這裏我先舉一個比較形象的例子,讓大家更好地理解這兩種實現方式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設我們開了一家餐廳專門送外賣,餐廳提供10種不同的備選套餐。在午市或者晚市叫餐高峯時段,餐廳可以採用如下兩種方案來準備套餐:第一種方案是事先將這10種套餐每種都做若干份,當有客戶叫外賣時,將該客戶叫的這個套餐(已經做好了)直接送出去;第二種方式是將這10種套餐需要的原材料都準備好,部分材料做成半成品(比如比較花時間的肉類),當有用戶叫餐時,將該套餐需要的原材料下鍋快速做好再送出去。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過上面非常簡化的案例介紹,大家應該不難理解,上面提到的第一種準備套餐的方式就是“事先計算型”,事先將套餐做好,而第二種方式就是“實時裝配型”,當用戶叫餐時,臨時做並快速做好。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在讓我們回到推薦web服務上,來介紹兩種推薦web服務方案。事先計算型就是將用戶的推薦結果事先計算好,放到數據庫中存放起來,當該用戶在使用產品過程中訪問推薦模塊時,推薦web服務模塊直接將該用戶計算好的推薦結果取出來,進行適當加工(比如過濾掉用戶已經看過的視頻),將最終推薦結果展示給用戶。實時裝配型是將計算推薦結果需要的數據(一般是各種特徵)提前準備好,當用戶訪問推薦模塊時,推薦web服務通過簡單的計算和組裝(利用前面準備好的各種特徵灌入推薦模型),生成該用戶的推薦結果,再將推薦結果返回給前端並展示給用戶。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"理解了這兩種不同的web服務方式的基本原理,我們在接下來的兩節中分別對它們的實現細節進行詳細介紹,讓讀者更好地理解它們的特性及技術實現細節。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.3 事先計算型web服務","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這一節我們來講解推薦系統事先計算型web服務的架構實現與基本原理(參見下面圖3)。這種方式可能是業界比較多地採用的一種推薦web服務架構實現方式,作者公司的所有推薦服務基本都是採用的該模式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ba/baa77ae7b86ba11861e532e504f91297.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖3:事先計算型web服務架構(綠色虛線框中的模塊即是圖2中的紅色模塊的細化)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"該模式最大的特點是事先將每個用戶的推薦結果計算出來,存到數據庫中(一般是NoSQL,如Redis、CouchBase等NoSQL數據庫,採用key-value的方式存儲,key就是用戶id,value就是給用戶的推薦結果,如果是用Redis存,value的數據結構可以使Sorted Sets,這種數據結構比較適合推薦系統,Sorted Sets中的element可以是推薦的標的物id,score是標的物的預測評分或者預測概率值等,還可以根據Sorted Sets中的score進行分頁篩選等操作),當有用戶請求時,前端訪問web接口服務器(前端會帶上用戶的唯一識別id進行HTTP請求,這樣就知道是哪個用戶,方便找到該用戶的推薦結果),web服務器從推薦結果庫中獲取該用戶的推薦結果(推薦結果一般只存儲給用戶推薦的標的物id列表及部分需要的其他信息,比如算法標識,方便後面做AB測試,下面圖4就是一種推薦結果存儲的數據格式,其中id就是標的物的唯一識別id),同時還需要訪問標的物metadata數據庫(一般存放在關係型數據庫中),將前端展示需要的其他信息(如標的物的名稱、價格、縮略圖等)拼接完整,最終以json的形式(下面圖5就是視頻推薦系統最終拼接好的json格式,互聯網企業一般採用的數據交互協議,也可以是其他協議,Google內部就採用protobuf協議)返回給前端展示給用戶。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/41/417fd861be0469faf271ab8da6b82410.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖4:推薦結果存儲的數據結構(json形式存儲)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3b/3beb25bd8b6a51885aaaabf3572b9ccc.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖5:最終返回給用戶的推薦結果(json格式)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"該架構既可以支持T+1推薦模式和實時推薦模式,對於T+1型推薦產品形態,每天爲用戶生成一次推薦結果,生成推薦結果時直接替換昨天的推薦結果就可以了。而對於實時推薦,情況會複雜一些,實時推薦可能會調整用戶的推薦結果(而不是完全替換),對用戶推薦結果進行增刪形成新的推薦結果,這時可行的方法有兩個:一是從推薦結果存儲數據庫中讀出該用戶的推薦結果,按照實時推薦算法邏輯對推薦結果進行修改,再將推薦結果存進去替換掉;另外一種做法是,增加一箇中間的鏡像存儲(可以採用HBase等,現在業界很多推薦算法都基於Hadoop/Spark平臺實現,用大數據生態系的HBase是比較好的選擇),所有的算法邏輯修改只對鏡像存儲進行操作,操作完成後,將鏡像存儲中修改後的推薦結果同步到最終的推薦庫中,這跟T+1更新就保持一致了,只不過現在是實時推薦,同一個用戶可能一天會更新多次推薦結果。作者公司的短視頻實時推薦更新就是採用後面的這種方案,我們會在第28章《基於標籤的實時短視頻推薦系統》28.3.1節進行詳細介紹。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.4 實時裝配型web服務","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本節我們來講解實時裝配型web服務的實現原理與架構(參考下面圖6)。這種方式事先不計算用戶的推薦結果,當有用戶請求時,web接口服務器從特徵數據庫(一般也是存放在Redis、HBase這種非關係型數據庫中)中將該用戶需要的特徵取出來,並將特徵灌入推薦模型,獲得該用戶的推薦結果,跟事先計算型一樣,還需要加載推薦標的物的metadata信息,拼接成完整的推薦結果,並返回給前端展示給用戶。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/34/341d7260944cec976f96b13bb5983b51.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖6:實時裝配型web服務架構(web接口服務加載推薦模型)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"該web服務架構需要將推薦模型加載到web接口服務中,可以實時基於用戶特徵獲得推薦結果,這就要求推薦模型可以在極短的時間(毫秒級)內獲得推薦結果,計算一定要快,否則會影響用戶體驗。當然另外一種可行的方案是,將推薦模型做成獨立的web模型服務,web接口服務通過HTTP或者RPC訪問模型服務獲得推薦結果。具體架構如下面圖7,這種方式的好處是推薦模型服務跟web服務解耦,可以分別獨立升級模型服務和推薦接口服務,互相之間不會影響,只要保證它們之間數據交互的協議不變就可以了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/84/84b125582e58ee3ba796b64d5c822383.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖7:通過推薦模型服務來獲取推薦結果的實時裝配型web服務架構","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實時裝配型架構在實際提供推薦服務時就與具體的推薦範式是T+1推薦還是實時推薦沒有關係了,因爲在任何時候web接口服務都是臨時調用推薦模型爲用戶生成推薦結果,只不過T+1推薦的模型可以一天訓練一次,而實時推薦的模型是實時訓練的(用戶的每一次操作行爲都會產生日誌,通過實時日誌處理,生成實時特徵,灌入實時模型訓練流程中,最終完成對模型的實時訓練,讓模型實時得到更新)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"業界流行的TensorFlow Serving就是一種實時裝配型服務架構,它提供web服務的架構模式類似上面圖6的形式,下面對其進行簡單介紹,讓讀者更好地理解這種模式。讀者可以查看參考資料1、2、3對TensorFlow Serving進行更深入的瞭解。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TensorFlow Serving是一個靈活的、高性能的機器學習模型在線服務框架,設計用於生產系統,可以與訓練好的TensorFlow模型高效整合,將訓練好的模型部署到線上,使用gRPC作爲接口接受外部調用。TensorFlow Serving支持模型熱更新與自動模型版本管理。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下圖爲TensorFlow Serving整個框架圖。Client端會不斷給Manager發送請求,Manager會根據版本管理策略管理模型更新,並將最新的模型計算結果返回給Client端。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9b/9b1503b8f1fc91a99cc93046f023e543.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"size","attrs":{"size":10}}],"text":"圖8:TensorFlow Serving架構,圖片來源於TensorFlow Serving官方文檔","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FaceBook開源的FAISS(見參考資料4)框架也是業界用的比較多的一款用於實時裝配型web服務的框架。FAISS包含幾種相似性搜索方法,它假設用戶或者標的物被表示爲向量並由整數標識(用戶和標的物用整數來唯一標識,即用戶id和標的物id),可以在海量向量庫中搜索出按照某種相似性計算的最相似的向量列表。FAISS提供了向量之間計算L2(歐幾里德)距離或內積距離的方法,與查詢向量最相似的向量是那些與查詢向量具有最小L2距離或最大內積的向量。FAISS具備在極短的時間(毫秒級)內計算某個向量最相似的一組向量的能力。它還支持cosine餘弦相似性查詢,因爲cosine餘弦只不過是向量內積的歸一化。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FAISS之所以能夠用於推薦系統提供實時推薦服務,主要是因爲很多推薦算法最終將用戶和標的物都表示爲向量,通過用戶向量與標的物向量的內積來衡量用戶對標的物的偏好程度,典型的矩陣分解算法就是這種形式。FAISS所起的作用相當於圖7中的推薦模型服務,利用它進行推薦的web服務架構就是圖7這種架構。最終的推薦模型用數學公式表示就是","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c3/c3bda93621360a53b285cf7629bb43c8.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":",","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/44/44fb5620d7cd925189dd04a9867540a2.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 是內積計算,u、v分別是用戶和標的物標向量,它們之間的內積表示用戶對標的物的偏好程度。FAISS提供計算用戶最相似的標的物的能力,並基於該相似度降序排列,取TopN最相似的標的物作爲最終的推薦結果。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.5 兩種web服務方式的優劣對比","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前面兩節已經對兩種推薦系統提供web服務的方案的技術細節進行了詳細介紹,在真實業務場景中可能比這個更復雜,可能不是單純的某種方案,會有一些變體,在這兩種方案的基礎上做適當調整與變化,可能同一產品的不同推薦形態採用不同的方式,同一種推薦方案也可能會融合這兩種方式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在這一節我們對比一下這兩個方案的優缺點,讓讀者更好地理解這兩種web服務方案,同時也爲讀者在具體推薦業務場景中進行選擇提供參考。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.5.1 事先計算型web服務的優缺點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事先計算型最大的優勢是提前將推薦結果準備好了,這樣在提供推薦服務時可以直接獲取推薦結果,因此大大提升了接口服務的響應速度,減少了響應時間,對用戶體驗是有極大幫助的。另外,事先計算好了,當模型推斷出現問題(比如調度模型推斷的計算服務掛了),最壞的情況是不更新推薦結果(這時無法插入最新推薦結果),用戶訪問時還是可以獲得推薦的,只不過給用戶的是過去一天的推薦結果。如果是實時計算推薦結果(實時裝配型),當模型出現問題時就無法獲得推薦結果,如果接口沒做很好的保護,這時接口可能會掛掉,導致前端出現無法展示任何推薦結果的故障,出現開天窗現象(不過好的推薦系統web服務一般會增加保護,在這種極端情況下,給定一組默認數據作爲推薦結果,默認推薦是提前緩存在前端的,不受短期網絡故障影響),因此,有更好的魯棒性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事先計算型另一個優點是架構更加簡單,web接口服務跟生成推薦過程解耦,可以分別對web接口和推薦結果計算優化升級,而不會互相影響。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事先計算型最大的缺點是,由於要事先爲每個用戶生成推薦,實際上很多用戶不是每天都訪問的,真正日活用戶佔總活躍用戶(比如月活用戶)的比例是很低的(當然像微信這類國民級APP除外),推薦模塊訪問用戶數一般也遠小於當天日活數,這就浪費了很多計算和存儲資源,特別是有海量用戶的APP,這時有大量的用戶沒有登錄反而需要每天爲其計算推薦結果,這時浪費是非常明顯的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事先計算型另外一個缺點是,事先計算好了,這就失去了靈活性,要調整修改用戶的推薦結果成本代價更高(信息流推薦等實時推薦產品是需要對推薦結果進行近實時調整)。就像前面的案例講的,套餐做好了,沒法滿足用戶特定的口味了,比如用戶想要重辣,那也沒辦法了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.5.2 實時裝配型web服務的優缺點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實時裝配型跟事先計算型基本是對稱的,事先計算型的優點是它的缺點,事先計算型的缺點反而是它的優點。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實時裝配型需要臨時爲用戶生成推薦結果,因此web接口服務需要多做一步處理,對接口性能有一定負面影響。另外,當推薦模型需要升級調整或者模型服務出現問題時(實時裝配型另一種實現方案是推薦模型作爲一個獨立web服務),會有短暫時間的不可用,這時會導致推薦web接口無法計算出推薦結果,進而無法給前端提供反饋信息。這兩種情況都會影響用戶體驗(當然做得好的系統會有模型熱更新,模型升級不會導致無法響應的情況出現,TensorFlow Serving就具備這種能力)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實時裝備型的架構也更加複雜,耦合度更高(在推薦web接口整合了推薦模型這種實時裝配型中,推薦web接口跟推薦結果計算是完全耦合在一起的,參見圖6)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實時裝配型由於是實時爲用戶計算推薦結果,因此相比事先計算型不會佔用太多的存儲、計算資源,對於節省費用是有極大幫助的,特別是在海量用戶場景下,這種節省更加明顯。它的另一個優點是對推薦結果調整空間大,因爲是臨時計算,可以在計算過程中增加一些場景化的處理邏輯,對推薦算法有更好的干預能力,更加適合實時推薦場景。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面介紹完了這兩種方案的優缺點,我們用一個表格整理一下,方便對比查看它們之間的異同點。","attrs":{}}]},{"type":"embedcomp","attrs":{"type":"table","data":{"content":"

推薦web服務類型

優點

缺點

事先計算型

1. 接口響應更快

2. 整個系統有更好的魯棒性,推薦計算出問題不影響接口返回結果

3. 架構更加簡單,耦合度低,可以對接口和推薦計算分別優化升級

1. 浪費計算存儲資源

2. 對推薦結果調整的靈活度低

實時裝配型

1. 更省存儲計算資源

2. 系統更靈活,可以方便臨時調整推薦邏輯

1. 接口有更多的處理邏輯,響應相對較慢

2. 當推薦模型或模型服務出現問題時,無法給用戶提供推薦,影響用戶體驗

3. 架構相對複雜,耦合度高,推薦接口和推薦結果計算存在直接依賴關係

"}}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"表1:事先計算型和實時裝配型的優缺點對比","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.6 影響web服務方案的因素及選擇原則","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上一節中,我們對兩種推薦web服務方案的優缺點進行了對比介紹,每種方案都有各自的優缺點,沒有哪一個方案是完全勝於另一個方案的。那麼在實際業務落地時,有哪些因素是會影響我們選擇具體的方案呢?我們應該怎樣選擇?有什麼判斷依據和準則嗎?在這一節中我們試圖從多個角度來回答這些問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.6.1 推薦產品形態的時效性對推薦web服務選擇的影響","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果推薦產品形態是T+1型推薦,由於每天只更新一次推薦結果,可以選擇事先計算型先將推薦結果計算出來。如果產品形態是實時信息流推薦,需要整合用戶的實時興趣變化,用戶的每一次行爲都會觸發更新推薦結果,這時採用臨時裝配型是更好的選擇。當然這也不是絕對的,作者公司的短視頻信息流推薦,就採用的事先計算型,事先計算型也可以做到近實時更新用戶推薦結果,前面已經提到,我們會在第28章《基於標籤的實時短視頻推薦系統》對算法原理進行詳細介紹。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.6.2 推薦團隊架構能力、工程實現能力對推薦web服務選擇的影響","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實時裝配型架構相對複雜,耦合度相對更高,在推薦時需要處理的邏輯更多,因此各個子模塊都需要相當穩定,並且需要具備較高的性能,因此對整個推薦軟件系統的要求更高。因此,如果推薦團隊架構能力強,人力比較充足的情況下可以選擇實時裝配型方案。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了更好地整合用戶的實時行爲,爲用戶提供可見即所得的推薦服務,很多信息流推薦需要對推薦算法進行實時訓練,比如Google在2013年推廣的FTRL算法就是對logistic在實時推薦場景下的工程實現,具備更高的工程實現難度,因此,對推薦團隊的工程實現能力是有較高要求的。實時裝配型一般需要處理用戶的實時行爲日誌,用於挖掘用戶實時興趣,構建實時模型,這就要求整個系統有更高的實時性,需要一套完善的實時處理架構體系的支撐, 這也增加了構建這類系統的複雜性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前面也提到實時計算型一般需要有一套類似FAISS這樣的實時匹配庫,爲用戶在極短的時間內搜尋到最喜歡的標的物。而搭建這樣一套系統,需要將推薦模型做成獨立的服務,並且保證推薦模型web服務具備穩定性、高併發、可拓展性等能力,這也對架構能力有極高要求。如果希望採用容器等新技術來更好地管理推薦模型服務,這也需要新的學習成本和運維成本。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.6.3 推薦階段對推薦web服務選擇的影響","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們知道企業級推薦系統生成推薦結果的過程一般分爲召回、排序兩個個階段(其實還包括業務調控,業務調控更多的是運營和策略性的調整,不屬於狹義的算法範疇,參考第4章《推薦算法概述》4.2節的介紹),先使用召回推薦算法從海量標的物中篩選出一組(一般幾百上千個)用戶可能感興趣的標的物,然後在排序階段利用更加精細化的推薦算法對結果進行重排序。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於召回是從所有標的物中篩選用戶可能感興趣的,當標的物數量龐大時(比如今日頭條有千億級文本、淘寶有上億級商品),即使召回算法簡單,計算量也是非常大的,一般可以採用事先計算型召回策略(爲了整合用戶最近的行爲,也可以基於用戶的興趣標籤或者用戶最近瀏覽的標的物進行近實時召回,這類召回策略也屬於事先計算型,比如根據用戶最近瀏覽的標的物召回相似的標的物,每個標的物相似標的物是事先計算好的)。而對於排序推薦算法,只需要從有限的(成百上千)的標的物中過濾出用戶最喜歡的幾十個,可以在較短時間內計算完,因此排序算法可以採用實時裝配型策略。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然,排序階段也是可以採用事先計算型的,這就相當於先召回,再排序將推薦結果計算好,只不過整個推薦過程將事先計算拆解爲召回和排序兩個階段來進行了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實,直接跟推薦接口銜接的是排序階段,召回階段是不直接參與web服務的,因此根據第二節的定義,嚴格意義上事先計算型、實時裝配型是不能用於描述召回階段的。不過有些產品的標的物數量不大(比如電影只有幾萬個),也可以將召回排序融合爲一個階段,只用一個算法就可以獲得推薦結果,或者排序可以採用簡單的規則和策略,這時排序邏輯可以整合到推薦web接口中,這兩種情況召回階段所起的作用就相當於排序階段的作用了,這時可以說召回直接跟web接口進行了交互,因此也可以用事先計算型、實時裝配型來描述召回階段。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"21.6.4 算法形態對推薦web服務選擇的影響","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"推薦算法種類繁多,從簡單的KNN、item-based協同過濾到複雜的深度學習、強化學習推薦算法,不同的算法實現方式、需要的數據來源、計算複雜度等都不一樣。這也導致了算法的使用場景不一樣。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"像深層深度學習這種模型結構非常複雜的推薦算法,即使爲單個標的物打分(即計算出用戶對標的物的偏好度),計算時間也是簡單算法的若干倍,這時在短時間內(比如100毫秒之內)爲大量的標的物打分是不現實的,因此這類算法一般用於排序階段(排序階段只對成百上千的標的物打分),因此比較適合實時裝配性的策略。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"簡單的推薦算法,如item-based協同過濾、矩陣分解,由於計算複雜度低,一般用於召回階段,因此是比較適合事先計算型的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本章講解了推薦系統提供web服務的兩種主要方式。一種是事先計算型,提前將用戶的推薦結果計算出來並存放到NoSQL中,當用戶使用推薦模塊時,推薦web服務直接將該用戶的推薦結果取出來並組裝成合適的數據格式最終在前端展示給用戶。另一種是實時裝配型,我們需要將計算推薦結果需要的原材料準備成“半成品”(就是各種特徵),將這些中間結果事先存起來,當用戶使用推薦服務時,推薦web服務通過簡單的組裝與計算(調用封裝好的推薦模型),將“半成品”加工成該用戶的推薦結果,並最終給到用戶。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這兩種提供web服務的推薦方案各有優缺點,我們需要根據公司現在的技術儲備、人員能力、團隊規模、產品形態等多個維度進行評估和選擇。不管採用哪種方式,最終的目的是一樣的,我們需要爲用戶提供個性化的、響應及時的優質推薦服務。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"參考資料","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1. [基於TensorFlow Serving的深度學習在線預估] ","attrs":{}},{"type":"link","attrs":{"href":"https://zhuanlan.zhihu.com/p/46591057","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"https://zhuanlan.zhihu.com/p/46591057","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2. [手把手教你使用TF服務將TensorFlow模型部署到生產環境] ","attrs":{}},{"type":"link","attrs":{"href":"https://zhuanlan.zhihu.com/p/60542828","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"https://zhuanlan.zhihu.com/p/60542828","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3. ","attrs":{}},{"type":"link","attrs":{"href":"https://www.tensorflow.org/serving","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"https://www.tensorflow.org/serving","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"4. ","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/facebookresearch/faiss","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"https://github.com/facebookresearch/faiss","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章