-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils.py
108 lines (102 loc) · 3.12 KB
/
utils.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
96
97
98
99
100
101
102
103
104
105
106
107
108
import tensorflow as tf
import random
import numpy as np
def data_set(data_url):
"""process data input."""
data = []
word_count = []
fin = open(data_url)
while True:
line = fin.readline()
if not line:
break
id_freqs = line.split()
doc = {}
count = 0
for id_freq in id_freqs[1:]:
items = id_freq.split(':')
# python starts from 0
doc[int(items[0])-1] = int(items[1])
count += int(items[1])
if count > 0:
data.append(doc)
word_count.append(count)
fin.close()
return data, word_count
def create_batches(data_size, batch_size, shuffle=True):
"""create index by batches."""
batches = []
ids = range(data_size)
if shuffle:
random.shuffle(ids)
for i in xrange(data_size / batch_size):
start = i * batch_size
end = (i + 1) * batch_size
batches.append(ids[start:end])
# the batch of which the length is less than batch_size
rest = data_size % batch_size
if rest > 0:
batches.append(ids[-rest:] + [-1] * (batch_size - rest)) # -1 as padding
return batches
def fetch_data(data, count, idx_batch, vocab_size):
"""fetch input data by batch."""
batch_size = len(idx_batch)
data_batch = np.zeros((batch_size, vocab_size))
count_batch = []
mask = np.zeros(batch_size)
indices = []
values = []
for i, doc_id in enumerate(idx_batch):
if doc_id != -1:
for word_id, freq in data[doc_id].items():
data_batch[i, word_id] = freq
count_batch.append(count[doc_id])
mask[i]=1.0
else:
count_batch.append(0)
return data_batch, count_batch, mask
def variable_parser(var_list, prefix):
"""return a subset of the all_variables by prefix."""
ret_list = []
for var in var_list:
varname = var.name
varprefix = varname.split('/')[0]
if varprefix == prefix:
ret_list.append(var)
return ret_list
def linear(inputs,
output_size,
no_bias=False,
bias_start_zero=False,
matrix_start_zero=False,
scope=None):
"""Define a linear connection."""
with tf.variable_scope(scope or 'Linear'):
if matrix_start_zero:
matrix_initializer = tf.constant_initializer(0)
else:
matrix_initializer = None
if bias_start_zero:
bias_initializer = tf.constant_initializer(0)
else:
bias_initializer = None
input_size = inputs.get_shape()[1].value
matrix = tf.get_variable('Matrix', [input_size, output_size],
initializer=matrix_initializer)
bias_term = tf.get_variable('Bias', [output_size],
initializer=bias_initializer)
output = tf.matmul(inputs, matrix)
if not no_bias:
output = output + bias_term
return output
def mlp(inputs,
mlp_hidden=[],
mlp_nonlinearity=tf.nn.tanh,
scope=None):
"""Define an MLP."""
with tf.variable_scope(scope or 'Linear'):
mlp_layer = len(mlp_hidden)
res = inputs
for l in xrange(mlp_layer):
res = mlp_nonlinearity(linear(res, mlp_hidden[l], scope='l'+str(l)))
return res