我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用torch.numel()。
def grad_sparsity(self): global_state = self._global_state if self._iter == 0: global_state["sparsity_avg"] = 0.0 non_zero_cnt = 0.0 all_entry_cnt = 0.0 for group in self._optimizer.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data grad_non_zero = grad.nonzero() if grad_non_zero.dim() > 0: non_zero_cnt += grad_non_zero.size()[0] all_entry_cnt += torch.numel(grad) beta = self._beta global_state["sparsity_avg"] = beta * global_state["sparsity_avg"] \ + (1 - beta) * non_zero_cnt / float(all_entry_cnt) self._sparsity_avg = \ global_state["sparsity_avg"] / self.zero_debias_factor() if self._verbose: logging.debug("sparsity %f, sparsity avg %f", non_zero_cnt / float(all_entry_cnt), self._sparsity_avg) return
def grad_sparsity(self): global_state = self._global_state if self._iter == 0: global_state["sparsity_avg"] = 0.0 non_zero_cnt = 0.0 all_entry_cnt = 0.0 for group in self._optimizer.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data grad_non_zero = grad.nonzero() if grad_non_zero.dim() > 0: non_zero_cnt += grad_non_zero.size()[0] all_entry_cnt += torch.numel(grad) beta = self._beta global_state["sparsity_avg"] = beta * global_state["sparsity_avg"] \ + (1 - beta) * non_zero_cnt / float(all_entry_cnt) self._sparsity_avg = \ global_state["sparsity_avg"] / self.zero_debias_factor() if DEBUG: logging.debug("sparsity %f, sparsity avg %f", non_zero_cnt / float(all_entry_cnt), self._sparsity_avg) return
def split_ps(point_set): #print point_set.size() num_points = point_set.size()[0]/2 diff = point_set.max(dim=0)[0] - point_set.min(dim=0)[0] dim = torch.max(diff, dim = 1)[1][0,0] cut = torch.median(point_set[:,dim])[0][0] left_idx = torch.squeeze(torch.nonzero(point_set[:,dim] > cut)) right_idx = torch.squeeze(torch.nonzero(point_set[:,dim] < cut)) middle_idx = torch.squeeze(torch.nonzero(point_set[:,dim] == cut)) if torch.numel(left_idx) < num_points: left_idx = torch.cat([left_idx, middle_idx[0:1].repeat(num_points - torch.numel(left_idx))], 0) if torch.numel(right_idx) < num_points: right_idx = torch.cat([right_idx, middle_idx[0:1].repeat(num_points - torch.numel(right_idx))], 0) left_ps = torch.index_select(point_set, dim = 0, index = left_idx) right_ps = torch.index_select(point_set, dim = 0, index = right_idx) return left_ps, right_ps, dim
def split_ps(point_set): #print point_set.size() num_points = point_set.size()[0]/2 diff = point_set.max(dim=0, keepdim = True)[0] - point_set.min(dim=0, keepdim = True)[0] dim = torch.max(diff, dim = 1, keepdim = True)[1][0,0] cut = torch.median(point_set[:,dim], keepdim = True)[0][0] left_idx = torch.squeeze(torch.nonzero(point_set[:,dim] > cut)) right_idx = torch.squeeze(torch.nonzero(point_set[:,dim] < cut)) middle_idx = torch.squeeze(torch.nonzero(point_set[:,dim] == cut)) if torch.numel(left_idx) < num_points: left_idx = torch.cat([left_idx, middle_idx[0:1].repeat(num_points - torch.numel(left_idx))], 0) if torch.numel(right_idx) < num_points: right_idx = torch.cat([right_idx, middle_idx[0:1].repeat(num_points - torch.numel(right_idx))], 0) left_ps = torch.index_select(point_set, dim = 0, index = left_idx) right_ps = torch.index_select(point_set, dim = 0, index = right_idx) return left_ps, right_ps, dim
def split_ps(point_set): #print point_set.size() num_points = point_set.size()[0]/2 diff = point_set.max(dim=0)[0] - point_set.min(dim=0)[0] diff = diff[:3] dim = torch.max(diff, dim = 1)[1][0,0] cut = torch.median(point_set[:,dim])[0][0] left_idx = torch.squeeze(torch.nonzero(point_set[:,dim] > cut)) right_idx = torch.squeeze(torch.nonzero(point_set[:,dim] < cut)) middle_idx = torch.squeeze(torch.nonzero(point_set[:,dim] == cut)) if torch.numel(left_idx) < num_points: left_idx = torch.cat([left_idx, middle_idx[0:1].repeat(num_points - torch.numel(left_idx))], 0) if torch.numel(right_idx) < num_points: right_idx = torch.cat([right_idx, middle_idx[0:1].repeat(num_points - torch.numel(right_idx))], 0) left_ps = torch.index_select(point_set, dim = 0, index = left_idx) right_ps = torch.index_select(point_set, dim = 0, index = right_idx) return left_ps, right_ps, dim
def grad_sparsity(self): global_state = self._global_state if self._iter == 0: global_state["sparsity_avg"] = 0.0 non_zero_cnt = 0.0 all_entry_cnt = 0.0 for group in self._optimizer.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data grad_non_zero = grad.nonzero() if grad_non_zero.dim() > 0: non_zero_cnt += grad_non_zero.size()[0] all_entry_cnt += torch.numel(grad) beta = self._beta global_state["sparsity_avg"] = beta * global_state["sparsity_avg"] \ + (1 - beta) * non_zero_cnt / float(all_entry_cnt) self._sparsity_avg = \ global_state["sparsity_avg"] / self.zero_debias_factor() return
def to_float(val): """ Check that val is one of the following: - pytorch autograd Variable with one element - pytorch tensor with one element - numpy array with one element - any type supporting float() operation And convert val to float """ if isinstance(val, np.ndarray): assert val.size == 1, \ "val should have one element (got {})".format(val.size) return float(val.squeeze()[0]) if torch is not None: if isinstance(val, torch_autograd.Variable): val = val.data if torch.is_tensor(val): assert torch.numel(val) == 1, \ "val should have one element (got {})".format(torch.numel(val)) return float(val.squeeze()[0]) try: return float(val) except: raise TypeError("Unsupported type for val ({})".format(type(val)))
def split_ps_reuse(point_set, level, pos, tree, cutdim): sz = point_set.size() num_points = np.array(sz)[0]/2 max_value = point_set.max(dim=0)[0] min_value = -(-point_set).max(dim=0)[0] diff = max_value - min_value dim = torch.max(diff, dim = 1)[1][0,0] cut = torch.median(point_set[:,dim])[0][0] left_idx = torch.squeeze(torch.nonzero(point_set[:,dim] > cut)) right_idx = torch.squeeze(torch.nonzero(point_set[:,dim] < cut)) middle_idx = torch.squeeze(torch.nonzero(point_set[:,dim] == cut)) if torch.numel(left_idx) < num_points: left_idx = torch.cat([left_idx, middle_idx[0:1].repeat(num_points - torch.numel(left_idx))], 0) if torch.numel(right_idx) < num_points: right_idx = torch.cat([right_idx, middle_idx[0:1].repeat(num_points - torch.numel(right_idx))], 0) left_ps = torch.index_select(point_set, dim = 0, index = left_idx) right_ps = torch.index_select(point_set, dim = 0, index = right_idx) tree[level+1][pos * 2] = left_ps tree[level+1][pos * 2 + 1] = right_ps cutdim[level][pos * 2] = dim cutdim[level][pos * 2 + 1] = dim return
def split_ps_reuse(point_set, level, pos, tree, cutdim): sz = point_set.size() num_points = np.array(sz)[0]/2 max_value = point_set.max(dim=0)[0] min_value = -(-point_set).max(dim=0)[0] diff = max_value - min_value diff = diff[:,:3] dim = torch.max(diff, dim = 1)[1][0,0] cut = torch.median(point_set[:,dim])[0][0] left_idx = torch.squeeze(torch.nonzero(point_set[:,dim] > cut)) right_idx = torch.squeeze(torch.nonzero(point_set[:,dim] < cut)) middle_idx = torch.squeeze(torch.nonzero(point_set[:,dim] == cut)) if torch.numel(left_idx) < num_points: left_idx = torch.cat([left_idx, middle_idx[0:1].repeat(num_points - torch.numel(left_idx))], 0) if torch.numel(right_idx) < num_points: right_idx = torch.cat([right_idx, middle_idx[0:1].repeat(num_points - torch.numel(right_idx))], 0) left_ps = torch.index_select(point_set, dim = 0, index = left_idx) right_ps = torch.index_select(point_set, dim = 0, index = right_idx) tree[level+1][pos * 2] = left_ps tree[level+1][pos * 2 + 1] = right_ps cutdim[level][pos * 2] = dim cutdim[level][pos * 2 + 1] = dim return