-
Notifications
You must be signed in to change notification settings - Fork 131
/
embeddings.py
80 lines (64 loc) · 2.58 KB
/
embeddings.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
# Copyright (C) 2016-2018 Mikel Artetxe <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from cupy_utils import *
import numpy as np
def read(file, threshold=0, vocabulary=None, dtype='float'):
header = file.readline().split(' ')
count = int(header[0]) if threshold <= 0 else min(threshold, int(header[0]))
dim = int(header[1])
words = []
matrix = np.empty((count, dim), dtype=dtype) if vocabulary is None else []
for i in range(count):
word, vec = file.readline().split(' ', 1)
if vocabulary is None:
words.append(word)
matrix[i] = np.fromstring(vec, sep=' ', dtype=dtype)
elif word in vocabulary:
words.append(word)
matrix.append(np.fromstring(vec, sep=' ', dtype=dtype))
return (words, matrix) if vocabulary is None else (words, np.array(matrix, dtype=dtype))
def write(words, matrix, file):
m = asnumpy(matrix)
print('%d %d' % m.shape, file=file)
for i in range(len(words)):
print(words[i] + ' ' + ' '.join(['%.6g' % x for x in m[i]]), file=file)
def length_normalize(matrix):
xp = get_array_module(matrix)
norms = xp.sqrt(xp.sum(matrix**2, axis=1))
norms[norms == 0] = 1
matrix /= norms[:, xp.newaxis]
def mean_center(matrix):
xp = get_array_module(matrix)
avg = xp.mean(matrix, axis=0)
matrix -= avg
def length_normalize_dimensionwise(matrix):
xp = get_array_module(matrix)
norms = xp.sqrt(xp.sum(matrix**2, axis=0))
norms[norms == 0] = 1
matrix /= norms
def mean_center_embeddingwise(matrix):
xp = get_array_module(matrix)
avg = xp.mean(matrix, axis=1)
matrix -= avg[:, xp.newaxis]
def normalize(matrix, actions):
for action in actions:
if action == 'unit':
length_normalize(matrix)
elif action == 'center':
mean_center(matrix)
elif action == 'unitdim':
length_normalize_dimensionwise(matrix)
elif action == 'centeremb':
mean_center_embeddingwise(matrix)