Skip to content

Commit b699796

Browse files
committed
submit implementation
1 parent b264ca3 commit b699796

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,43 @@
1-
# cd_algorithm
2-
Code repository for under review paper.
1+
This project hosts the official implementation for our ICONIP 2020 paper:
2+
3+
Learning Discrete Sentence Representations via Construction & Decomposition [Springer Link](https://link.springer.com/chapter/10.1007/978-3-030-63830-6_66).
4+
5+
## Abstract
6+
In this paper, we address the problem of learning low-dimensional, discrete representations of real-valued vectors. We propose a new algorithm called similarity matrix construction and decomposition (C\&D). In the preparation phase, we constructively generate a set of consistent, unbiased and comprehensive anchor vectors, and obtain their low-dimensional forms with PCA. The C\&D algorithm learns the discrete representations of vectors in batches. For a batch of input vectors, we first construct a similarity matrix between them and the anchor vectors, and then learn their discrete representations from the similarity matrix decomposition, where the low-dimensional forms of the anchor vectors are regarded as a fixed factor of the similarity matrix. The matrix decomposition is a mixed-integer optimization problem. We obtain the optimal solution for each bit with mathematical derivation, and then use the discrete coordinate descent method to solve it. The C\&D algorithm does not learn directly discrete representations from the input vectors, which distinguishes it from other discrete learning algorithms. We evaluate the C\&D algorithm on sentence embedding compression tasks. Extensively experimental results reveal the C\&D algorithm outperforms the latest 4 methods and reaches state-of-the-art. Detailed analysis and ablation study further validate the rationality of the C\&D algorithm.
7+
8+
## Usage
9+
The experimental environment of C&D algorithm is consistent with [Shen et al..](https://github.com/Linear95/BinarySentEmb) This means that using this repository requires 3 simple steps:
10+
1. Set up the experimental environment according to the [Shen et al.](https://github.com/Linear95/BinarySentEmb) instructions.
11+
1. Add the CDBinEncoder class in discrete_encoders.py to the file with the same name in the [repository](https://github.com/Linear95/BinarySentEmb).
12+
1. Modify the evaluate.py in [repository](https://github.com/Linear95/BinarySentEmb) to evaluate the C&D algorithm.
13+
14+
## Citation
15+
If you find our work or code useful in your research, please consider citing:
16+
'''
17+
@inproceedings{DBLP:conf/iconip/SongZL20,
18+
author = {Haohao Song and
19+
Dongsheng Zou and
20+
Weijia Li},
21+
editor = {Haiqin Yang and
22+
Kitsuchart Pasupa and
23+
Andrew Chi{-}Sing Leung and
24+
James T. Kwok and
25+
Jonathan H. Chan and
26+
Irwin King},
27+
title = {Learning Discrete Sentence Representations via Construction {\&}
28+
Decomposition},
29+
booktitle = {Neural Information Processing - 27th International Conference, {ICONIP}
30+
2020, Bangkok, Thailand, November 23-27, 2020, Proceedings, Part {I}},
31+
series = {Lecture Notes in Computer Science},
32+
volume = {12532},
33+
pages = {786--798},
34+
publisher = {Springer},
35+
year = {2020},
36+
url = {https://doi.org/10.1007/978-3-030-63830-6\_66},
37+
doi = {10.1007/978-3-030-63830-6\_66},
38+
timestamp = {Fri, 20 Nov 2020 12:41:31 +0100}
39+
}
40+
'''
41+
If you have any questions, please contact me via issue or [email](songhaohao2018@cqu.edu.cn).
42+
43+

discrete_encoders.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import numpy as np
2+
3+
import torch
4+
5+
6+
class CDBinEncoder():
7+
def __init__(self, g, r): # g is the original input dimension, and r is the target dimension
8+
super(object, self).__init__()
9+
10+
self.fix_seed(37)
11+
12+
print('initia parameters... ...')
13+
self.g = g
14+
self.r = r
15+
16+
self.V = torch.from_numpy(self.generate_V(g, g * 5)).float().cuda()
17+
self.normed_V = (self.V / torch.norm(self.V, dim=0).unsqueeze(0)).cuda()
18+
19+
self.P = self.generate_P_svd(self.V, r).float().cuda()
20+
21+
self.V_p = (self.P @ self.V * np.sqrt(r)).float().cuda()
22+
self.inverse_V_p = torch.pinverse(self.V_p).float().cuda()
23+
24+
def fix_seed(self, seed):
25+
np.random.seed(seed)
26+
torch.manual_seed(seed)
27+
torch.cuda.manual_seed(seed)
28+
29+
def generate_V(self, num_rows, num_cols):
30+
limit = np.sqrt(2. / (num_rows + num_cols))
31+
random_matrix = np.random.normal(loc=0.0, scale=limit, size=(num_rows, num_cols))
32+
33+
emb_mean = np.mean(random_matrix, axis=0)[None, :]
34+
random_matrix -= emb_mean
35+
36+
return random_matrix
37+
38+
def generate_P_svd(self, V, r):
39+
u, sigma, v = torch.svd(V)
40+
return u[:r, :]
41+
42+
def generate_P(self, g, r):
43+
limit = np.sqrt(6. / (g + r))
44+
random_matrix = np.random.uniform(low=-limit, high=limit, size=(g, r))
45+
46+
u, sigma, v = np.linalg.svd(random_matrix)
47+
48+
return u[:r, :]
49+
50+
def dcd(self, S, U, V):
51+
L = U.shape[0]
52+
Q = (V @ S.t()).cuda()
53+
54+
while True:
55+
is_update = False
56+
for i in range(L):
57+
U_b_prime = torch.cat((U[:i, :], U[i + 1:, :]))
58+
59+
v_p = V[i, :]
60+
V_p_prime = torch.cat((V[:i, :], V[i + 1:, :]))
61+
62+
q = Q[i, :]
63+
64+
bracket_result = (q - U_b_prime.t() @ V_p_prime @ v_p).cuda()
65+
66+
new_u = bracket_result.sign().cuda()
67+
new_u[torch.eq(new_u, 0.)] = 1.
68+
69+
if torch.all(torch.eq(new_u, U[i, :])):
70+
continue
71+
U[i, :] = new_u
72+
is_update = True
73+
74+
if not is_update: break
75+
76+
return U.t().cpu().numpy()
77+
78+
def encode(self, X):
79+
X = torch.from_numpy(X).cuda()
80+
81+
normed_X = (X / torch.norm(X, dim=1).unsqueeze(1)).cuda()
82+
83+
S = (normed_X @ self.normed_V * self.r).cuda()
84+
85+
X_small_code = (S @ self.inverse_V_p).cuda()
86+
87+
return self.dcd(S, X_small_code.t(), self.V_p)

0 commit comments

Comments
 (0)