diff options
author | aoliverg <aoliverg@uoc.edu> | 2022-05-05 12:51:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-05 12:51:39 +0300 |
commit | 7ad0d648f8a01045672438fdacf59e230e4a9d4c (patch) | |
tree | 3e1c82ab512a8f8ca64ffd7e8f8d9f49cef3d091 | |
parent | 3552a7a3c919f28438811686ec2801ae7d2257dc (diff) |
Delete embeddings.py
-rw-r--r-- | embeddings.py | 80 |
1 files changed, 0 insertions, 80 deletions
diff --git a/embeddings.py b/embeddings.py deleted file mode 100644 index 9a407a5..0000000 --- a/embeddings.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright (C) 2016-2018 Mikel Artetxe <artetxem@gmail.com> -# -# 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) |