-
Notifications
You must be signed in to change notification settings - Fork 511
Fill and write each array before creating the next one, to save memory. #177
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
qwertystop
wants to merge
3
commits into
jcjohnson:master
Choose a base branch
from
qwertystop:handle-large
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -45,33 +45,44 @@ | |
# Choose the datatype based on the vocabulary size | ||
dtype = np.uint8 | ||
if len(token_to_idx) > 255: | ||
dtype = np.uint32 | ||
if len(token_to_idx) > 65535: | ||
if len(token_to_idx) > 4294967295: | ||
dtype = np.uint64 | ||
else: | ||
dtype = np.uint32 | ||
else: | ||
dtype = np.uint16 | ||
if not args.quiet: | ||
print 'Using dtype ', dtype | ||
|
||
# Just load data into memory ... we'll have to do something more clever | ||
# for huge datasets but this should be fine for now | ||
train = np.zeros(train_size, dtype=dtype) | ||
val = np.zeros(val_size, dtype=dtype) | ||
test = np.zeros(test_size, dtype=dtype) | ||
splits = [train, val, test] | ||
|
||
# Go through the file again and write data to numpy arrays | ||
split_idx, cur_idx = 0, 0 | ||
# Create, fill, and store each dataset, | ||
# one at a time to save memory | ||
with codecs.open(args.input_txt, 'r', args.encoding) as f: | ||
for line in f: | ||
for char in line: | ||
splits[split_idx][cur_idx] = token_to_idx[char] | ||
cur_idx += 1 | ||
if cur_idx == splits[split_idx].size: | ||
split_idx += 1 | ||
cur_idx = 0 | ||
with h5py.File(args.output_h5, 'w') as h: | ||
def fill_and_store(arr_size, set_name): | ||
"""Create a one-dimensional numpy array | ||
of the given size, fill it, | ||
and write the result to h under the given name. | ||
|
||
Leaves the source file advanced as far | ||
as it had to go to fill the array. | ||
|
||
If the remaining part of the file is shorter | ||
than arr_size, the remainder of the array is | ||
filled with zeroes. | ||
""" | ||
arr = np.zeros(arr_size, dtype=dtype) | ||
for idx in xrange(arr_size): | ||
char = f.read(1) | ||
if not char: | ||
break | ||
arr[idx] = token_to_idx[char] | ||
|
||
h.create_dataset(set_name, data=arr) | ||
|
||
# Write data to HDF5 file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor comment #2: Indentation doesn't match the rest of the file. |
||
with h5py.File(args.output_h5, 'w') as f: | ||
f.create_dataset('train', data=train) | ||
f.create_dataset('val', data=val) | ||
f.create_dataset('test', data=test) | ||
fill_and_store(train_size, 'train') | ||
fill_and_store(val_size, 'val') | ||
fill_and_store(test_size, 'test') | ||
|
||
# For 'bytes' encoding, replace non-ascii characters so the json dump | ||
# doesn't crash | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comment #1: use a single level of branching: