我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用torch.le()。
def preProc2(x): # Access the global variables global P, expP, negExpP P = P.type_as(x) expP = expP.type_as(x) negExpP = negExpP.type_as(x) # Create a variable filled with -1. Second part of the condition z = Variable(torch.zeros(x.size())).type_as(x) absX = torch.abs(x) cond1 = torch.gt(absX, negExpP) cond2 = torch.le(absX, negExpP) if (torch.sum(cond1) > 0).data.all(): x1 = torch.sign(x[cond1]) z[cond1] = x1 if (torch.sum(cond2) > 0).data.all(): x2 = x[cond2]*expP z[cond2] = x2 return z
def allclose(x: T.FloatTensor, y: T.FloatTensor, rtol: float = 1e-05, atol: float = 1e-08) -> bool: """ Test if all elements in the two tensors are approximately equal. absolute(x - y) <= (atol + rtol * absolute(y)) Args: x: A tensor. y: A tensor. rtol (optional): Relative tolerance. atol (optional): Absolute tolerance. returns: bool: Check if all of the elements in the tensors are approximately equal. """ return tall(torch.abs(x - y).le((atol + rtol * torch.abs(y))))
def backward(self, grad_output): v1, v2, y = self.saved_tensors buffer = v1.new() _idx = self._new_idx(v1) gw1 = grad_output.new() gw2 = grad_output.new() gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(buffer, self.w1, self.w22) gw1.addcmul_(-1, buffer.expand_as(v1), v1) gw1.mul_(self.w.expand_as(v1)) torch.mul(buffer, self.w1, self.w32) gw2.addcmul_(-1, buffer.expand_as(v1), v2) gw2.mul_(self.w.expand_as(v1)) torch.le(_idx, self._outputs, 0) _idx = _idx.view(-1, 1).expand(gw1.size()) gw1[_idx] = 0 gw2[_idx] = 0 torch.eq(_idx, y, 1) _idx = _idx.view(-1, 1).expand(gw2.size()) gw1[_idx] = gw1[_idx].mul_(-1) gw2[_idx] = gw2[_idx].mul_(-1) if self.size_average: gw1.div_(y.size(0)) gw2.div_(y.size(0)) if grad_output[0] != 1: gw1.mul_(grad_output) gw2.mul_(grad_output) return gw1, gw2, None
def updateGradInput(self, input, y): v1 = input[0] v2 = input[1] gw1 = self.gradInput[0] gw2 = self.gradInput[1] gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(self.buffer, self.w1, self.w22) gw1.addcmul_(-1, self.buffer.expand_as(v1), v1) gw1.mul_(self.w.expand_as(v1)) torch.mul(self.buffer, self.w1, self.w32) gw2.addcmul_(-1, self.buffer.expand_as(v1), v2) gw2.mul_(self.w.expand_as(v1)) # self._idx = self._outputs <= 0 torch.le(self._idx, self._outputs, 0) self._idx = self._idx.view(-1, 1).expand(gw1.size()) gw1[self._idx] = 0 gw2[self._idx] = 0 torch.eq(self._idx, y, 1) self._idx = self._idx.view(-1, 1).expand(gw2.size()) gw1[self._idx] = gw1[self._idx].mul_(-1) gw2[self._idx] = gw2[self._idx].mul_(-1) if self.sizeAverage: gw1.div_(y.size(0)) gw2.div_(y.size(0)) return self.gradInput
def backward(self, grad_output): v1, v2, y = self.saved_tensors buffer = v1.new() _idx = v1.new().byte() gw1 = grad_output.new() gw2 = grad_output.new() gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(self.w1, self.w22, out=buffer) gw1.addcmul_(-1, buffer.expand_as(v1), v1) gw1.mul_(self.w.expand_as(v1)) torch.mul(self.w1, self.w32, out=buffer) gw2.addcmul_(-1, buffer.expand_as(v1), v2) gw2.mul_(self.w.expand_as(v1)) torch.le(self._outputs, 0, out=_idx) _idx = _idx.view(-1, 1).expand(gw1.size()) gw1[_idx] = 0 gw2[_idx] = 0 torch.eq(y, 1, out=_idx) _idx = _idx.view(-1, 1).expand(gw2.size()) gw1[_idx] = gw1[_idx].mul_(-1) gw2[_idx] = gw2[_idx].mul_(-1) if self.size_average: gw1.div_(y.size(0)) gw2.div_(y.size(0)) grad_output_val = grad_output[0] if grad_output_val != 1: gw1.mul_(grad_output_val) gw2.mul_(grad_output_val) return gw1, gw2, None
def updateGradInput(self, input, y): v1 = input[0] v2 = input[1] gw1 = self.gradInput[0] gw2 = self.gradInput[1] gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(self.w1, self.w22, out=self.buffer) gw1.addcmul_(-1, self.buffer.expand_as(v1), v1) gw1.mul_(self.w.expand_as(v1)) torch.mul(self.w1, self.w32, out=self.buffer) gw2.addcmul_(-1, self.buffer.expand_as(v1), v2) gw2.mul_(self.w.expand_as(v1)) # self._idx = self._outputs <= 0 torch.le(self._outputs, 0, out=self._idx) self._idx = self._idx.view(-1, 1).expand(gw1.size()) gw1[self._idx] = 0 gw2[self._idx] = 0 torch.eq(y, 1, out=self._idx) self._idx = self._idx.view(-1, 1).expand(gw2.size()) gw1[self._idx] = gw1[self._idx].mul_(-1) gw2[self._idx] = gw2[self._idx].mul_(-1) if self.sizeAverage: gw1.div_(y.size(0)) gw2.div_(y.size(0)) return self.gradInput
def backward(ctx, grad_output): v1, v2, y = ctx.saved_tensors buffer = v1.new() _idx = v1.new().byte() gw1 = grad_output.new() gw2 = grad_output.new() gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(ctx.w1, ctx.w22, out=buffer) gw1.addcmul_(-1, buffer.expand_as(v1), v1) gw1.mul_(ctx.w.expand_as(v1)) torch.mul(ctx.w1, ctx.w32, out=buffer) gw2.addcmul_(-1, buffer.expand_as(v1), v2) gw2.mul_(ctx.w.expand_as(v1)) torch.le(ctx._outputs, 0, out=_idx) _idx = _idx.view(-1, 1).expand(gw1.size()) gw1[_idx] = 0 gw2[_idx] = 0 torch.eq(y, 1, out=_idx) _idx = _idx.view(-1, 1).expand(gw2.size()) gw1[_idx] = gw1[_idx].mul_(-1) gw2[_idx] = gw2[_idx].mul_(-1) if ctx.size_average: gw1.div_(y.size(0)) gw2.div_(y.size(0)) grad_output_val = grad_output[0] if grad_output_val != 1: gw1.mul_(grad_output_val) gw2.mul_(grad_output_val) return gw1, gw2, None, None, None
def lesser_equal(x: T.FloatTensor, y: T.FloatTensor) -> T.ByteTensor: """ Elementwise test if x <= y. Args: x: A tensor. y: A tensor. Returns: tensor (of bools): Elementwise test of x <= y. """ return torch.le(x, y)