Python torch.median() Examples

The following are 30 code examples of torch.median(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch , or try the search function .
Example #1
Source File: metric_learning.py    From Landmark2019-1st-and-3rd-Place-Solution with Apache License 2.0 6 votes vote down vote up
def forward(self, input, label):
        # normalize features
        x = F.normalize(input)
        # normalize weights
        W = F.normalize(self.weight)
        # dot product
        logits = F.linear(x, W)
        # add margin
        theta = torch.acos(torch.clamp(logits, -1.0 + 1e-7, 1.0 - 1e-7))
        target_logits = torch.cos(theta + self.m)
        one_hot = torch.zeros_like(logits)
        one_hot.scatter_(1, label.view(-1, 1).long(), 1)
        if self.ls_eps > 0:
            one_hot = (1 - self.ls_eps) * one_hot + self.ls_eps / self.out_features
        output = logits * (1 - one_hot) + target_logits * one_hot
        # feature re-scale
        with torch.no_grad():
            B_avg = torch.where(one_hot < 1, torch.exp(self.s * logits), torch.zeros_like(logits))
            B_avg = torch.sum(B_avg) / input.size(0)
            theta_med = torch.median(theta)
            self.s = torch.log(B_avg) / torch.cos(torch.min(self.theta_zero * torch.ones_like(theta_med), theta_med))
        output *= self.s

        return output 
Example #2
Source File: run_original.py    From yass with Apache License 2.0 6 votes vote down vote up
def template_calculation_parallel(unit, ids, spike_array, temps, data, snipit):

    idx = np.where(ids==unit)[0]
    times = spike_array[idx]
    
    # get indexes of spikes that are not too close to end; 
    #       note: spiketimes are relative to current chunk; i.e. start at 0
    idx2 = np.where(times<(data.shape[1]-temps.shape[1]))[0]

    # grab waveforms; 
    if idx2.shape[0]>0:
        wfs = np.median(data[:,times[idx2][:,None]+
                                snipit-temps.shape[1]+1].
                                transpose(1,2,0),0)
        return (wfs, idx2.shape[0])
    else:
        return (wfs_empty, 0) 
Example #3
Source File: wmad_estimator.py    From deep_demosaick with MIT License 6 votes vote down vote up
def forward(self, x):
        db7_decomp_high = self.db7_decomp_high
        if x.shape[1] > 1:
            db7_decomp_high = torch.cat([self.db7_decomp_high]*x.shape[1], dim=0)

        if x.is_cuda:
            db7_decomp_high = db7_decomp_high.cuda()

        diagonal = F.pad(x, (0,0,self.db7_decomp_high.shape[2]//2,self.db7_decomp_high.shape[2]//2), mode='reflect')
        diagonal = F.conv2d(diagonal, db7_decomp_high, stride=(2,1), groups=x.shape[1])
        diagonal = F.pad(diagonal, (self.db7_decomp_high.shape[2]//2,self.db7_decomp_high.shape[2]//2,0,0), mode='reflect')
        diagonal = F.conv2d(diagonal.transpose(2,3), db7_decomp_high, stride=(2,1), groups=x.shape[1])
        #diagonal = diagonal.transpose(2,3)
        sigma = 0
        diagonal = diagonal.view(diagonal.shape[0],diagonal.shape[1],-1)
        for c in range(diagonal.shape[1]):
            d = diagonal[:,c]
            sigma += torch.median(torch.abs(d), dim=1)[0] / 0.6745
        sigma = sigma / diagonal.shape[1]
        sigma = sigma.detach()
        del db7_decomp_high
        return sigma 
Example #4
Source File: networks.py    From mannequinchallenge with Apache License 2.0 6 votes vote down vote up
def Confidence_Loss(self, pred_confidence, mask, pred_d, gt_d):
        # using least square to find scaling factor
        N = torch.sum(mask) + EPSILON
        N = N.item()

        if N > 0.5:
            scale_factor = torch.median(
                gt_d.data[mask.data > 0.1] /
                (pred_d.data[mask.data > 0.1] + EPSILON)).item()
            pred_d_aligned = pred_d * scale_factor

            error = torch.abs(pred_d_aligned.data -
                              gt_d.data) / (gt_d.data + EPSILON)
            error = torch.exp(-error * 2.0)

            error_var = autograd.Variable(error, requires_grad=False)
            u_loss = mask * torch.abs(pred_confidence - error_var)
            confidence_term = torch.sum(u_loss) / N
        else:
            confidence_term = 0.0

        return confidence_term 
Example #5
Source File: peak_response_mapping.py    From ultra-thin-PRM with MIT License 6 votes vote down vote up
def __init__(self, *args, **kargs):
        super(PeakResponseMapping, self).__init__(*args)

        self.inferencing = False
        # use global average pooling to aggregate responses if peak stimulation is disabled
        self.enable_peak_stimulation = kargs.get('enable_peak_stimulation', True)
        # return only the class response maps in inference mode if peak backpropagation is disabled
        self.enable_peak_backprop = kargs.get('enable_peak_backprop', True)
        # window size for peak finding
        self.win_size = kargs.get('win_size', 3)
        # sub-pixel peak finding
        self.sub_pixel_locating_factor = kargs.get('sub_pixel_locating_factor', 1)
        # peak filtering
        self.filter_type = kargs.get('filter_type', 'median')
        if self.filter_type == 'median':
            self.peak_filter = self._median_filter
        elif self.filter_type == 'mean':
            self.peak_filter = self._mean_filter
        elif self.filter_type == 'max':
            self.peak_filter = self._max_filter
        elif isinstance(self.filter_type, (int, float)):
            self.peak_filter = lambda x: self.filter_type
        else:
            self.peak_filter = None 
Example #6
Source File: metrics.py    From pytorch-adacos with MIT License 6 votes vote down vote up
def forward(self, input, label=None):
        # normalize features
        x = F.normalize(input)
        # normalize weights
        W = F.normalize(self.W)
        # dot product
        logits = F.linear(x, W)
        if label is None:
            return logits
        # feature re-scale
        theta = torch.acos(torch.clamp(logits, -1.0 + 1e-7, 1.0 - 1e-7))
        one_hot = torch.zeros_like(logits)
        one_hot.scatter_(1, label.view(-1, 1).long(), 1)
        with torch.no_grad():
            B_avg = torch.where(one_hot < 1, torch.exp(self.s * logits), torch.zeros_like(logits))
            B_avg = torch.sum(B_avg) / input.size(0)
            # print(B_avg)
            theta_med = torch.median(theta[one_hot == 1])
            self.s = torch.log(B_avg) / torch.cos(torch.min(math.pi/4 * torch.ones_like(theta_med), theta_med))
        output = self.s * logits

        return output 
Example #7
Source File: dual_layers.py    From provable-robustness-max-linear-regions with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def bounds(self, network=None):
        if self.I_empty:
            return 0, 0

        if network is None:
            nu = self.nus[-1]
            no = self.nu_ones[-1]
        else:
            nu = network(self.nus[0])
            no = network(self.nu_ones[0])

        n = torch.median(self.nus[-1].abs(), 1)[0]

        # From notes: 
        # \sum_i l_i[nu_i]_+ \approx (-n + no)/2
        # which is the negative of the term for the upper bound
        # for the lower bound, use -nu and negate the output, so 
        # (n - no)/2 since the no term flips twice and the l1 term
        # flips only once. 
        zl = (-n - no) / 2
        zu = (n - no) / 2

        return zl, zu 
Example #8
Source File: dual_inputs.py    From provable-robustness-max-linear-regions with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def bounds(self, network=None): 
        if network is None: 
            nu_u = self.nu_u[-1]
            nu_one_u = self.nu_one_u[-1]
            nu_l = self.nu_l[-1]
            nu_one_l = self.nu_one_l[-1]
        else: 
            nu_u = network(self.nu_u[0])
            nu_one_u = network(self.nu_one_u[0])
            nu_l = network(self.nu_l[0])
            nu_one_l = network(self.nu_one_l[0])

        nu_l1_u = torch.median(nu_u.abs(),1)[0]
        nu_pos_u = (nu_l1_u + nu_one_u)/2
        nu_neg_u = (-nu_l1_u + nu_one_u)/2

        nu_l1_l = torch.median(nu_l.abs(),1)[0]
        nu_pos_l = (nu_l1_l + nu_one_l)/2
        nu_neg_l = (-nu_l1_l + nu_one_l)/2

        zu = nu_pos_u + nu_neg_l
        zl = nu_neg_u + nu_pos_l
        return zl,zu 
Example #9
Source File: inspector.py    From Torchelie with MIT License 6 votes vote down vote up
def analyze(self, batch, pred, true, pred_label=None, paths=None):
        pred = torch.sigmoid(pred)
        for_label = torch.median((pred * true + (1 - pred) * (1 - true)).reshape(pred.shape[0], -1), -1)[0]
        if pred_label is None:
            pred_label = (pred > 0.5).int()
        if paths is None:
            paths = [None] * len(batch)
        this_data = list(zip(batch, for_label, true.mean(tuple(range(1,true.dim()))) > 0.5, (pred_label == true).float().mean(tuple(range(1,pred_label.dim()))) > 0.5,
                             paths))

        self.best += this_data
        self.best.sort(key=lambda x: -x[1])
        self.best = self.best[:self.topk]

        self.worst += this_data
        self.worst.sort(key=lambda x: x[1])
        self.worst = self.worst[:self.topk]

        self.confused += this_data
        self.confused.sort(key=lambda x: abs(self.center_value - x[1]))
        self.confused = self.confused[:self.topk] 
Example #10
Source File: dual_layers.py    From CROWN-IBP with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def bounds(self, network=None): 
        if self.I_empty: 
            return 0,0

        if network is None: 
            nu = self.nus[-1]
            no = self.nu_ones[-1]
        else: 
            nu = network(self.nus[0])
            no = network(self.nu_ones[0])

        n = torch.median(nu.abs(), 1)[0]

        # From notes: 
        # \sum_i l_i[nu_i]_+ \approx (-n + no)/2
        # which is the negative of the term for the upper bound
        # for the lower bound, use -nu and negate the output, so 
        # (n - no)/2 since the no term flips twice and the l1 term
        # flips only once. 
        zl = (-n - no)/2
        zu = (n - no)/2

        return zl,zu 
Example #11
Source File: dual_inputs.py    From CROWN-IBP with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def bounds(self, network=None): 
        if network is None: 
            nu_u = self.nu_u[-1]
            nu_one_u = self.nu_one_u[-1]
            nu_l = self.nu_l[-1]
            nu_one_l = self.nu_one_l[-1]
        else: 
            nu_u = network(self.nu_u[0])
            nu_one_u = network(self.nu_one_u[0])
            nu_l = network(self.nu_l[0])
            nu_one_l = network(self.nu_one_l[0])

        nu_l1_u = torch.median(nu_u.abs(),1)[0]
        nu_pos_u = (nu_l1_u + nu_one_u)/2
        nu_neg_u = (-nu_l1_u + nu_one_u)/2

        nu_l1_l = torch.median(nu_l.abs(),1)[0]
        nu_pos_l = (nu_l1_l + nu_one_l)/2
        nu_neg_l = (-nu_l1_l + nu_one_l)/2

        zu = nu_pos_u + nu_neg_l
        zl = nu_neg_u + nu_pos_l
        return zl,zu

# L2 balls 
Example #12
Source File: dual_layers.py    From convex_adversarial with MIT License 6 votes vote down vote up
def bounds(self, network=None): 
        if self.I_empty: 
            return 0,0

        if network is None: 
            nu = self.nus[-1]
            no = self.nu_ones[-1]
        else: 
            nu = network(self.nus[0])
            no = network(self.nu_ones[0])

        n = torch.median(nu.abs(), 1)[0]

        # From notes: 
        # \sum_i l_i[nu_i]_+ \approx (-n + no)/2
        # which is the negative of the term for the upper bound
        # for the lower bound, use -nu and negate the output, so 
        # (n - no)/2 since the no term flips twice and the l1 term
        # flips only once. 
        zl = (-n - no)/2
        zu = (n - no)/2

        return zl,zu 
Example #13
Source File: functional.py    From audio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _median_smoothing(
        indices: Tensor,
        win_length: int
) -> Tensor:
    r"""
    Apply median smoothing to the 1D tensor over the given window.
    """

    # Centered windowed
    pad_length = (win_length - 1) // 2

    # "replicate" padding in any dimension
    indices = torch.nn.functional.pad(
        indices, (pad_length, 0), mode="constant", value=0.
    )

    indices[..., :pad_length] = torch.cat(pad_length * [indices[..., pad_length].unsqueeze(-1)], dim=-1)
    roll = indices.unfold(-1, win_length, 1)

    values, _ = torch.median(roll, -1)
    return values 
Example #14
Source File: dual_inputs.py    From convex_adversarial with MIT License 6 votes vote down vote up
def bounds(self, network=None): 
        if network is None: 
            nu_u = self.nu_u[-1]
            nu_one_u = self.nu_one_u[-1]
            nu_l = self.nu_l[-1]
            nu_one_l = self.nu_one_l[-1]
        else: 
            nu_u = network(self.nu_u[0])
            nu_one_u = network(self.nu_one_u[0])
            nu_l = network(self.nu_l[0])
            nu_one_l = network(self.nu_one_l[0])

        nu_l1_u = torch.median(nu_u.abs(),1)[0]
        nu_pos_u = (nu_l1_u + nu_one_u)/2
        nu_neg_u = (-nu_l1_u + nu_one_u)/2

        nu_l1_l = torch.median(nu_l.abs(),1)[0]
        nu_pos_l = (nu_l1_l + nu_one_l)/2
        nu_neg_l = (-nu_l1_l + nu_one_l)/2

        zu = nu_pos_u + nu_neg_l
        zl = nu_neg_u + nu_pos_l
        return zl,zu

# L2 balls 
Example #15
Source File: dual_layers.py    From provable-robustness-max-linear-regions with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def select_layer(layer, dual_net, X, l1_proj, l1_type, in_f, out_f, zsi,
                 zl=None, zu=None):
    if isinstance(layer, nn.Linear):
        return DualLinear(layer, out_f)
    elif isinstance(layer, nn.Conv2d) or isinstance(layer, cl.Conv2dUntiedBias):
        return DualConv2d(layer, out_f)
    elif isinstance(layer, nn.ReLU):
        if zl is None and zu is None:
            zl, zu = zip(*[l.bounds() for l in dual_net])
            # for l,dn in zip(zl,dual_net):
            #     print(dn, l.size())
            zl, zu = sum(zl), sum(zu)
        if zl is None or zu is None:
            raise ValueError("Must either provide both l,u bounds or neither.")

        I = ((zu > 0).detach() * (zl < 0).detach())
        if l1_proj is not None and l1_type == 'median' and I.sum().item() > l1_proj:
            return DualReLUProj(zl, zu, l1_proj)
        else:
            return DualReLU(zl, zu)

    elif 'Flatten' in (str(layer.__class__.__name__)):
        return DualReshape(in_f, out_f)
    elif isinstance(layer, Dense):
        return DualDense(layer, dual_net, out_f)
    elif isinstance(layer, nn.BatchNorm2d):
        return DualBatchNorm2d(layer, zsi, out_f)
    else:
        print(layer)
        raise ValueError("No module for layer {}".format(str(layer.__class__.__name__))) 
Example #16
Source File: functional.py    From audio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _find_max_per_frame(
        nccf: Tensor,
        sample_rate: int,
        freq_high: int
) -> Tensor:
    r"""
    For each frame, take the highest value of NCCF,
    apply centered median smoothing, and convert to frequency.

    Note: If the max among all the lags is very close
    to the first half of lags, then the latter is taken.
    """

    lag_min = int(math.ceil(sample_rate / freq_high))

    # Find near enough max that is smallest

    best = torch.max(nccf[..., lag_min:], -1)

    half_size = nccf.shape[-1] // 2
    half = torch.max(nccf[..., lag_min:half_size], -1)

    best = _combine_max(half, best)
    indices = best[1]

    # Add back minimal lag
    indices += lag_min
    # Add 1 empirical calibration offset
    indices += 1

    return indices 
Example #17
Source File: test.py    From SphericalViewSynthesis with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def compute_errors(gt, pred, invalid_mask, weights, sampling, mode='cpu', median_scale=False):
    b, _, __, ___ = gt.size()
    scale = torch.median(gt.reshape(b, -1), dim=1)[0] / torch.median(pred.reshape(b, -1), dim=1)[0]\
        if median_scale else torch.tensor(1.0).expand(b, 1, 1, 1).to(gt.device) 
    pred = pred * scale.reshape(b, 1, 1, 1)
    valid_sum = torch.sum(~invalid_mask, dim=[1, 2, 3], keepdim=True)
    gt[invalid_mask] = 0.0
    pred[invalid_mask] = 0.0
    thresh = torch.max((gt / pred), (pred / gt))
    thresh[invalid_mask | (sampling < 0.5)] = 2.0
    
    sum_dims = [1, 2, 3]
    delta_valid_sum = torch.sum(~invalid_mask & (sampling > 0), dim=[1, 2, 3], keepdim=True)
    delta1 = (thresh < 1.25   ).float().sum(dim=sum_dims, keepdim=True).float() / delta_valid_sum.float()
    delta2 = (thresh < (1.25 ** 2)).float().sum(dim=sum_dims, keepdim=True).float() / delta_valid_sum.float()
    delta3 = (thresh < (1.25 ** 3)).float().sum(dim=sum_dims, keepdim=True).float() / delta_valid_sum.float()

    rmse = (gt - pred) ** 2
    rmse[invalid_mask] = 0.0
    rmse_w = rmse * weights
    rmse_mean = torch.sqrt(rmse_w.sum(dim=sum_dims, keepdim=True) / valid_sum.float())

    rmse_log = (torch.log(gt) - torch.log(pred)) ** 2
    rmse_log[invalid_mask] = 0.0
    rmse_log_w = rmse_log * weights
    rmse_log_mean = torch.sqrt(rmse_log_w.sum(dim=sum_dims, keepdim=True) / valid_sum.float())

    abs_rel = (torch.abs(gt - pred) / gt)
    abs_rel[invalid_mask] = 0.0
    abs_rel_w = abs_rel * weights
    abs_rel_mean = abs_rel_w.sum(dim=sum_dims, keepdim=True) / valid_sum.float()

    sq_rel = (((gt - pred)**2) / gt)
    sq_rel[invalid_mask] = 0.0
    sq_rel_w = sq_rel * weights
    sq_rel_mean = sq_rel_w.sum(dim=sum_dims, keepdim=True) / valid_sum.float()

    return (abs_rel_mean, abs_rel), (sq_rel_mean, sq_rel), (rmse_mean, rmse), \
        (rmse_log_mean, rmse_log), delta1, delta2, delta3 
Example #18
Source File: test.py    From SphericalViewSynthesis with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def parse_arguments(args):
    usage_text = (
        "Semi-supervised Spherical Depth Estimation Testing."        
    )
    parser = argparse.ArgumentParser(description=usage_text)
    # enumerables
    parser.add_argument('-b',"--batch_size", type=int, help="Test a <batch_size> number of samples each iteration.")    
    parser.add_argument('--save_iters', type=int, default=100, help='Maximum test iterations whose results will be saved.')
    # paths    
    parser.add_argument("--test_path", type=str, help="Path to the testing file containing the test set file paths")
    parser.add_argument("--save_path", type=str, help="Path to the folder where the models and results will be saved at.")
    # model
    parser.add_argument("--configuration", required=False, type=str, default='mono', help="Data loader configuration <mono>, <lr>, <ud>, <tc>", choices=['mono', 'lr', 'ud', 'tc'])
    parser.add_argument('--weights', type=str, help='Path to the trained weights file.')
    parser.add_argument('--model', default="default", type=str, help='Model selection argument.')    
    # hardware
    parser.add_argument('-g','--gpu', type=str, default='0', help='The ids of the GPU(s) that will be utilized. (e.g. 0 or 0,1, or 0,2). Use -1 for CPU.')
    # other
    parser.add_argument('-n','--name', type=str, default='default_name', help='The name of this train/test. Used when storing information.')    
    parser.add_argument("--visdom", type=str, nargs='?', default=None, const="127.0.0.1", help="Visdom server IP (port defaults to 8097)")
    parser.add_argument("--visdom_iters", type=int, default=400, help = "Iteration interval that results will be reported at the visdom server for visualization.")
    # metrics
    parser.add_argument("--depth_thres", type=float, default=20.0, help = "Depth threshold - depth clipping.")
    parser.add_argument("--width", type=float, default=512, help = "Spherical image width.")
    parser.add_argument("--baseline", type=float, default=0.26, help = "Stereo baseline distance (in either axis).")
    parser.add_argument("--median_scale", required=False, default=False, action="store_true", help = "Perform median scaling before calculating metrics.")
    parser.add_argument("--spherical_weights", required=False, default=False, action="store_true", help = "Use spherical weighting when calculating the metrics.")
    parser.add_argument("--spherical_sampling", required=False, default=False, action="store_true", help = "Use spherical sampling when calculating the metrics.")
    # save options    
    parser.add_argument("--save_recon", required=False, default=False, action="store_true", help = "Flag to toggle reconstructed result saving.")
    parser.add_argument("--save_original", required=False, default=False, action="store_true", help = "Flag to toggle input (image) saving.")
    parser.add_argument("--save_depth", required=False, default=False, action="store_true", help = "Flag to toggle output (depth) saving.")    
    return parser.parse_known_args(args) 
Example #19
Source File: functional.py    From audio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def detect_pitch_frequency(
        waveform: Tensor,
        sample_rate: int,
        frame_time: float = 10 ** (-2),
        win_length: int = 30,
        freq_low: int = 85,
        freq_high: int = 3400,
) -> Tensor:
    r"""Detect pitch frequency.

    It is implemented using normalized cross-correlation function and median smoothing.

    Args:
        waveform (Tensor): Tensor of audio of dimension (..., freq, time)
        sample_rate (int): The sample rate of the waveform (Hz)
        frame_time (float, optional): Duration of a frame (Default: ``10 ** (-2)``).
        win_length (int, optional): The window length for median smoothing (in number of frames) (Default: ``30``).
        freq_low (int, optional): Lowest frequency that can be detected (Hz) (Default: ``85``).
        freq_high (int, optional): Highest frequency that can be detected (Hz) (Default: ``3400``).

    Returns:
        Tensor: Tensor of freq of dimension (..., frame)
    """
    # pack batch
    shape = list(waveform.size())
    waveform = waveform.reshape([-1] + shape[-1:])

    nccf = _compute_nccf(waveform, sample_rate, frame_time, freq_low)
    indices = _find_max_per_frame(nccf, sample_rate, freq_high)
    indices = _median_smoothing(indices, win_length)

    # Convert indices to frequency
    EPSILON = 10 ** (-9)
    freq = sample_rate / (EPSILON + indices.to(torch.float))

    # unpack batch
    freq = freq.reshape(shape[:-1] + list(freq.shape[-1:]))

    return freq 
Example #20
Source File: peak_response_mapping.py    From ultra-thin-PRM with MIT License 5 votes vote down vote up
def _median_filter(input):
        batch_size, num_channels, h, w = input.size()
        threshold, _ = torch.median(input.view(batch_size, num_channels, h * w), dim=2)
        return threshold.contiguous().view(batch_size, num_channels, 1, 1) 
Example #21
Source File: ada_lanczos_net.py    From LanczosNetwork with MIT License 5 votes vote down vote up
def _get_graph_laplacian(self, node_feat, adj_mask):
    """ Compute graph Laplacian

      Args:
        node_feat: float tensor, shape B X N X D
        adj_mask: float tensor, shape B X N X N, binary mask, should contain self-loop

      Returns:
        L: float tensor, shape B X N X N
    """
    batch_size = node_feat.shape[0]
    num_node = node_feat.shape[1]
    dim_feat = node_feat.shape[2]

    # compute pairwise distance
    idx_row, idx_col = np.meshgrid(range(num_node), range(num_node))
    idx_row, idx_col = torch.Tensor(idx_row.reshape(-1)).long().to(node_feat.device), torch.Tensor(
        idx_col.reshape(-1)).long().to(node_feat.device)

    diff = node_feat[:, idx_row, :] - node_feat[:, idx_col, :]  # shape B X N^2 X D
    dist2 = (diff * diff).sum(dim=2)  # shape B X N^2
    
    # sigma2, _ = torch.median(dist2, dim=1, keepdim=True) # median is sometimes 0
    # sigma2 = sigma2 + 1.0e-7

    sigma2 = torch.mean(dist2, dim=1, keepdim=True)

    A = torch.exp(-dist2 / sigma2)  # shape B X N^2
    A = A.reshape(batch_size, num_node, num_node) * adj_mask  # shape B X N X N
    row_sum = torch.sum(A, dim=2, keepdim=True)
    pad_row_sum = torch.zeros_like(row_sum)
    pad_row_sum[row_sum == 0.0] = 1.0    
    alpha = 0.5
    D = 1.0 / (row_sum + pad_row_sum).pow(alpha)  # shape B X N X 1
    L = D * A * D.transpose(1, 2)  # shape B X N X N

    return L 
Example #22
Source File: median_absolute_error.py    From ignite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def median_absolute_error_compute_fn(y_pred, y):
    e = torch.abs(y.view_as(y_pred) - y_pred)
    return torch.median(e).item() 
Example #23
Source File: median_relative_absolute_error.py    From ignite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def median_relative_absolute_error_compute_fn(y_pred, y):
    e = torch.abs(y.view_as(y_pred) - y_pred) / torch.abs(y.view_as(y_pred) - torch.mean(y))
    return torch.median(e).item() 
Example #24
Source File: median_absolute_percentage_error.py    From ignite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def median_absolute_percentage_error_compute_fn(y_pred, y):
    e = torch.abs(y.view_as(y_pred) - y_pred) / torch.abs(y.view_as(y_pred))
    return 100.0 * torch.median(e).item() 
Example #25
Source File: loss_functions.py    From SfmLearner-Pytorch with MIT License 5 votes vote down vote up
def compute_errors(gt, pred, crop=True):
    abs_diff, abs_rel, sq_rel, a1, a2, a3 = 0,0,0,0,0,0
    batch_size = gt.size(0)

    '''
    crop used by Garg ECCV16 to reprocude Eigen NIPS14 results
    construct a mask of False values, with the same size as target
    and then set to True values inside the crop
    '''
    if crop:
        crop_mask = gt[0] != gt[0]
        y1,y2 = int(0.40810811 * gt.size(1)), int(0.99189189 * gt.size(1))
        x1,x2 = int(0.03594771 * gt.size(2)), int(0.96405229 * gt.size(2))
        crop_mask[y1:y2,x1:x2] = 1

    for current_gt, current_pred in zip(gt, pred):
        valid = (current_gt > 0) & (current_gt < 80)
        if crop:
            valid = valid & crop_mask

        valid_gt = current_gt[valid]
        valid_pred = current_pred[valid].clamp(1e-3, 80)

        valid_pred = valid_pred * torch.median(valid_gt)/torch.median(valid_pred)

        thresh = torch.max((valid_gt / valid_pred), (valid_pred / valid_gt))
        a1 += (thresh < 1.25).float().mean()
        a2 += (thresh < 1.25 ** 2).float().mean()
        a3 += (thresh < 1.25 ** 3).float().mean()

        abs_diff += torch.mean(torch.abs(valid_gt - valid_pred))
        abs_rel += torch.mean(torch.abs(valid_gt - valid_pred) / valid_gt)

        sq_rel += torch.mean(((valid_gt - valid_pred)**2) / valid_gt)

    return [metric.item() / batch_size for metric in [abs_diff, abs_rel, sq_rel, a1, a2, a3]] 
Example #26
Source File: graphnn.py    From torchsupport with MIT License 5 votes vote down vote up
def __init__(self, traversal=StandardNodeTraversal(1)):
    super(NeighbourMedian, self).__init__(torch.median, traversal=traversal) 
Example #27
Source File: dual_inputs.py    From provable-robustness-max-linear-regions with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def bounds(self, network=None):
        if network is None: 
            nu = self.nu[-1]
            nu_x = self.nu_x[-1]
        else: 
            nu = network(self.nu[0])
            nu_x = network(self.nu_x[0])

        l1 = torch.median(self.nu[-1].abs(), 1)[0]
        return (self.nu_x[-1] - self.epsilon*l1, 
                self.nu_x[-1] + self.epsilon*l1) 
Example #28
Source File: dual_inputs.py    From provable-robustness-max-linear-regions with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def select_input(X, epsilon, l1_proj, l1_type, bounded_input, q_norm):
    if l1_proj is not None and l1_type=='median' and X[0].numel() > l1_proj:
        if bounded_input: 
            return InfBallProjBounded(X,epsilon,l1_proj)
        else: 
            return InfBallProj(X,epsilon,l1_proj)
    else:
        if bounded_input: 
            return InfBallBounded(X, epsilon)
        else:
            return InfBall(X, epsilon, q_norm) 
Example #29
Source File: world_model_evaluator.py    From ReAgent with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def compute_median_feature_value(self, features):
        # enum type
        if features.shape[1] > 1:
            feature_counts = torch.sum(features, dim=0)
            median_feature_counts = torch.median(feature_counts)
            # no similar method as numpy.where in torch
            for i in range(features.shape[1]):
                if feature_counts[i] == median_feature_counts:
                    break
            median_feature = torch.zeros(features.shape[1])
            median_feature[i] = 1
        # other types
        else:
            median_feature = features.mean(dim=0)
        return median_feature 
Example #30
Source File: net.py    From TimeSeries with Apache License 2.0 5 votes vote down vote up
def test(self, x, v_batch, id_batch, hidden, cell, sampling=False):
        batch_size = x.shape[1]
        if sampling:
            samples = torch.zeros(self.params.sample_times, batch_size, self.params.predict_steps,
                                       device=self.params.device)
            for j in range(self.params.sample_times):
                decoder_hidden = hidden
                decoder_cell = cell
                for t in range(self.params.predict_steps):
                    mu_de, sigma_de, decoder_hidden, decoder_cell = self(x[self.params.predict_start + t].unsqueeze(0),
                                                                         id_batch, decoder_hidden, decoder_cell)
                    gaussian = torch.distributions.normal.Normal(mu_de, sigma_de)
                    pred = gaussian.sample()  # not scaled
                    samples[j, :, t] = pred * v_batch[:, 0] + v_batch[:, 1]
                    if t < (self.params.predict_steps - 1):
                        x[self.params.predict_start + t + 1, :, 0] = pred

            sample_mu = torch.median(samples, dim=0)[0]
            sample_sigma = samples.std(dim=0)
            return samples, sample_mu, sample_sigma

        else:
            decoder_hidden = hidden
            decoder_cell = cell
            sample_mu = torch.zeros(batch_size, self.params.predict_steps, device=self.params.device)
            sample_sigma = torch.zeros(batch_size, self.params.predict_steps, device=self.params.device)
            for t in range(self.params.predict_steps):
                mu_de, sigma_de, decoder_hidden, decoder_cell = self(x[self.params.predict_start + t].unsqueeze(0),
                                                                     id_batch, decoder_hidden, decoder_cell)
                sample_mu[:, t] = mu_de * v_batch[:, 0] + v_batch[:, 1]
                sample_sigma[:, t] = sigma_de * v_batch[:, 0]
                if t < (self.params.predict_steps - 1):
                    x[self.params.predict_start + t + 1, :, 0] = mu_de
            return sample_mu, sample_sigma