Skip to content

Instantly share code, notes, and snippets.

@aelaguiz
Last active December 10, 2015 15:19
Show Gist options
  • Save aelaguiz/4453854 to your computer and use it in GitHub Desktop.
Save aelaguiz/4453854 to your computer and use it in GitHub Desktop.
Equivalent straight numpy/python for Theanos softmax function
import numpy
import theano
import theano.tensor as T
def theano_softmax():
x = T.dmatrix('x')
_y = T.nnet.softmax(x)
f = theano.function([x], _y)
return f
def theano_p_y_given_x():
x = T.dmatrix('x')
w = T.dmatrix('w')
b = T.dmatrix('b')
input = T.dot(x, w) + b
y = T.nnet.softmax(input)
f = theano.function([x, w, b], y)
return f
def softmax(w):
w = numpy.array(w)
maxes = numpy.amax(w, axis=1)
maxes = maxes.reshape(maxes.shape[0], 1)
e = numpy.exp(w - maxes)
dist = e / numpy.sum(e, axis=1)
return dist
def p_y_given_x(X, w, b):
dt = numpy.dot(X, w) + b
return softmax(dt)
X = numpy.array([[1, 2], [3, 4]])
w = numpy.array([[1, 1], [1, 1]])
b = numpy.array([[0, 0], [0, 0]])
print "---------------------"
print "Theano"
print theano_softmax()(X)
print "Ours"
print softmax(X)
print "---------------------"
print ""
print "---------------------"
print "Theano P(y) given X:"
print theano_p_y_given_x()(X, w, b)
print "Our P(y) given X:"
print p_y_given_x(X, w, b)
@aelaguiz
Copy link
Author

aelaguiz commented Jan 4, 2013

Theano
[[ 0.26894142 0.73105858]
[ 0.26894142 0.73105858]]
Ours
[[ 0.26894142 0.73105858]
[ 0.26894142 0.73105858]]

@zhengwy888
Copy link

The sum of softmax in your code does not take uneven arrays, try softmax(numpy.random.randn(3,2))
I encountered this error:
ValueError: operands could not be broadcast together with shapes (3,2) (3,)

To fix I changed the line
dist = e / numpy.sum(e, axis=1)
to
dist = expNum / numpy.sum(e, axis=1, keepdims=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment