This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def SparseCategoricalCrossentropy(labels,logits): | |
''' labels: shape [n_batch] contains true classes as numbers from 0 to n_classes-1 | |
logits: shape [n_batch,n_classes], predicted log probabilities ''' | |
Z = tf.reduce_logsumexp(logits,axis=-1) | |
lookup_labels = tf.stack([tf.range(tf.shape(labels)[0]),tf.cast(labels,tf.int32)],1) | |
true_logits = tf.gather_nd(logits,lookup_labels,batch_dims=0) | |
return -true_logits + Z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import islice,chain | |
from collections import deque | |
def gen_skipgrams(itr,window=1,symmetric=False,Q=None): | |
itr = iter(itr) | |
if not Q: | |
Q = deque(islice(itr,window-1),maxlen=window) | |
append = Q.append | |
for i in itr: | |
for j in Q: |
NewerOlder