Skip to content

Instantly share code, notes, and snippets.

@jiangnanhugo
Last active September 25, 2016 12:32
Show Gist options
  • Save jiangnanhugo/3464572ab31314707ae6a2008510bce8 to your computer and use it in GitHub Desktop.
Save jiangnanhugo/3464572ab31314707ae6a2008510bce8 to your computer and use it in GitHub Desktop.
import theano as T
def nce_binary_conditional_likelihood(p_unnormalized,y,y_flat,noise_samples,noise_dist,k):
p_unnormalized_exp=T.exp(p_unnormalized)
p_unnormalized_flat=p_unnormalized_exp.flatten()
unnorm_y=p_unnormalized_flat[y_flat]
noise_y=noise_dist[y]
pos_cost=T.log(unnorm_y/(unnorm_y+k*noise_y))
neg_cost=T.log(k*noise_dist/(p_unnormalized_exp+k*noise_dist))
return -T.mean(pos_cost+ T.sum(neg_cost*noise_samples,axis=1),dtype=theano.config.floatX)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment