python - Cross Entropy for batch with Theano -


i attempting implement rnn , have output predictions p_y of shape (batch_size, time_points, num_classes). have target_output of shape (batch_size, time_points), value @ given index of target_output integer denoting class (a value between 0 , num_classes-1). how can index p_y target_output probabilities of given class need compute cross-entropy?

i'm not sure how in numpy. expression p_y[target_output] not give desired results.

you need use advanced indexing (search "advanced indexing" here). theano advanced indexing behaves differently numpy knowing how in numpy may not helpful!

here's function setup, note order of dimensions differs yours. use (time points, batch_size, num_classes). assumes want use 1-of-n categorical cross-entropy variant. may not want sequence length padding either.

def categorical_crossentropy_3d(coding_dist, true_dist, lengths):     # 0 out false probabilities , sum remaining true probabilities remove third dimension.     indexes = theano.tensor.arange(coding_dist.shape[2])     mask = theano.tensor.neq(indexes, true_dist.reshape((true_dist.shape[0], true_dist.shape[1], 1)))     predicted_probabilities = theano.tensor.set_subtensor(coding_dist[theano.tensor.nonzero(mask)], 0.).sum(axis=2)      # pad short sequences 1's (the pad locations implicitly correct!)     indexes = theano.tensor.arange(predicted_probabilities.shape[0]).reshape((predicted_probabilities.shape[0], 1))     mask = indexes >= lengths     predicted_probabilities = theano.tensor.set_subtensor(predicted_probabilities[theano.tensor.nonzero(mask)], 1.)      return -theano.tensor.log(predicted_probabilities) 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -