-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
executable file
·59 lines (52 loc) · 1.7 KB
/
helper.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
import numpy as np
import random
from torch.autograd import Variable
import torch
import pdb
# function maps each word to an index
def get_idx(char_data):
word_to_ix = {}
count = 0
char_data = list(char_data)
char_data.sort()
for word in char_data:
if word not in word_to_ix:
word_to_ix[word] = count
count += 1
return word_to_ix
def add_cuda_to_variable(data_nums, is_gpu):
tensor = torch.FloatTensor(data_nums)
if isinstance(data_nums, list):
tensor = tensor.unsqueeze_(0)
if is_gpu:
return Variable(tensor.cuda())
else:
return Variable(tensor)
# returns prediction based on probabilites
def flip_coin(probabilities, is_gpu):
stacked_probs = np.cumsum(probabilities)
rand_int = random.random()
if is_gpu:
sp = stacked_probs[0].cpu().numpy()
else:
sp = stacked_probs.numpy()
dist = abs(sp - rand_int)
return np.argmin(dist)
def flip_coin_batch(probabilities, is_gpu):
stacked_probs = np.cumsum(probabilities)
N = probabilities.size(0)
v_size = probabilities.size(1)
rand_int = np.array([np.random.random(N)]).T
rand_int = rand_int.repeat(v_size, axis=1)
if is_gpu:
sp = stacked_probs[0].cpu().numpy()
else:
sp = stacked_probs.numpy()
dist = abs(sp - rand_int)
return np.array([np.argmin(dist, axis=1)]).T
def custom_softmax(output, T):
return torch.exp(torch.div(output, T)) / torch.sum(torch.exp(torch.div(output, T)))
def custom_softmax_batch(output, T):
denom = torch.sum(torch.exp(torch.div(output, T)), dim=1).unsqueeze(-1)
denom = denom.expand(output.size(0), output.size(1))
return torch.exp(torch.div(output, T)) / denom