-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss_func.py
38 lines (25 loc) · 1.32 KB
/
loss_func.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
import tensorflow as tf
def cross_entropy_loss(inputs, true_w):
"""
==========================================================================
inputs: The embeddings for context words. Dimension is [batch_size, embedding_size].
true_w: The embeddings for predicting words. Dimension of true_w is [batch_size, embedding_size].
Write the code that calculate A = log(exp({u_o}^T v_c))
A =
And write the code that calculate B = log(\sum{exp({u_w}^T v_c)})
B =
==========================================================================
"""
return tf.subtract(B, A)
def nce_loss(inputs, weights, biases, labels, sample, unigram_prob):
"""
==========================================================================
inputs: Embeddings for context words. Dimension is [batch_size, embedding_size].
weigths: Weights for nce loss. Dimension is [Vocabulary, embeeding_size].
biases: Biases for nce loss. Dimension is [Vocabulary, 1].
labels: Word_ids for predicting words. Dimesion is [batch_size, 1].
samples: Word_ids for negative samples. Dimension is [num_sampled].
unigram_prob: Unigram probability. Dimesion is [Vocabulary].
Implement Noise Contrastive Estimation Loss Here
==========================================================================
"""