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)
python类sqrt()的实例源码
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
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
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
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 ) ) )
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) )
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)
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) )
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)
def test_sqrt(self):
self._testMath(torch.sqrt, lambda x: math.sqrt(x) if x > 0 else float('nan'))
def test_rsqrt(self):
self._testMath(torch.rsqrt, lambda x: 1 / math.sqrt(x) if x > 0 else float('nan'))
def reset_parameters(self):
std = 1.0 / math.sqrt(self.input_size)
for w in self.parameters():
w.data.uniform_(-std, std)
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)
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)
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))
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)
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
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
def test_sqrt(self):
self._testMath(torch.sqrt, lambda x: math.sqrt(x) if x > 0 else float('nan'))
def test_rsqrt(self):
self._testMath(torch.rsqrt, lambda x: 1 / math.sqrt(x) if x > 0 else float('nan'))