摘要:本文將先簡單介紹Bandit 問題和本地差分隱私的相關背景,而後介紹基於本地差分隱私的 Bandit 算法,最後經過一個簡單的電影推薦場景來驗證 LDP LinUCB 算法。
老虎機(Bandit)問題是強化學習中一類重要的問題,因爲它定義簡潔且有大量的理論分析,所以被普遍應用於新聞推薦,醫學試驗等實際場景中。隨着人類進入大數據時代,用戶對自身數據的隱私性日益重視,這對機器學習算法的設計提出了新的挑戰。爲了在保護隱私的狀況下解決 Bandit 這一經典問題,北京大學和華爲諾亞方舟實驗室聯合提出了基於本地差分隱私的 Bandit 算法,論文已被 NeurIPS 2020 錄用,代碼已基於 MindSpore 開源首發。html
本文將先簡單介紹 Bandit 問題和本地差分隱私的相關背景,而後介紹基於本地差分隱私的 Bandit 算法,最後經過一個簡單的電影推薦場景來驗證 LDP LinUCB 算法。git
你們都有過這樣的經歷,在咱們刷微博或是讀新聞的時候,常常會看到一些系統推薦的內容,這些推薦的內容是根據用戶對過往推薦內容的點擊狀況以及閱讀時長等反饋來產生的。在這個過程裏,系統事先不知道用戶對各類內容的偏好,經過不斷地與用戶進行交互(推薦內容 — 獲得反饋),來慢慢學習到用戶的偏好特徵,不斷提升推薦的精準性,從而最大化用戶的價值,這就是一個典型的 Bandit 問題。算法
Bandit 問題有 context-free 和 contextual 兩種常見的設定,下面給出它們具體的數學定義。app
【Context-Free Bandit】
【Contextual Bandit】
傳統的差分隱私技術(Differential Privacy,DP)是將用戶數據集中到一個可信的數據中心,在數據中心對用戶數據進行匿名化使其符合隱私保護的要求後,再分發給下游使用,咱們將其稱之爲中心化差分隱私。可是,一個絕對可信的數據中心很難找到,所以人們提出了本地差分隱私技術(Local Differential Privacy,LDP),它直接在客戶端進行數據的隱私化處理後再提交給數據中心,完全杜絕了數據中心泄露用戶隱私的可能。dom
Context-Free Bandit
咱們能夠證實,上述算法有以下的性能:機器學習
根據上述定理,咱們只需將任一非隱私保護的算法按照算法 1 進行改造,就當即能夠獲得對應的隱私保護版本的算法,且它的累積 regret 的理論上界和非隱私算法只相差一個函數
因子,所以算法 1 具備很強的通用性。咱們將損失函數知足不一樣凸性和光滑性條件下的 regret 簡單羅列以下:性能
上述算法和結論能夠擴展到每一輪能觀測多個動做損失值的狀況, 感興趣的能夠參見論文(https://arxiv.org/abs/2006.00701)。學習
Contextual Bandit
【定理】 依照至少爲測試
的機率,LDP LinUCB 算法的 regret 知足如
上述算法和結論能夠擴展到 gg 不是恆等變換的狀況, 感興趣的能夠參見論文(https://arxiv.org/abs/2006.00701)。
MovieLens 是一個包含多個用戶對多部電影評分的公開數據集,咱們能夠用它來模擬電影推薦。咱們經過src/dataset.py 來構建環境:咱們從數據集中抽取一部分有電影評分數據的用戶,而後將評分矩陣經過 SVD 分解來補全評分數據,並將分數歸一化到[−1,+1]。在每次交互的時候,系統隨機抽取一個用戶,推薦算法得到特徵,並選擇一部電影進行推薦,MovieLensEnv會在打分矩陣中查詢該用戶對電影對評分並返回,從而模擬用戶給電影打分。
class MovieLensEnv: def observation(self): """random select a user and return its feature.""" sampled_user = random.randint(0, self._data_matrix.shape[0] - 1) self._current_user = sampled_user return Tensor(self._feature[sampled_user]) def current_rewards(self): """rewards for current user.""" return Tensor(self._approx_ratings_matrix[self._current_user])
import mindspore.nn as nn class LinUCB(nn.Cell): def __init__(self, context_dim, epsilon=100, delta=0.1, alpha=0.1, T=1e5): ... # Parameters self._V = Tensor(np.zeros((context_dim, context_dim), dtype=np.float32)) self._u = Tensor(np.zeros((context_dim,), dtype=np.float32)) self._theta = Tensor(np.zeros((context_dim,), dtype=np.float32))
每來一個用戶,LDP LinUCB 算法根據用戶和電影的聯合特徵x基於當前的模型來選擇最優的電影a_max作推薦,並傳輸帶噪聲的更新量:
import mindspore.nn as nn class LinUCB(nn.Cell):... def construct(self, x, rewards): """compute the perturbed gradients for parameters.""" # Choose optimal action x_transpose = self.transpose(x, (1, 0)) scores_a = self.squeeze(self.matmul(x, self.expand_dims(self._theta, 1))) scores_b = x_transpose * self.matmul(self._Vc_inv, x_transpose) scores_b = self.reduce_sum(scores_b, 0) scores = scores_a + self._beta * scores_b max_a = self.argmax(scores) xa = x[max_a] xaxat = self.matmul(self.expand_dims(xa, -1), self.expand_dims(xa, 0)) y = rewards[max_a] y_max = self.reduce_max(rewards) y_diff = y_max - y self._current_regret = float(y_diff.asnumpy()) self._regret += self._current_regret # Prepare noise B = np.random.normal(0, self._sigma, size=xaxat.shape) B = np.triu(B) B += B.transpose() - np.diag(B.diagonal()) B = Tensor(B.astype(np.float32)) Xi = np.random.normal(0, self._sigma, size=xa.shape) Xi = Tensor(Xi.astype(np.float32)) # Add noise and update parameters return xaxat + B, xa * y + Xi, max_a
系統收到更新量以後,更新模型參數以下:
import mindspore.nn as nn class LinUCB(nn.Cell):... def server_update(self, xaxat, xay): """update parameters with perturbed gradients.""" self._V += xaxat self._u += xay self.inverse_matrix() theta = self.matmul(self._Vc_inv, self.expand_dims(self._u, 1)) self._theta = self.squeeze(theta)
咱們測試不一樣的 \varepsilonε 對累積 regret 對影響:
- x 軸:交互輪數
- y 軸:累積 regret
相關模型代碼已上線 MindSpore Model Zoo:https://gitee.com/mindspore/mindspore/tree/master/model_zoo感興趣的可自行體驗。
1. Kai Zheng, Tianle Cai, Weiran Huang, Zhenguo Li, Liwei Wang. "Locally Differentially Private (Contextual) Bandits Learning." Advances in Neural Information Processing Systems. 2020.
2. LDP LinUCB 代碼:
https://gitee.com/mindspore/mindspore/tree/master/model_zoo/research/rl/ldp_linucb
本文分享自華爲雲社區《MindSpore 首發:隱私保護的 Bandit 算法,實現電影推薦》,原文做者:chengxiaoli。