Skip to content

Instantly share code, notes, and snippets.

@iamaaditya
Forked from raingo/ops.py
Created August 30, 2016 03:04
Show Gist options
  • Save iamaaditya/68dc58b3260783f6f3c298ddbc2745ca to your computer and use it in GitHub Desktop.
Save iamaaditya/68dc58b3260783f6f3c298ddbc2745ca to your computer and use it in GitHub Desktop.
multi dimensional softmax with tensorflow
import tensorflow as tf
"""
Multi dimensional softmax,
refer to https://github.com/tensorflow/tensorflow/issues/210
compute softmax along the dimension of target
the native softmax only supports batch_size x dimension
"""
def softmax(target, axis, name=None):
with tf.op_scope([target], name, 'softmax'):
max_axis = tf.reduce_max(target, axis, keep_dims=True)
target_exp = tf.exp(target-max_axis)
normalize = tf.reduce_sum(target_exp, axis, keep_dims=True)
softmax = target_exp / normalize
return softmax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment