forked from KnHuq/tensorflow_with_latest_papers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartial_ordering_embedding.py
More file actions
54 lines (35 loc) · 1.44 KB
/
Copy pathpartial_ordering_embedding.py
File metadata and controls
54 lines (35 loc) · 1.44 KB
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
import numpy as np
import tensorflow as tf
class partial_ordering_embedding(object):
'''partial_ordering_embedding as defined by Vendrov et al. http://arxiv.org/pdf/1511.06361v6.pdf'''
def __init__(self, embedding_size):
self.embedding_size = embedding_size
#TODO: nick, investigate max margin loss
def max_margin_loss(self, alpha = 1.0):
'''equation 3 in paper
(u, v) is a seen pair
where (u', v') is an unseen pair
Loss = E(f(u), f(v)) + max{0.0, alpha - E(f(u'), f(v'))}
'''
positive_loss_part = self.partial_order_error(u,v)
negative_loss_part = tf.maximum(0.0, alpha - self.partial_order_error(u_prime, v_prime))
loss = tf.reduce_sum(positive_loss_part + negative_loss_part)
return loss
def partial_order_error(self, x, y):
'''
calculates the following penalty of an ordered pair (x,y) as defined by:
E(x,y) = ||max(0, y- x||^2
notice that the error is always positive, imposing a strong prior to antisymmetry
x has shape [batch_size, vector_size]
y has shape [batch_size, vector_size]
returns partial_order_error of shape [batch_size]
'''
def euclidean_norm(tensor):
'''accepts tensor of shape [batch_size x vector_size]'''
return tf.reduce_sum(tensor**2, 1)
#no square op is here because in norm calculation we do NOT take sqrt
return tf.maximum(0, euclidean_norm(y-x))