コード例 #1
0
    def setUp(self):
        self.op_type = "cross_entropy"
        batch_size = 30
        class_num = 10

        X = randomize_probability(batch_size, class_num, dtype='float64')

        label = np.random.randint(0, class_num, (batch_size, 1), dtype="int64")
        cross_entropy = np.asmatrix([[-np.log(X[i][label[i][0]])]
                                     for i in range(X.shape[0])],
                                    dtype="float64")

        self.inputs = {"X": X, "Label": label}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": False}
コード例 #2
0
    def setUp(self):
        self.op_type = "cross_entropy"
        batch_size = 5
        class_num = 37

        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)
        cross_entropy = (-label * np.log(X)).sum(
            axis=1, keepdims=True).astype("float32")

        self.inputs = {"X": X, "Label": label}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": True}
コード例 #3
0
    def setUp(self):
        self.op_type = "cross_entropy"
        batch_size = 30
        class_num = 10

        X = randomize_probability(batch_size, class_num, dtype='float64')

        label = np.random.randint(0, class_num, (batch_size, 1), dtype="int64")
        cross_entropy = np.asmatrix(
            [[-np.log(X[i][label[i][0]])] for i in range(X.shape[0])],
            dtype="float64")

        self.inputs = {"X": X, "Label": label}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": False}
コード例 #4
0
    def setUp(self):
        self.op_type = "cross_entropy"
        batch_size = 5
        class_num = 37

        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)
        cross_entropy = (-label * np.log(X)).sum(
            axis=1, keepdims=True).astype("float32")

        self.inputs = {"X": X, "Label": label}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": True}
コード例 #5
0
 def setUp(self):
     self.op_type = "bpr_loss"
     batch_size = 40
     class_num = 5
     X = randomize_probability(batch_size, class_num, dtype='float64')
     label = np.random.randint(0, class_num, (batch_size, 1), dtype="int64")
     bpr_loss_result = []
     for i in range(batch_size):
         sum = 0.0
         for j in range(class_num):
             if j == label[i][0]:
                 continue
             sum += (-np.log(1.0 + np.exp(X[i][j] - X[i][label[i][0]])))
         bpr_loss_result.append(-sum / (class_num - 1))
     bpr_loss = np.asmatrix([[x] for x in bpr_loss_result], dtype="float64")
     self.inputs = {"X": X, "Label": label}
     self.outputs = {"Y": bpr_loss}
コード例 #6
0
    def setUp(self):
        self.op_type = "cross_entropy"
        shape = [4, 3]
        ins_num = np.prod(np.array(shape))
        class_num = 37

        X_2d = randomize_probability(ins_num, class_num)
        label_2d = np.random.uniform(0.1, 1.0,
                                     [ins_num, class_num]).astype("float32")
        label_2d /= label_2d.sum(axis=1, keepdims=True)
        cross_entropy_2d = (-label_2d * np.log(X_2d)).sum(
            axis=1, keepdims=True).astype("float32")

        X = X_2d.reshape(shape + [class_num])
        label = label_2d.reshape(shape + [class_num])
        cross_entropy = np.array(cross_entropy_2d).reshape(shape + [1])

        self.inputs = {"X": X, "Label": label}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": True}
コード例 #7
0
    def setUp(self):
        self.op_type = "cross_entropy"
        shape = [10, 2, 4]
        ins_num = np.prod(np.array(shape))
        class_num = 10

        X_2d = randomize_probability(ins_num, class_num, dtype='float64')

        label_2d = np.random.randint(0, class_num, (ins_num, 1), dtype="int64")
        cross_entropy_2d = np.asmatrix([[-np.log(X_2d[i][label_2d[i][0]])]
                                        for i in range(X_2d.shape[0])],
                                       dtype="float64")

        X = X_2d.reshape(shape + [class_num])
        label = label_2d.reshape(shape + [1])
        cross_entropy = np.array(cross_entropy_2d).reshape(shape + [1])

        self.inputs = {"X": X, "Label": label}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": False}
コード例 #8
0
    def setUp(self):
        self.op_type = "cross_entropy"
        batch_size = 5
        class_num = 17

        X = randomize_probability(batch_size, class_num)
        label_index = np.random.randint(
            0, class_num, (batch_size), dtype="int32")
        label = np.zeros(X.shape)
        label[np.arange(batch_size), label_index] = 1

        cross_entropy = np.asmatrix(
            [[-np.log(X[i][label_index[i]])] for i in range(X.shape[0])],
            dtype="float32")
        cross_entropy2 = (-label * np.log(X)).sum(
            axis=1, keepdims=True).astype("float32")

        self.inputs = {"X": X, "Label": label.astype(np.float32)}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": True}
コード例 #9
0
    def setUp(self):
        self.op_type = "cross_entropy"
        shape = [4, 3, 2]
        ins_num = np.prod(np.array(shape))
        class_num = 17

        X_2d = randomize_probability(ins_num, class_num)
        label_index_2d = np.random.randint(0,
                                           class_num, (ins_num),
                                           dtype="int32")
        label_2d = np.zeros(X_2d.shape)
        label_2d[np.arange(ins_num), label_index_2d] = 1

        cross_entropy_2d = np.asmatrix([[-np.log(X_2d[i][label_index_2d[i]])]
                                        for i in range(X_2d.shape[0])],
                                       dtype="float32")

        X = X_2d.reshape(shape + [class_num])
        label = label_2d.reshape(shape + [class_num])
        cross_entropy = np.array(cross_entropy_2d).reshape(shape + [1])

        self.inputs = {"X": X, "Label": label.astype(np.float32)}
        self.outputs = {"Y": cross_entropy}
        self.attrs = {"soft_label": True}
コード例 #10
0
 def init_x(self):
     self.x = randomize_probability(self.batch_size,
                                    self.class_num,
                                    dtype=self.dtype)
コード例 #11
0
 def init_x(self):
     self.shape = [4, 3, 2]
     self.ins_num = np.prod(np.array(self.shape))
     self.X_2d = randomize_probability(self.ins_num,
                                       self.class_num).astype(self.dtype)
     self.x = self.X_2d.reshape(self.shape + [self.class_num])