Skip to content

Instantly share code, notes, and snippets.

@lubosz
Created May 17, 2018 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lubosz/fb957e4707d857521a96fc24ebbe75cd to your computer and use it in GitHub Desktop.
Save lubosz/fb957e4707d857521a96fc24ebbe75cd to your computer and use it in GitHub Desktop.
Example that shows how torch saves more float precision than it prints by default
#!/usr/bin/env python3
import torch
from torch.autograd import Variable
import numpy as np
def load_embeddings(count):
word_embeddings = {}
with open('2014_tudarmstadt_german_100mincount.vocab', 'r') as f:
for line in f:
line_list = line.strip().split(" ")
word = line_list[0]
word_embeddings[word] = line_list[1:]
if len(word_embeddings) >= count:
return word_embeddings
if __name__ == "__main__":
embeddings = load_embeddings(100)
for word, vector_str in embeddings.items():
print("++ %s ++" % word)
vector_float = [float(s) for s in vector_str]
vector_np = np.array(vector_float)
vector_torch = torch.from_numpy(vector_np)
vector_torch_list = vector_torch.tolist()
print("strings", vector_str[:5])
print("floats ", vector_float[:5])
print("numpy ", vector_np[:5])
print("torch ", vector_torch[:5])
print("torch l", vector_torch_list[:5])
for i in range(100):
number_str = vector_str[i]
number_float = vector_float[i]
number_np = vector_np[i]
number_torch = vector_torch_list[i]
assert(number_str == str(number_float))
assert(number_str == str(number_np))
assert(number_str == str(number_torch))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment