|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
from op_test import OpTest
|
|
|
|
|
from op_test import OpTest, randomize_probability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCrossEntropyOp1(OpTest):
|
|
|
|
@ -12,12 +12,12 @@ class TestCrossEntropyOp1(OpTest):
|
|
|
|
|
batch_size = 30
|
|
|
|
|
class_num = 10
|
|
|
|
|
|
|
|
|
|
X = np.random.uniform(0.1, 1.0,
|
|
|
|
|
[batch_size, class_num]).astype("float32")
|
|
|
|
|
X = randomize_probability(batch_size, class_num, dtype='float64')
|
|
|
|
|
|
|
|
|
|
label = np.random.randint(0, class_num, (batch_size, 1), dtype="int32")
|
|
|
|
|
cross_entropy = np.asmatrix(
|
|
|
|
|
[[-np.log(X[i][label[i][0]])] for i in range(X.shape[0])],
|
|
|
|
|
dtype="float32")
|
|
|
|
|
dtype="float64")
|
|
|
|
|
|
|
|
|
|
self.inputs = {"X": X, "Label": label}
|
|
|
|
|
self.outputs = {"Y": cross_entropy}
|
|
|
|
@ -27,7 +27,7 @@ class TestCrossEntropyOp1(OpTest):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(["X"], "Y")
|
|
|
|
|
self.check_grad(["X"], "Y", numeric_grad_delta=0.001)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCrossEntropyOp2(OpTest):
|
|
|
|
@ -39,8 +39,7 @@ class TestCrossEntropyOp2(OpTest):
|
|
|
|
|
batch_size = 5
|
|
|
|
|
class_num = 37
|
|
|
|
|
|
|
|
|
|
X = np.random.uniform(0.1, 1.0,
|
|
|
|
|
[batch_size, class_num]).astype("float32")
|
|
|
|
|
X = randomize_probability(batch_size, class_num)
|
|
|
|
|
label = np.random.uniform(0.1, 1.0,
|
|
|
|
|
[batch_size, class_num]).astype("float32")
|
|
|
|
|
label /= label.sum(axis=1, keepdims=True)
|
|
|
|
@ -55,7 +54,8 @@ class TestCrossEntropyOp2(OpTest):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(["X"], "Y", max_relative_error=0.05)
|
|
|
|
|
self.check_grad(
|
|
|
|
|
["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCrossEntropyOp3(OpTest):
|
|
|
|
@ -67,8 +67,7 @@ class TestCrossEntropyOp3(OpTest):
|
|
|
|
|
batch_size = 5
|
|
|
|
|
class_num = 17
|
|
|
|
|
|
|
|
|
|
X = np.random.uniform(0.1, 1.0,
|
|
|
|
|
[batch_size, class_num]).astype("float32")
|
|
|
|
|
X = randomize_probability(batch_size, class_num)
|
|
|
|
|
label_index = np.random.randint(
|
|
|
|
|
0, class_num, (batch_size), dtype="int32")
|
|
|
|
|
label = np.zeros(X.shape)
|
|
|
|
@ -88,7 +87,8 @@ class TestCrossEntropyOp3(OpTest):
|
|
|
|
|
self.check_output()
|
|
|
|
|
|
|
|
|
|
def test_check_grad(self):
|
|
|
|
|
self.check_grad(["X"], "Y", max_relative_error=0.05)
|
|
|
|
|
self.check_grad(
|
|
|
|
|
["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|