-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.py
36 lines (28 loc) · 1.13 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
import numpy as np
def batch_iter(tensor, batch_size, shuffle=False):
batches_count = tensor.shape[0] // batch_size
if shuffle:
shuffle_indices = np.random.permutation(np.arange(tensor.shape[0]))
data = tensor[shuffle_indices]
else:
data = tensor
neg_shuffle_indices = np.random.permutation(np.arange(tensor.shape[0]))
negative_data = tensor[neg_shuffle_indices]
for batch_num in range(batches_count):
start_index = batch_num * batch_size
end_index = min((batch_num + 1)*batch_size, tensor.shape[0])
yield data[start_index:end_index]
def neg_sampling_iter(tensor, batch_size, count, seed=None):
batches_count = tensor.shape[0] // batch_size
trials = 0
np.random.seed(seed)
shuffle_indices = np.random.permutation(np.arange(tensor.shape[0]))
data = tensor[shuffle_indices]
for batch_num in range(batches_count):
trials += 1
start_index = batch_num * batch_size
end_index = min((batch_num + 1)*batch_size, tensor.shape[0])
if trials > count:
return
else:
yield data[start_index:end_index]