Ejemplo n.º 1
0
 def accuracy(self, table):
     """forward를 반드시 해야 하고, backward 이전에 사용해야 합니다."""
     out = self.layers[-1].out
     out_argmax = tensor.argmax(out, -1, tensor.create_sum(out, -1))
     table_argmax = tensor.argmax(table, -1, tensor.create_sum(table, -1))
     eq = tensor.function_elment_wise(
         out_argmax, table_argmax, Layers._equal,
         tensor.create_element_wise_product(out_argmax, table_argmax, int))
     reduce_sum = tensor.sum_axis(eq, 0, tensor.Tensor([1], [1]))
     return reduce_sum.array[0] / len(out_argmax.array)
Ejemplo n.º 2
0
def test_axis(shape, axis, error=0.001):
    t1 = tensor.create_randomly(shape)
    n1 = parseNumpy(t1)
    t = tensor.sum_axis(t1, axis, tensor.create_sum(t1, axis))
    n = np.sum(n1, axis)
    print(t1)
    print(t)
    print(n)
    return compare(t, n)
Ejemplo n.º 3
0
def batch_nrom_forward(x_shape):
    def process_no_zero(x):
        return x + 10e-7

    x = tensor.create_randomly(x_shape, -3, 4)
    #x = tensor.Tensor([1.,2.,3.,5.],[1,4])
    mean = tensor.create_sum(x, 0)
    deviation = tensor.create_element_wise_product(x, mean)
    jegop = tensor.create_element_wise_product(deviation, deviation)
    dispersion = tensor.create_sum(jegop, 0)
    dispersion2 = dispersion.copy()
    #std = dispersion.copy()

    forward_out = x.copy()
    forward_new_out = x.copy()
    compare_out = x.copy()

    print(x)
    #잘 알려진 방법
    tensor.mean_axis(x, 0, mean)
    print(mean)
    tensor.sub(x, mean, deviation)
    tensor.mul(deviation, deviation, jegop)
    tensor.mean_axis(jegop, 0, dispersion)
    tensor.function(dispersion, process_no_zero, dispersion)
    tensor.function(dispersion, math.sqrt, dispersion)
    tensor.div(deviation, dispersion, forward_out)

    #새로운 방법
    norm.forward(x.array, x.shape, dispersion2.array, forward_new_out.array)

    tensor.function_element_wise(forward_out, forward_new_out, isSame,
                                 compare_out)

    print(compare_out)

    pass
Ejemplo n.º 4
0
    def forward(self, x):
        if (self.out.shape[0] != x.shape[0]):
            self.out = x.copy()
            self.tmp_sum = tensor.create_sum(x, -1)
            self.batch_size = x.shape[0]  #self.t.shape[0] # x.shape[0]

        #softmax
        SoftmaxWithLoss.overflow_process(x, self.out)
        tensor.function(self.out, self.exp_func, self.out)
        column_count = self.out.shape[-1]
        for r in range(self.batch_size):
            s = 0
            for c in range(column_count):
                s += self.out.array[r * column_count + c]
            for c in range(column_count):
                self.out.array[r * column_count + c] /= s

        return self.out
Ejemplo n.º 5
0
    def forward(self, x, t):
        if (self.out.shape[0] != x.shape[0]):
            self.out = x.copy()
            self.tmp_sum = tensor.create_sum(x, -1)
            self.batch_size = t.shape[0]  # x.shape[0]

        self.t = t

        #softmax
        SoftmaxWithLoss.overflow_process(x, self.out)
        tensor.function(self.out, self.exp_func, self.out)

        column_count = self.out.shape[-1]
        for r in range(self.batch_size):
            s = 0
            for c in range(column_count):
                s += self.out.array[r * column_count + c]
            for c in range(column_count):
                self.out.array[r * column_count + c] /= s

        #cross_etropy_error
        column_count = t.shape[-1]
        is_one_hot = int(column_count == 1)  #원핫이 아닌 경우 1 변수명과 의미가 다름 주의...
        self.loss = 0
        point = 0

        for i in range(self.batch_size):
            for c in range(column_count):
                point = i * column_count + c
                if (t.array[point] + is_one_hot > 0):
                    self.loss -= self.log_func(
                        self.out.array[point + is_one_hot * t.array[point]] +
                        1e-7)
                    break
        self.loss /= self.batch_size

        return self.loss
Ejemplo n.º 6
0
def test_norm_backward(x_shape, h=0.001):
    def process_no_zero(x):
        return x + 10e-7

    x = tensor.create_randomly(x_shape, -3, 4)
    #x = tensor.Tensor([1.,2.,3.,5.],[1,4])
    mean = tensor.create_sum(x, 0)
    d_mean = mean.copy()
    #d_mean2 = mean.copy()
    deviation = tensor.create_element_wise_product(x, mean)
    jegop = tensor.create_element_wise_product(deviation, deviation)
    print(jegop.shape)
    dispersion = tensor.create_sum(jegop, 0)
    print(dispersion.shape)
    dispersion2 = dispersion.copy()
    d_dispersion = dispersion.copy()
    d_deviation = deviation.copy()

    batch_size = tensor.Tensor([x_shape[0]], [1])

    tmp_x = x.copy()

    forward_out = x.copy()
    forward_new_out = x.copy()
    backward_out = x.copy()
    backward_new_out = x.copy()
    compare_out = x.copy()
    #dx = tensor.create_ones(x.shape)
    dx = tensor.create_randomly(x.shape)

    print(x)
    #잘 알려진 방법
    #forward
    tensor.mean_axis(x, 0, mean)
    print(mean)
    tensor.sub(x, mean, deviation)
    tensor.mul(deviation, deviation, jegop)
    tensor.mean_axis(jegop, 0, dispersion)
    tensor.function(dispersion, process_no_zero, dispersion)
    tensor.function(dispersion, math.sqrt, dispersion)
    tensor.div(deviation, dispersion, forward_out)
    #backward
    tensor.div(dx, dispersion, d_deviation)
    tensor.mul(dx, deviation, tmp_x)
    tensor.div(tmp_x, dispersion, tmp_x)
    tensor.sum_axis(tmp_x, 0, d_dispersion)
    tensor.div(tmp_x, dispersion, tmp_x)
    tensor.sum_axis(tmp_x, 0, d_dispersion)  #

    def mul_minus(x):
        return -x

    tensor.function(d_dispersion, mul_minus, d_dispersion)

    tensor.div(deviation, dispersion, tmp_x)
    tensor.mul(tmp_x, d_dispersion, tmp_x)
    tensor.div(tmp_x, batch_size, tmp_x)
    tensor.add(tmp_x, d_deviation, d_deviation)
    tensor.sum_axis(d_deviation, 0, d_mean)
    tensor.div(d_mean, batch_size, d_mean)
    tensor.sub(d_deviation, d_mean, backward_out)

    #새로운 방법
    norm.forward(x.array, x.shape, dispersion2.array, forward_new_out.array)
    backward_new_out = forward_new_out.copy()
    norm.backward(dx.array, dispersion2.array, backward_new_out.array)

    tensor.function_element_wise(backward_out, backward_new_out, isSame,
                                 compare_out)
    print('back = ')
    print(compare_out)
    tensor.function_element_wise(forward_out, forward_new_out, isSame,
                                 compare_out)
    print('forward = ')
    print(compare_out)
    dispersion_compare = dispersion.copy()
    tensor.function_element_wise(dispersion, dispersion2, isSame,
                                 dispersion_compare)
    print('dispersion = ')
    print(dispersion_compare)