python类sqrt()的实例源码

cfg.py 文件源码 项目:pytorch-caffe-darknet-convert 作者: marvis 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def save_conv_shrink_bn(fp, conv_model, bn_model, eps=1e-5):
    if bn_model.bias.is_cuda:
        bias = bn_model.bias.data - bn_model.running_mean * bn_model.weight.data / torch.sqrt(bn_model.running_var + eps)
        convert2cpu(bias).numpy().tofile(fp)
        s = conv_model.weight.data.size()
        weight = conv_model.weight.data * (bn_model.weight.data / torch.sqrt(bn_model.running_var + eps)).view(-1,1,1,1).repeat(1, s[1], s[2], s[3])
        convert2cpu(weight).numpy().tofile(fp)
    else:
        bias = bn_model.bias.data - bn_model.running_mean * bn_model.weight.data / torch.sqrt(bn_model.running_var + eps)
        bias.numpy().tofile(fp)
        s = conv_model.weight.data.size()
        weight = conv_model.weight.data * (bn_model.weight.data / torch.sqrt(bn_model.running_var + eps)).view(-1,1,1,1).repeat(1, s[1], s[2], s[3])
        weight.numpy().tofile(fp)
transformer_net.py 文件源码 项目:fast-neural-style 作者: abhiskk 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def forward(self, x):
        n = x.size(2) * x.size(3)
        t = x.view(x.size(0), x.size(1), n)
        mean = torch.mean(t, 2).unsqueeze(2).unsqueeze(3).expand_as(x)
        # Calculate the biased var. torch.var returns unbiased var
        var = torch.var(t, 2).unsqueeze(2).unsqueeze(3).expand_as(x) * ((n - 1) / float(n))
        scale_broadcast = self.scale.unsqueeze(1).unsqueeze(1).unsqueeze(0)
        scale_broadcast = scale_broadcast.expand_as(x)
        shift_broadcast = self.shift.unsqueeze(1).unsqueeze(1).unsqueeze(0)
        shift_broadcast = shift_broadcast.expand_as(x)
        out = (x - mean) / torch.sqrt(var + self.eps)
        out = out * scale_broadcast + shift_broadcast
        return out
model_classes.py 文件源码 项目:e2e-model-learning 作者: locuslab 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def set_sig(self, X, Y):
        Y_pred = self.lin(X) + self.net(X)
        var = torch.mean((Y_pred-Y)**2, 0)
        self.sig.data = torch.sqrt(var).cuda().data
a3c_model.py 文件源码 项目:love-letter 作者: user01 项目源码 文件源码 阅读 47 收藏 0 点赞 0 评论 0
def normalized_columns_initializer(weights, std=1.0):
    out = torch.randn(weights.size())
    out *= std / torch.sqrt(out.pow(2).sum(1).expand_as(out))
    return out
lddmm_pytorch.py 文件源码 项目:lddmm-ot 作者: jeanfeydy 项目源码 文件源码 阅读 45 收藏 0 点赞 0 评论 0
def arclength_param(line) :
    "Arclength parametrisation of a piecewise affine curve."
    vel = line[1:, :] - line[:-1, :]
    vel = np.sqrt(np.sum( vel ** 2, 1 ))
    return np.hstack( ( [0], np.cumsum( vel, 0 ) ) )
lddmm_pytorch.py 文件源码 项目:lddmm-ot 作者: jeanfeydy 项目源码 文件源码 阅读 43 收藏 0 点赞 0 评论 0
def to_measure(self) :
        """
        Outputs the sum-of-diracs measure associated to the curve.
        Each segment from the connectivity matrix self.c
        is represented as a weighted dirac located at its center,
        with weight equal to the segment length.
        """
        segments = self.segments()
        centers = [         .5 * (  seg[0] + seg[1]      ) for seg in segments ]
        lengths = [np.sqrt(np.sum( (seg[1] - seg[0])**2 )) for seg in segments ]
        return ( np.array(centers), np.array(lengths) )
lddmm_pytorch.py 文件源码 项目:lddmm-ot 作者: jeanfeydy 项目源码 文件源码 阅读 48 收藏 0 点赞 0 评论 0
def _vertices_to_measure( q, connec ) :
        """
        Transforms a torch array 'q1' into a measure, assuming a connectivity matrix connec.
        It is the Torch equivalent of 'to_measure'.
        """
        a = q[connec[:,0]] ; b = q[connec[:,1]]
        # A curve is represented as a sum of diracs, one for each segment
        x  = .5 * (a + b)                     # Mean
        mu = torch.sqrt( ((b-a)**2).sum(1) )  # Length
        return (x, mu)
curve.py 文件源码 项目:lddmm-ot 作者: jeanfeydy 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def to_measure(self) :
        """
        Outputs the sum-of-diracs measure associated to the curve.
        Each segment from the connectivity matrix self.c
        is represented as a weighted dirac located at its center,
        with weight equal to the segment length.
        """
        segments = self.segments()
        centers = [         .5 * (  seg[0] + seg[1]      ) for seg in segments ]
        lengths = [np.sqrt(np.sum( (seg[1] - seg[0])**2 )) for seg in segments ]
        return ( np.array(centers), np.array(lengths) )
curve.py 文件源码 项目:lddmm-ot 作者: jeanfeydy 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def _vertices_to_measure( q, connec ) :
        """
        Transforms a torch array 'q1' into a measure, assuming a connectivity matrix connec.
        It is the Torch equivalent of 'to_measure'.
        """
        a = q[connec[:,0]] ; b = q[connec[:,1]]
        # A curve is represented as a sum of diracs, one for each segment
        x  = .5 * (a + b)                     # Mean
        mu = torch.sqrt( ((b-a)**2).sum(1) )  # Length
        return (x, mu)
test_torch.py 文件源码 项目:pytorch 作者: tylergenter 项目源码 文件源码 阅读 42 收藏 0 点赞 0 评论 0
def test_sqrt(self):
        self._testMath(torch.sqrt, lambda x: math.sqrt(x) if x > 0 else float('nan'))
test_torch.py 文件源码 项目:pytorch 作者: tylergenter 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_rsqrt(self):
        self._testMath(torch.rsqrt, lambda x: 1 / math.sqrt(x) if x > 0 else float('nan'))
normalize.py 文件源码 项目:benchmark 作者: pytorch 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def reset_parameters(self):
        std = 1.0 / math.sqrt(self.input_size)
        for w in self.parameters():
            w.data.uniform_(-std, std)
normalize.py 文件源码 项目:benchmark 作者: pytorch 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def forward(self, x):
        size = x.size()
        x = x.view(x.size(0), -1)
        x = (x - th.mean(x, 1).unsqueeze(1)) / th.sqrt(th.var(x, 1).unsqueeze(1) + self.epsilon)
        if self.learnable:
            x =  self.alpha.expand_as(x) * x + self.beta.expand_as(x)
        return x.view(size)
model.py 文件源码 项目:rl 作者: Shmuma 项目源码 文件源码 阅读 49 收藏 0 点赞 0 评论 0
def reset_parameters(self):
        std = math.sqrt(3 / self.in_features)
        nn.init.uniform(self.weight, -std, std)
        nn.init.uniform(self.bias, -std, std)
model.py 文件源码 项目:rl 作者: Shmuma 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__(self, in_features, out_features, sigma_zero=0.4, bias=True):
        super(NoisyFactorizedLinear, self).__init__(in_features, out_features, bias=bias)
        sigma_init = sigma_zero / math.sqrt(in_features)
        self.sigma_weight = nn.Parameter(torch.Tensor(out_features, in_features).fill_(sigma_init))
        self.register_buffer("epsilon_input", torch.zeros(1, in_features))
        self.register_buffer("epsilon_output", torch.zeros(out_features, 1))
        if bias:
            self.sigma_bias = nn.Parameter(torch.Tensor(out_features).fill_(sigma_init))
model.py 文件源码 项目:rl 作者: Shmuma 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def forward(self, input):
        torch.randn(self.epsilon_input.size(), out=self.epsilon_input)
        torch.randn(self.epsilon_output.size(), out=self.epsilon_output)

        func = lambda x: torch.sign(x) * torch.sqrt(torch.abs(x))
        eps_in = func(self.epsilon_input)
        eps_out = func(self.epsilon_output)

        bias = self.bias
        if bias is not None:
            bias = bias + self.sigma_bias * Variable(eps_out.t())
        noise_v = Variable(torch.mul(eps_in, eps_out))
        return F.linear(input, self.weight + self.sigma_weight * noise_v, bias)
gridgen.py 文件源码 项目:lr-gan.pytorch 作者: jwyang 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def forward(self, input1, input2):
        self.batchgrid3d = torch.zeros(torch.Size([input1.size(0)]) + self.grid3d.size())

        for i in range(input1.size(0)):
            self.batchgrid3d[i] = self.grid3d

        self.batchgrid3d = Variable(self.batchgrid3d)

        self.batchgrid = torch.zeros(torch.Size([input1.size(0)]) + self.grid.size())

        for i in range(input1.size(0)):
            self.batchgrid[i] = self.grid

        self.batchgrid = Variable(self.batchgrid)

        #print(self.batchgrid3d)

        x = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,0:4]), 3)
        y = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,4:8]), 3)
        z = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,8:]), 3)
        #print(x)
        r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5

        #print(r)
        theta = torch.acos(z/r)/(np.pi/2)  - 1
        #phi = torch.atan(y/x)
        phi = torch.atan(y/(x + 1e-5))  + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor))
        phi = phi/np.pi

        input_u = input2.view(-1,1,1,1).repeat(1,self.height, self.width,1)

        output = torch.cat([theta,phi], 3)

        output1 = torch.atan(torch.tan(np.pi/2.0*(output[:,:,:,1:2] + self.batchgrid[:,:,:,2:] * input_u[:,:,:,:])))  /(np.pi/2)
        output2 = torch.cat([output[:,:,:,0:1], output1], 3)

        return output2
gridgen.py 文件源码 项目:lr-gan.pytorch 作者: jwyang 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def forward(self, depth, trans0, trans1, rotate):
        self.batchgrid3d = torch.zeros(torch.Size([depth.size(0)]) + self.grid3d.size())

        for i in range(depth.size(0)):
            self.batchgrid3d[i] = self.grid3d

        self.batchgrid3d = Variable(self.batchgrid3d)

        self.batchgrid = torch.zeros(torch.Size([depth.size(0)]) + self.grid.size())

        for i in range(depth.size(0)):
            self.batchgrid[i] = self.grid

        self.batchgrid = Variable(self.batchgrid)

        x = self.batchgrid3d[:,:,:,0:1] * depth + trans0.view(-1,1,1,1).repeat(1, self.height, self.width, 1)

        y = self.batchgrid3d[:,:,:,1:2] * depth + trans1.view(-1,1,1,1).repeat(1, self.height, self.width, 1)
        z = self.batchgrid3d[:,:,:,2:3] * depth
        #print(x.size(), y.size(), z.size())
        r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5

        #print(r)
        theta = torch.acos(z/r)/(np.pi/2)  - 1
        #phi = torch.atan(y/x)
        phi = torch.atan(y/(x + 1e-5))  + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor))
        phi = phi/np.pi

        #print(theta.size(), phi.size())


        input_u = rotate.view(-1,1,1,1).repeat(1,self.height, self.width,1)

        output = torch.cat([theta,phi], 3)
        #print(output.size())

        output1 = torch.atan(torch.tan(np.pi/2.0*(output[:,:,:,1:2] + self.batchgrid[:,:,:,2:] * input_u[:,:,:,:])))  /(np.pi/2)
        output2 = torch.cat([output[:,:,:,0:1], output1], 3)

        return output2
test_torch.py 文件源码 项目:pytorch-coriander 作者: hughperkins 项目源码 文件源码 阅读 56 收藏 0 点赞 0 评论 0
def test_sqrt(self):
        self._testMath(torch.sqrt, lambda x: math.sqrt(x) if x > 0 else float('nan'))
test_torch.py 文件源码 项目:pytorch-coriander 作者: hughperkins 项目源码 文件源码 阅读 69 收藏 0 点赞 0 评论 0
def test_rsqrt(self):
        self._testMath(torch.rsqrt, lambda x: 1 / math.sqrt(x) if x > 0 else float('nan'))


问题


面经


文章

微信
公众号

扫码关注公众号