-
Notifications
You must be signed in to change notification settings - Fork 12
/
torch_utils.py
130 lines (89 loc) · 3.04 KB
/
torch_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
import torch
import torch.nn as nn
def gpu(tensor, gpu=False):
if gpu:
return tensor.cuda()
else:
return tensor
def accuracy_one(x):
return x[:,0] < 0.5
def shuffle_sentences(word,sent):
random_state = np.random.RandomState()
shuffle_indices = np.arange(len(sent))
random_state.shuffle(shuffle_indices)
return tuple([word[shuffle_indices,:], sent[shuffle_indices]])
def shuffle(*arrays):
random_state = np.random.RandomState()
shuffle_indices = np.arange(len(arrays[0]))
random_state.shuffle(shuffle_indices)
if len(arrays) == 1:
return arrays[0][shuffle_indices]
else:
return tuple(x[shuffle_indices] for x in arrays)
def minibatch_sentences(batch_size, word, sent):
for i in range(0, len(sent), batch_size):
yield tuple([word[i:i+batch_size,:], sent[i:i+batch_size]])
def minibatch(batch_size, *tensors):
if len(tensors) == 1:
tensor = tensors[0]
for i in range(0, len(tensor), batch_size):
yield tensor[i:i + batch_size]
else:
for i in range(0, len(tensors[0]), batch_size):
yield tuple(x[i:i + batch_size] for x in tensors)
def regression_loss(observed_ratings, predicted_ratings):
return ((observed_ratings - predicted_ratings) ** 2).mean()
def l1_loss(observed_ratings, predicted_ratings):
return (torch.abs(observed_ratings-predicted_ratings)).mean()
def hinge_loss(positive_predictions, negative_predictions):
loss = torch.clamp(negative_predictions -
positive_predictions +
1.0, 0.0)
return loss.mean()
class ScaledEmbedding(nn.Embedding):
"""
Embedding layer that initialises its values
to using a normal variable scaled by the inverse
of the emedding dimension.
"""
def reset_parameters(self):
"""
Initialize parameters.
"""
self.weight.data.normal_(0, 1.0 / self.embedding_dim)
if self.padding_idx is not None:
self.weight.data[self.padding_idx].fill_(0)
class ZeroEmbedding(nn.Embedding):
"""
Used for biases.
"""
def reset_parameters(self):
"""
Initialize parameters.
"""
self.weight.data.zero_()
if self.padding_idx is not None:
self.weight.data[self.padding_idx].fill_(0)
def sample_items(num_items, shape, random_state=None):
"""
Randomly sample a number of items.
Parameters
----------
num_items: int
Total number of items from which we should sample:
the maximum value of a sampled item id will be smaller
than this.
shape: int or tuple of ints
Shape of the sampled array.
random_state: np.random.RandomState instance, optional
Random state to use for sampling.
Returns
-------
items: np.array of shape [shape]
Sampled item ids.
"""
if random_state is None:
random_state = np.random.RandomState()
items = random_state.randint(0, num_items, shape)
return items