Skip to content

Instantly share code, notes, and snippets.

View maciejskorski's full-sized avatar

Maciej Skorski maciejskorski

View GitHub Profile
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
@maciejskorski
maciejskorski / skipgram_generator.txt
Last active April 4, 2020 18:52
skipgram generator
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: