Skip to content

Fix conflicting glove.6B.100d.trimmed.txt when run write.py #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions utils/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import numpy as np

import os.path


_MAX_BATCH_SIZE = 128
_MAX_DOC_LENGTH = 200
Expand Down Expand Up @@ -36,28 +38,29 @@ def _add_word(word):

embeddings_path = './data/glove/glove.6B.100d.trimmed.txt'

with open(embeddings_path) as f:
line = f.readline()
chunks = line.split(" ")
dimensions = len(chunks) - 1
f.seek(0)

vocab_size = sum(1 for line in f)
vocab_size += 4 #3
f.seek(0)

glove = np.ndarray((vocab_size, dimensions), dtype=np.float32)
glove[PADDING_TOKEN] = np.random.normal(0, 0.02, dimensions)
glove[UNKNOWN_TOKEN] = np.random.normal(0, 0.02, dimensions)
glove[START_TOKEN] = np.random.normal(0, 0.02, dimensions)
glove[END_TOKEN] = np.random.normal(0, 0.02, dimensions)

for line in f:
if os.path.exists(embeddings_path):
with open(embeddings_path) as f:
line = f.readline()
chunks = line.split(" ")
idx = _add_word(chunks[0])
glove[idx] = [float(chunk) for chunk in chunks[1:]]
if len(_idx_to_word) >= vocab_size:
break
dimensions = len(chunks) - 1
f.seek(0)

vocab_size = sum(1 for line in f)
vocab_size += 4 #3
f.seek(0)

glove = np.ndarray((vocab_size, dimensions), dtype=np.float32)
glove[PADDING_TOKEN] = np.random.normal(0, 0.02, dimensions)
glove[UNKNOWN_TOKEN] = np.random.normal(0, 0.02, dimensions)
glove[START_TOKEN] = np.random.normal(0, 0.02, dimensions)
glove[END_TOKEN] = np.random.normal(0, 0.02, dimensions)

for line in f:
chunks = line.split(" ")
idx = _add_word(chunks[0])
glove[idx] = [float(chunk) for chunk in chunks[1:]]
if len(_idx_to_word) >= vocab_size:
break



Expand Down