# Learning Heterogeneous Knowledge Base Embeddings for Explainable Recommendation
## 5 Recommendation Explanation with Knowledge Reasoning
### Explanation Path  
The key to generate an explanation of the recommendation is to find a sound logic inference sequence from the user to the item in the knowledge graph. In this work, we propose to find such a sequence by constructing an explanation path between the user and the item in the latent knowledge space.  
Formally, let \(E_{h}^{r}\) and \(E_{t}^{r}\) be the sets of all possible head entities and tail entities for a relation \(r\). We define an explanation path from entity \(e_{u}\) to entity \(e_{i}\) as two sets of relation \(R_{\alpha}=\{r_{\alpha}|\alpha\in[1,m]\}\) and \(R_{\beta}=\{r_{\beta}|\beta\in[1,n]\}\) such that  
\[\mathbf{e_{u}}+\sum_{\alpha=1}^{m}\mathbf{r_{\alpha}}=\mathbf{e_{i}}+\sum_{ \beta=1}^{n}\mathbf{r_{\beta}} \tag{9}\]where \(e_{u}\in E_{h}^{r_{a}}\) for \(\alpha=1\), \(e_{i}\in E_{h}^{r_{\beta}}\) for \(\beta=1\), \(E_{t}^{r_{a-1}}=E_{h}^{r_{a}}\) for \(\alpha\in[2,m]\), \(E_{t}^{r_{\beta-1}}=E_{h}^{r_{\beta}}\) for \(\beta\in[2,n]\), and \(E_{t}^{r_{a}}=E_{t}^{r_{\beta}}\) for \(\alpha=m,\beta=n\). In other words, there is an explanation path between \(e_{u}\) and \(e_{i}\) if there is an entity that can be inferred by both \(e_{u}\) (with \(R_{a}\)) and \(e_{i}\) (with \(R_{\beta}\)) with the observed relations in the knowledge graph.  
```
Input \(S=\{(E_{h},E_{t},r)\},e_{u},e_{i}\), maximum depth \(z\) Output:\(e_{x},R_{a},R_{\beta}\) Procedure Main() \(V_{u},P_{u},\mathcal{R}_{u}=BFS(S,e_{u},z)\). \(V_{i},P_{i},\mathcal{R}_{i}=BFS(S,e_{i},z)\). \(P\leftarrow\{\}\). for\(e\in V_{u}\cap V_{i}\)do \(P[e]=P_{u}(e)\cdot P_{i}(e)\).  end for Pick up \(e_{x}\in V_{u}\cap V_{i}\) with the largest \(P[e]\). \(R_{a}=\mathcal{R}_{u}[e_{x}]\), \(R_{\beta}=\mathcal{R}_{i}[e_{x}]\). return\(e_{x},R_{a},R_{\beta}\) FunctionBFS(\(S,e,z\)) \(V_{e}\leftarrow\) all entities in the entity set \(E_{t}\) within \(z\) hops from \(e\). \(P_{e}\leftarrow\) the probability of each entity in \(V_{e}\) computed by Equation (10). \(\mathcal{R}_{e}\leftarrow\) the paths from \(e\) to the space of each entity in \(V_{e}\). return\(V_{e},P_{e},\mathcal{R}_{e}\);
```  
**Algorithm 1**Recommendation Explanation Extraction  
For better illustration, we depict a recommendation example where an item (_iPad_) is recommended to a user (_[PERSON]_) in Figure 2. As we can see, the word \"IOS\" can be inferred by _iPad_ and _[PERSON]_ using the relation _Mention_; the brand _Apple_ can be inferred by _iPad_ using _Produced_by_, and by _[PERSON]_ using _Purchase_ + _Produced_by_. Thus, we have two explanation paths that link _[PERSON]_ with _iPad_.  
To generate explanations, we can create simple templates base on the relation type and apply them to the explanation paths. For example, we can say that _[PERSON]_ may be interested in _iPad_ because he often _mentions_ \"IOS\" in his reviews, and \"IOS\"is often _mentioned_ in the reviews of _iPad_; or that _[PERSON]_ may be interested in _iPad_ because he often _purchases_ products _produced by Apple_, and _iPad_ is also _produced_ by _Apple_. In these explanations, the italic words are entities and relations on the explanation path.