forked from vansky/neural-complexity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_embeddings.py
95 lines (77 loc) · 3.03 KB
/
get_embeddings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
Code to extract all the vocabulary embeddings from a neural language model.
'''
from __future__ import print_function
import argparse
import time
import math
import sys
import warnings
import torch
import torch.nn as nn
import data
import model
try:
from progress.bar import Bar
PROGRESS = True
except ModuleNotFoundError:
PROGRESS = False
# suppress SourceChangeWarnings
warnings.filterwarnings("ignore")
sys.stderr.write('Libraries loaded\n')
# Parallelization notes:
# Does not currently operate across multiple nodes
# Single GPU is better for default: tied,emsize:200,nhid:200,nlayers:2,dropout:0.2
#
# Multiple GPUs are better for tied,emsize:1500,nhid:1500,nlayers:2,dropout:0.65
# 4 GPUs train on wikitext-2 in 1/2 - 2/3 the time of 1 GPU
parser = argparse.ArgumentParser(description='PyTorch RNN/LSTM Language Model')
# Model parameters
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
# Data parameters
parser.add_argument('--model_file', type=str, default='model.pt',
help='path to model')
parser.add_argument('--vocab_file', type=str, default='vocab.txt',
help='path to vocab_file')
args = parser.parse_args()
if torch.cuda.is_available():
if not args.cuda:
sys.stderr.write("WARNING: You have a CUDA device, so you should probably run with --cuda\n")
else:
#torch.cuda.manual_seed(args.seed)
if torch.cuda.device_count() == 1:
args.single = True
device = torch.device("cuda" if args.cuda else "cpu")
###############################################################################
# Load the vocab
###############################################################################
idx2word = {}
with open(args.vocab_file, 'r') as f:
idx = 0
for line in f:
line = line.strip()
idx2word[idx] = line
idx += 1
###############################################################################
# Load the model
###############################################################################
with open(args.model_file, 'rb') as f:
if args.cuda:
model = torch.load(f).to(device)
else:
model = torch.load(f, map_location='cpu')
if args.cuda and (not args.single) and (torch.cuda.device_count() > 1):
# If applicable, use multi-gpu for training
# Scatters minibatches (in dim=1) across available GPUs
model = nn.DataParallel(model, dim=1)
if isinstance(model, torch.nn.DataParallel):
# if multi-gpu, access real model for training
model = model.module
# after load the rnn params are not a continuous chunk of memory
# this makes them a continuous chunk, and will speed up forward pass
model.rnn.flatten_parameters()
#print(model.encoder(torch.LongTensor([w for w in range(model.encoder.num_embeddings)])))
for idx, embed in enumerate(model.encoder(torch.LongTensor([w for w in range(model.encoder.num_embeddings)])).data.numpy().tolist()):
word = idx2word[idx]
print(word+' '+' '.join(str(f) for f in embed))