I want to test nn.CrossEntropyLoss() is same as tf.nn.softmax_cross_entropy_with_logits in tensorflow.
so I have tested on tensorflow and pytorch. I got value with tensorflow, but I don`t know how to get value of pytorch.
Tensorflow test :
sess = tf.Session()
y_true = tf.convert_to_tensor(np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]))
y_hat = tf.convert_to_tensor(np.array([[0.5, 1.5, 0.1], [2.2, 1.3, 1.7]]))
# print(y_true)
tf_loss = tf.nn.softmax_cross_entropy_with_logits(labels=y_hat, logits=y_true)
print(sess.run(tf_loss))
-> [1.7580339 6.36751251]
Pytorch test :
loss = nn.CrossEntropyLoss()
input = torch.Tensor(np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]))
target = torch.Tensor(np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]), dtype=torch.long)
print(type(input), type(target))
output = loss(input, target)
output.backward()
print(output)
It occurs error :weary:
> target = torch.Tensor(np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]), dtype=torch.long)
> TypeError: new() received an invalid combination of arguments - got (numpy.ndarray, dtype=torch.dtype), but expected one of:
> * (torch.device device)
> * (torch.Storage storage)
> * (Tensor other)
> * (tuple of ints size, torch.device device)
> didn't match because some of the keywords were incorrect: dtype
> * (object data, torch.device device)
> didn't match because some of the keywords were incorrect: dtype
Anyone can help me?