Equivalent of Keras's binary_crossentropy in PyTorch?
I want to port some code from keras to pytorch, but I cann't find equivalent of Keras's binary_crossentropy in PyTorch. PyTorch's binary_cross_entropy has different behavior with keras's. 

import torch
import torch.nn.functional as F
input = torch.tensor([[ 0.6845,  0.2454],
                      [ 0.7186,  0.3710],
                      [ 0.3480,  0.3374]])
target = torch.tensor([[ 0.,  1.],
                       [ 1.,  1.],
                       [ 1.,  1.]])
F.binary_cross_entropy(input, target, reduce=False)
#tensor([[ 1.1536,  1.4049],
#    [ 0.3305,  0.9916],
#    [ 1.0556,  1.0865]])
import keras.backend as K
K.eval(K.binary_crossentropy(K.variable(input.detach().numpy()), K.variable(target.detach().numpy())))

 #[[11.032836 12.030124]
 #[ 4.486187 10.02776 ]
 #[10.394435 10.563424]]


Is there anyone know why these two results are different? thanks!

Keras binary crossentropy takes y_true, y_pred, while Pytorch takes them in the opposite order, therefore you need to change the Keras line to

K.eval(K.binary_crossentropy(K.variable(target.detach().numpy()), K.variable(input.detach().numpy())))


In this way you get the correct output:

array([[ 1.15359652,  1.40486574],
       [ 0.33045045,  0.99155325],
       [ 1.05555284,  1.0864861 ]], dtype=float32)

