Exemplo n.º 1
0
 def check_forward(self, x_data):
     x = chainer.Variable(x_data)
     y = F.log1p(x)
     testing.assert_allclose(numpy.log1p(self.x),
                             y.data,
                             atol=1e-7,
                             rtol=1e-7)
Exemplo n.º 2
0
 def lossfun_multi_leam(self, z, t):
     with cuda.get_device(z):
             z_class = z[-self.n_class:]
             z = z[:-self.n_class]
     t_class = Variable(cp.array(range(self.n_class)))
     loss = - (F.mean(t * F.log(z) + (1-t)*F.log1p(-z))) / z.shape[0] + F.softmax_cross_entropy(z_class, t_class)
     return loss   
Exemplo n.º 3
0
    def update_core(self):
        batch = self._iterators['main'].next()
        l_batch_origin = [i[2] for i in batch]
        changeflag = [i[3] for i in batch]

        x = chainer.cuda.to_gpu(np.array([i[0] for i in batch]))
        value = self.value

        labels = [l[1] for l in batch] 
        row_idx, col_idx, val_idx = [], [], []
        for i in range(len(labels)):
            l_list = list(set(labels[i])) # remove duplicate cateories to avoid double count
            for y in l_list:
                row_idx.append(i)
                col_idx.append(y)
                val_idx.append(1)
        m = len(labels)
        n = self.class_dim
        t = sp.csr_matrix((val_idx, (row_idx, col_idx)), shape=(m, n), dtype=np.int8).todense()
        

        t = chainer.cuda.to_gpu(t)
        
        optimizer = self._optimizers['main']
        y = optimizer.target(x)
        loss = F.sigmoid_cross_entropy(y, t)
        flag=1
        #それぞれのカテゴリーに対して誤差を計算する
        for i1,i2,i3,i4 in zip_longest(t,y,l_batch_origin,changeflag):
            if changeflag == '1':  
                for j1,j2 in zip_longest(i1,i2):
                    myloss = F.sigmoid_cross_entropy(j2,j1) + (j1*(F.log(F.sigmoid(j2))))#ラベルの変更がされている場合のlossの計算
                    if math.isnan(myloss.data):
                    
                        myloss = F.sigmoid_cross_entropy(j2,j1)+(j1*(F.log1p(F.sigmoid(j2))))
                    
                    if flag == 1:
                        all_loss = myloss
                        flag = 0
                        
                    else:
                        all_loss += myloss
            else:
                for j1,j2 in zip_longest(i1,i2):
                    myloss = F.sigmoid_cross_entropy(j2,j1) #ラベルの変更がされていない場合のlossの計算
                    
                    if flag == 1:
                        all_loss = myloss
                        flag = 0
                        
                    else:
                        all_loss += myloss
        all_loss = all_loss/t.size
        optimizer.target.cleargrads()
        all_loss.backward()#逆伝播
        optimizer.update()#パラメータの更新
        chainer.reporter.report({'main/loss':all_loss})#lossの書き出し
Exemplo n.º 4
0
 def _calculate_kl(W, log_sigma2):
     log_alpha = F.clip(log_sigma2 - F.log(W ** 2 + 1e-8), -8., 8.)
     clip_mask = (log_alpha.data > loga_threshold)
     normalizer = 1. / W.size
     reg = (0.63576 * F.sigmoid(1.87320 + 1.48695 * log_alpha)) + \
           (- 0.5 * F.log1p(F.exp(- log_alpha))) - 0.63576
     xp = cuda.get_array_module(reg)
     reg = F.where(clip_mask, xp.zeros(reg.shape).astype('f'), reg)
     return - F.sum(reg) * normalizer
Exemplo n.º 5
0
 def check_forward(self, x_data):
     x = chainer.Variable(x_data)
     y = F.log1p(x)
     testing.assert_allclose(
         numpy.log1p(self.x), y.data, atol=1e-7, rtol=1e-7)
Exemplo n.º 6
0
 def log1p(self, x):
     return F.log1p(x)
Exemplo n.º 7
0
 def lossfun_multi_cnn(z, t):
     with cuda.get_device(z):
         z = z
     loss = - (F.mean(t * F.log(z) + (1-t)*F.log1p(-z)))
     return loss
Exemplo n.º 8
0
 def lossfun(self, y, t):
     #return F.sigmoid_cross_entropy(y,t)
     return (y * (t - (y >= 0)) - F.log1p(F.exp(-F.abs(y))))
Exemplo n.º 9
0
 def forward(self, inputs, device):
     x, = inputs
     return functions.log1p(x),
Exemplo n.º 10
0

sum_entropy1 = 0.0
sum_entropy2 = 0.0
n = 0
for i in range(0, len(hcpes) - BATCH_SIZE, BATCH_SIZE):
    x1, x2 = mini_batch(hcpes[i:i + BATCH_SIZE])
    with chainer.no_backprop_mode():
        with chainer.using_config('train', False):
            y1, y2 = model(x1, x2)

        p1 = F.softmax(y1)
        #entropy1 = F.sum(- p1 * F.log(p1), axis=1)
        y1_max = F.max(y1, axis=1, keepdims=True)
        log_p1 = y1 - (
            F.log(F.sum(F.exp(y1 - y1_max), axis=1, keepdims=True)) + y1_max)
        entropy1 = F.sum(-p1 * log_p1, axis=1)
        sum_entropy1 += F.mean(entropy1).data

        p2 = F.sigmoid(y2)
        #entropy2 = -(p2 * F.log(p2) + (1 - p2) * F.log(1 - p2))
        log1p_ey2 = F.log1p(F.exp(y2))
        entropy2 = -(p2 * (y2 - log1p_ey2) + (1 - p2) * -log1p_ey2)
        sum_entropy2 += F.mean(entropy2).data

    n += 1

avr_entropy1 = sum_entropy1 / n
avr_entropy2 = sum_entropy2 / n
print(avr_entropy1, avr_entropy2)