"tests/tools/utils.py" did not exist on "08b60eb1628ef91a29a14de33b046f8f19808531"
Capsule Tutorial(WIP).ipynb 5.32 KB
Newer Older
VoVAllen's avatar
VoVAllen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Capsule Network\n",
    "================\n",
    "**Author**: `Jinjing Zhou`\n",
    "\n",
    "This tutorial explains how to use DGL library and its language to implement the [capsule network](http://arxiv.org/abs/1710.09829) proposed by Geoffrey Hinton and his team. The algorithm aims to provide a better alternative to current neural network structures. By using DGL library, users can implement the algorithm in a more intuitive way."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model Overview\n",
    "\n",
    "### Introduction\n",
    "Capsule Network is \n",
    "\n",
    "### What's a capsule?\n",
    "> A capsule is a group of neurons whose activity vector represents the instantiation parameters of a specific type of entity such as an object or an object part.    \n",
    "\n",
    "Generally Speaking, the idea of capsule is to encode all the information about the features in a vector form, by substituting scalars in traditional neural network with vectors. And use the norm of the vector to represents the meaning of original scalars. \n",
    "![figure_1](./capsule_f1.png)\n",
    "\n",
    "### Dynamic Routing Algorithm\n",
    "<img src=\"./capsule_f2.png\" style=\"height:300px;\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model Implementations\n",
    "\n",
    "### 1. Consider capsule routing  as a graph structure\n",
    "\n",
    "We can consider each capsule as a node in a graph, and connect the nodes between layers.\n",
    "<img src=\"./capsule_f3.png\" style=\"height:200px;\"/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def construct_graph(self):\n",
    "    g = dgl.DGLGraph()\n",
    "    g.add_nodes(self.in_channel + self.num_unit)\n",
    "    self.in_channel_nodes = list(range(self.in_channel))\n",
    "    self.capsule_nodes = list(range(self.in_channel, self.in_channel + self.num_unit))\n",
    "    u, v = [], []\n",
    "    for i in self.in_channel_nodes:\n",
    "        for j in self.capsule_nodes:\n",
    "            u.append(i)\n",
    "            v.append(j)\n",
    "    g.add_edges(u, v)\n",
    "    return g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Pre-compute $\\hat{u}_{j|i}$, initialize $b_{ij}$ and store them as edge attribute\n",
    "<img src=\"./capsule_f4.png\" style=\"height:200px;\"/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = x.transpose(1, 2)\n",
    "x = torch.stack([x] * self.num_unit, dim=2).unsqueeze(4)\n",
    "W = self.weight.expand(self.batch_size, *self.weight.shape)\n",
    "u_hat = torch.matmul(W, x).permute(1, 2, 0, 3, 4).squeeze().contiguous()\n",
    "self.g.set_e_repr({'b_ij': edge_features.view(-1)})\n",
    "self.g.set_e_repr({'u_hat': u_hat.view(-1, self.batch_size, self.unit_size)})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Initialize node features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_features = torch.zeros(self.in_channel + self.num_unit, self.batch_size, self.unit_size).to(device)\n",
    "self.g.set_n_repr({'h': node_features})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Write message passing functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@staticmethod\n",
    "def capsule_msg(src, edge):\n",
    "    return {'b_ij': edge['b_ij'], 'h': src['h'], 'u_hat': edge['u_hat']}\n",
    "\n",
    "@staticmethod\n",
    "def capsule_reduce(node, msg):\n",
    "    b_ij_c, u_hat = msg['b_ij'], msg['u_hat']\n",
    "    # line 4\n",
    "    c_i = F.softmax(b_ij_c, dim=0)\n",
    "    # line 5\n",
    "    s_j = (c_i.unsqueeze(2).unsqueeze(3) * u_hat).sum(dim=1)\n",
    "    return {'h': s_j}\n",
    "\n",
    "def capsule_update(self, msg):\n",
    "    # line 6\n",
    "    v_j = self.squash(msg['h'])\n",
    "    return {'h': v_j}\n",
    "\n",
    "def update_edge(self, u, v, edge):\n",
    "    # line 7\n",
    "    return {'b_ij': edge['b_ij'] + (v['h'] * edge['u_hat']).mean(dim=1).sum(dim=1)}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Executing algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(self.num_routing):\n",
    "    self.g.update_all(self.capsule_msg, self.capsule_reduce, self.capsule_update)\n",
    "    self.g.update_edge(edge_func=self.update_edge)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}