您当前的位置: 首页 >  Python

惊鸿一博

暂无认证

  • 1浏览

    0关注

    535博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

python_numpy_求L1损失的两种方法

惊鸿一博 发布时间:2020-12-21 21:16:26 ,浏览量:1

# 假设只有大于0的值是有效值
#方法1: 手动求(目标值: target, 预测值:pred);
        tmask = target > 0   
        #统计depth图中非0元素个数
        tmp_depth = target.copy()
        tmp_depth[tmp_depth !=  0 ] = 1
        tmp_depth[tmp_depth == 0] = 0
        num = tmp_depth.sum()
        
        difmap = abs(target[tmask] - pred[tmask])
        L1 = difmap.sum()/num
        L1Dif = 'L1 difference: ' + str(L1) + '\r\n'
        print(L1Dif)

#方法2:使用 np.sum;num.abs
    def get_L1Loss(self, pred, target):
        '''
        pred: numpy
        target: numpy
        '''
        valid_mask = target>0
        diff = target - pred
        diff = diff[valid_mask]
        loss = np.mean(np.abs(diff))
        return loss

 

关注
打赏
1663399408
查看更多评论
立即登录/注册

微信扫码登录

0.0408s