Last active
July 15, 2024 16:13
-
-
Save jeffdonahue/b0641bafc2df22d7bd46d953dd89ef0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import numpy as np | |
| import theano | |
| import theano.tensor as T | |
| pred, target = T.vectors('pt') # pred in (-inf, +inf), target in [0, 1] | |
| L1 = T.nnet.binary_crossentropy(T.nnet.sigmoid(pred), target).sum() | |
| L2 = T.nnet.sigmoid_binary_crossentropy(pred, target).sum() | |
| fl1, fl2 = [theano.function([pred, target], L) for L in (L1, L2)] | |
| g1, g2 = [theano.grad(L, [pred, target]) for L in (L1, L2)] | |
| fg1, fg2 = [theano.function([pred, target], g) for g in (g1, g2)] | |
| np.random.seed(0) | |
| n = 3 | |
| px = np.random.randn(n).astype(theano.config.floatX) | |
| lx = np.random.random(n).astype(theano.config.floatX) | |
| print 'binary_crossentropy loss:', fl1(px, lx) | |
| theano.printing.debugprint(fl1) | |
| print '\nsigmoid_binary_crossentropy loss:', fl2(px, lx) | |
| theano.printing.debugprint(fl2) | |
| print '\nbinary_crossentropy grad:', fg1(px, lx) | |
| theano.printing.debugprint(fg1) | |
| print '\nsigmoid_binary_crossentropy grad:', fg2(px, lx) | |
| theano.printing.debugprint(fg2) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: