-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsgd.py
82 lines (65 loc) · 2.34 KB
/
rsgd.py
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
#!/usr/bin/env python3
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import torch as th
from torch.optim.optimizer import Optimizer, required
spten_t = th.sparse.FloatTensor
def poincare_grad(p, d_p):
r"""
Function to compute Riemannian gradient from the
Euclidean gradient in the Poincaré ball.
Args:
p (Tensor): Current point in the ball
d_p (Tensor): Euclidean gradient at p
"""
if d_p.is_sparse:
p_sqnorm = th.sum(
p.data[d_p._indices()[0].squeeze()] ** 2, dim=1,
keepdim=True
).expand_as(d_p._values())
n_vals = d_p._values() * ((1 - p_sqnorm) ** 2) / 4
d_p = spten_t(d_p._indices(), n_vals, d_p.size())
else:
p_sqnorm = th.sum(p.data ** 2, dim=-1, keepdim=True)
d_p = d_p * ((1 - p_sqnorm) ** 2 / 4).expand_as(d_p)
return d_p
def euclidean_grad(p, d_p):
#print("d_p")
#print(d_p)
return d_p
def euclidean_retraction(p, d_p, lr):
p.data.add_(-lr, d_p)
class RiemannianSGD(Optimizer):
r"""Riemannian stochastic gradient descent.
Args:
params (iterable): iterable of parameters to optimize or dicts defining
parameter groups
rgrad (Function): Function to compute the Riemannian gradient from
an Euclidean gradient
retraction (Function): Function to update the parameters via a
retraction of the Riemannian gradient
lr (float): learning rate
"""
def __init__(self, params, lr=required, rgrad=required, retraction=required):
defaults = dict(lr=lr, rgrad=rgrad, retraction=retraction)
super(RiemannianSGD, self).__init__(params, defaults)
def step(self, lr=None):
"""Performs a single optimization step.
Arguments:
lr (float, optional): learning rate for the current update.
"""
loss = None
for group in self.param_groups:
for p in group['params']:
if p.grad is None:
continue
d_p = p.grad.data
if lr is None:
lr = group['lr']
d_p = group['rgrad'](p, d_p)
group['retraction'](p, d_p, lr)
return loss