-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now, the model initialization part is working
- Loading branch information
1 parent
0aa3aaa
commit e121618
Showing
14 changed files
with
197 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import numpy as np | ||
import math | ||
|
||
class WordEmbeddingMap: | ||
def __init__(self, config): | ||
self.emb_dict = self.load(config) | ||
self.dim = self.emb_dict.shape[-1] | ||
|
||
def load(self): | ||
emb_matrix = None | ||
emb_dict = dict() | ||
for line in open(config.get_string("glove.matrixResourceName")): | ||
if not len(line.split()) == 2: | ||
if "\t" in line: | ||
delimiter = "\t" | ||
else: | ||
delimiter = " " | ||
line_split = line.rstrip().split(delimiter) | ||
# extract word and vector | ||
word = line_split[0] | ||
x = np.array([float(i) for i in line_split[1:]]) | ||
vector = (x /np.linalg.norm(x)) | ||
embedding_size = vector.shape[0] | ||
emb_dict[word] = vector | ||
base = math.sqrt(6/embedding_size) | ||
emb_dict["<UNK>"] = np.random.uniform(-base,base,(embedding_size)) | ||
return emb_dict | ||
self.emb_dict, self.dim = load(config) | ||
|
||
def isOutOfVocabulary(self, word): | ||
return word not in self.emb_dict | ||
return word not in self.emb_dict | ||
|
||
def load(config): | ||
emb_matrix = None | ||
emb_dict = dict() | ||
for line in open(config.get_string("glove.matrixResourceName")): | ||
if not len(line.split()) == 2: | ||
if "\t" in line: | ||
delimiter = "\t" | ||
else: | ||
delimiter = " " | ||
line_split = line.rstrip().split(delimiter) | ||
# extract word and vector | ||
word = line_split[0] | ||
x = np.array([float(i) for i in line_split[1:]]) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ZhengTang1120
Author
|
||
vector = (x /np.linalg.norm(x)) | ||
embedding_size = vector.shape[0] | ||
emb_dict[word] = vector | ||
base = math.sqrt(6/embedding_size) | ||
emb_dict["<UNK>"] = np.random.uniform(-base,base,(embedding_size)) | ||
return emb_dict, embedding_size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
I would write this as: