def get_individual_distance_loss(self, A_i, A_j, AB_i, AB_j,
B_i, B_j, BA_i, BA_j):
distance_in_A = self.distance(A_i, A_j)
distance_in_AB = self.distance(AB_i, AB_j)
distance_in_B = self.distance(B_i, B_j)
distance_in_BA = self.distance(BA_i, BA_j)
if self.normalize_distances:
distance_in_A = (distance_in_A - self.expectation_A) / self.std_A
distance_in_AB = (distance_in_AB - self.expectation_B) / self.std_B
distance_in_B = (distance_in_B - self.expectation_B) / self.std_B
distance_in_BA = (distance_in_BA - self.expectation_A) / self.std_A
return torch.abs(distance_in_A - distance_in_AB), torch.abs(distance_in_B - distance_in_BA)
python类abs()的实例源码
def distance(self, A, B):
return torch.abs(torch.mean(A) - torch.mean(B))
def get_individual_distance_loss(self, A_i, A_j, AB_i, AB_j, A_to_AB):
distance_in_A = self.distance(A_i, A_j)
distance_in_AB = self.distance(AB_i, AB_j)
if self.normalize_distances:
if A_to_AB:
distance_in_A = (distance_in_A - self.expectation_A) / self.std_A
distance_in_AB = (distance_in_AB - self.expectation_B) / self.std_B
else:
distance_in_A = (distance_in_A - self.expectation_B) / self.std_B
distance_in_AB = (distance_in_AB - self.expectation_A) / self.std_A
return torch.abs(distance_in_A - distance_in_AB)
def updateOutput(self, input):
assert input.dim() == 2
input_size = input.size()
self._output = self._output or input.new()
self.norm = self.norm or input.new()
self.buffer = self.buffer or input.new()
self._output.resize_as_(input)
# specialization for the infinity norm
if self.p == float('inf'):
if not self._indices:
self._indices = torch.cuda.FloatTensor() if torch.typename(self.output) == 'torch.cuda.FloatTensor' \
else torch.LongTensor()
torch.abs(self.buffer, input)
torch.max(self.norm, self._indices, self.buffer, 1)
self.norm.add_(self.eps)
else:
self.normp = self.normp or input.new()
if self.p % 2 != 0:
torch.abs(self.buffer, input).pow_(self.p)
else:
torch.pow(self.buffer, input, self.p)
torch.sum(self.normp, self.buffer, 1).add_(self.eps)
torch.pow(self.norm, self.normp, 1./self.p)
torch.div(self._output, input, self.norm.view(-1, 1).expand_as(input))
self.output = self._output.view(input_size)
return self.output
def check_jacobian(self, module, input, jacobian_input=True):
jacobian_parameters = bool(self._get_parameters(module)[0])
analytical = self._analytical_jacobian(module, input, jacobian_input, jacobian_parameters)
numerical = self._numerical_jacobian(module, input, jacobian_input, jacobian_parameters)
analytical_t = iter_tensors(analytical)
numerical_t = iter_tensors(numerical)
# TODO: compare structure
self.assertLessEqual(
max(a.add(-1, n).abs().max() for a, n in zip(analytical_t, numerical_t)),
PRECISION
)
def check_criterion_jacobian(self, criterion, input, target):
eps = 1e-6
self._forward_criterion(criterion, input, target)
analytical_d_x = self._backward_criterion(criterion, input, target)
numerical_d_x = deepcopy(analytical_d_x)
input_t = iter_tensors(input)
numerical_t = iter_tensors(numerical_d_x)
for x, d_x in zip(input_t, numerical_t):
x = x.view(-1)
d_x = d_x.view(-1)
for i in range(x.nelement()):
original = x[i]
x[i] = original + eps
fx1 = self._forward_criterion(criterion, input, target)
x[i] = original - eps
fx2 = self._forward_criterion(criterion, input, target)
deriv = (fx1 - fx2) / (2.*eps)
d_x[i] = deriv
x[i] = original
# TODO: check structure
analytical_t = iter_tensors(analytical_d_x)
numerical_t = iter_tensors(numerical_d_x)
self.assertLessEqual(
max(a.add(-1, n).abs().max() for a, n in zip(analytical_t, numerical_t)),
PRECISION
)
def forward(self, s1, l1, s2, l2):
p_s1 = self.Embd(s1)
p_s2 = self.Embd(s2)
s1_a_out = torch_util.auto_rnn_bilstm(self.lstm, p_s1, l1)
s2_a_out = torch_util.auto_rnn_bilstm(self.lstm, p_s2, l2)
s1_max_out = torch_util.max_along_time(s1_a_out, l1)
s2_max_out = torch_util.max_along_time(s2_a_out, l2)
features = torch.cat([s1_max_out, s2_max_out, torch.abs(s1_max_out - s2_max_out), s1_max_out * s2_max_out], dim=1)
out = self.classifier(features)
return out
def test_scale(self):
audio_orig = self.sig.clone()
result = transforms.Scale()(audio_orig)
self.assertTrue(result.min() >= -1. and result.max() <= 1.,
print("min: {}, max: {}".format(result.min(), result.max())))
maxminmax = np.abs(
[audio_orig.min(), audio_orig.max()]).max().astype(np.float)
result = transforms.Scale(factor=maxminmax)(audio_orig)
self.assertTrue((result.min() == -1. or result.max() == 1.) and
result.min() >= -1. and result.max() <= 1.,
print("min: {}, max: {}".format(result.min(), result.max())))
def test_compose(self):
audio_orig = self.sig.clone()
length_orig = audio_orig.size(0)
length_new = int(length_orig * 1.2)
maxminmax = np.abs(
[audio_orig.min(), audio_orig.max()]).max().astype(np.float)
tset = (transforms.Scale(factor=maxminmax),
transforms.PadTrim(max_len=length_new))
result = transforms.Compose(tset)(audio_orig)
self.assertTrue(np.abs([result.min(), result.max()]).max() == 1.)
self.assertTrue(result.size(0) == length_new)
def test_nested_map_data():
means = [Variable(torch.randn(2)) for i in range(8)]
mean_batch_size = 2
stds = [Variable(torch.abs(torch.randn(2))) for i in range(6)]
std_batch_size = 3
def model(means, stds):
return pyro.map_data("a", means,
lambda i, x:
pyro.map_data("a_{}".format(i), stds,
lambda j, y:
pyro.sample("x_{}{}".format(i, j),
dist.normal, x, y),
batch_size=std_batch_size),
batch_size=mean_batch_size)
model = model
xs = model(means, stds)
assert len(xs) == mean_batch_size
assert len(xs[0]) == std_batch_size
tr = poutine.trace(model).get_trace(means, stds)
for name in tr.nodes.keys():
if tr.nodes[name]["type"] == "sample" and name.startswith("x_"):
assert tr.nodes[name]["scale"] == 4.0 * 2.0
def _algo_2_vert_comp(self, sent1_block_a, sent2_block_a, sent1_block_b, sent2_block_b):
comparison_feats = []
ws_no_inf = [w for w in self.filter_widths if not np.isinf(w)]
for pool in ('max', 'min', 'mean'):
for ws1 in self.filter_widths:
x1 = sent1_block_a[ws1][pool]
batch_size = x1.size()[0]
for ws2 in self.filter_widths:
x2 = sent2_block_a[ws2][pool]
if (not np.isinf(ws1) and not np.isinf(ws2)) or (np.isinf(ws1) and np.isinf(ws2)):
comparison_feats.append(F.cosine_similarity(x1, x2).contiguous().view(batch_size, 1))
comparison_feats.append(F.pairwise_distance(x1, x2))
comparison_feats.append(torch.abs(x1 - x2))
for pool in ('max', 'min'):
for ws in ws_no_inf:
oG_1B = sent1_block_b[ws][pool]
oG_2B = sent2_block_b[ws][pool]
for i in range(0, self.n_per_dim_filters):
x1 = oG_1B[:, :, i]
x2 = oG_2B[:, :, i]
batch_size = x1.size()[0]
comparison_feats.append(F.cosine_similarity(x1, x2).contiguous().view(batch_size, 1))
comparison_feats.append(F.pairwise_distance(x1, x2))
comparison_feats.append(torch.abs(x1 - x2))
return torch.cat(comparison_feats, dim=1)
def l1_loss(x, y):
return torch.abs(x - y).mean()
babi_main.py 文件源码
项目:Dynamic-memory-networks-plus-Pytorch
作者: dandelin
项目源码
文件源码
阅读 42
收藏 0
点赞 0
评论 0
def make_interaction(self, facts, questions, prevM):
'''
facts.size() -> (#batch, #sentence, #hidden = #embedding)
questions.size() -> (#batch, 1, #hidden)
prevM.size() -> (#batch, #sentence = 1, #hidden = #embedding)
z.size() -> (#batch, #sentence, 4 x #embedding)
G.size() -> (#batch, #sentence)
'''
batch_num, sen_num, embedding_size = facts.size()
questions = questions.expand_as(facts)
prevM = prevM.expand_as(facts)
z = torch.cat([
facts * questions,
facts * prevM,
torch.abs(facts - questions),
torch.abs(facts - prevM)
], dim=2)
z = z.view(-1, 4 * embedding_size)
G = F.tanh(self.z1(z))
G = self.z2(G)
G = G.view(batch_num, -1)
G = F.softmax(G)
return G
def preProc1(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()).fill_(-1)).type_as(x)
absX = torch.abs(x)
cond1 = torch.gt(absX, negExpP)
if (torch.sum(cond1) > 0).data.all():
x1 = torch.log(torch.abs(x[cond1]))/P
z[cond1] = x1
return z
def l1_dist(x, y, keepdim=True):
d = torch.abs(x - y)
return reduce_sum(d, keepdim=keepdim)
def l1_norm(x, keepdim=True):
return reduce_sum(x.abs(), keepdim=keepdim)
def loss(self, x, samples):
_, proposal_output = self.forward(x, samples)
batch_size = len(samples)
locations = proposal_output[:, 0]
scales = proposal_output[:, 1] + util.epsilon
log_two_scales = torch.log(2 * scales)
l = 0
for b in range(batch_size):
value = samples[b].value[0]
location = locations[b]
scale = scales[b]
l += log_two_scales[b] + torch.abs(value - location) / scale
return l
def pairwise_distance(x1, x2, p=2, eps=1e-6):
r"""
Computes the batchwise pairwise distance between vectors v1,v2:
.. math ::
\Vert x \Vert _p := \left( \sum_{i=1}^n \vert x_i \vert ^ p \right) ^ {1/p}
Args:
x1: first input tensor
x2: second input tensor
p: the norm degree. Default: 2
Shape:
- Input: :math:`(N, D)` where `D = vector dimension`
- Output: :math:`(N, 1)`
>>> input1 = autograd.Variable(torch.randn(100, 128))
>>> input2 = autograd.Variable(torch.randn(100, 128))
>>> output = F.pairwise_distance(input1, input2, p=2)
>>> output.backward()
"""
assert x1.size() == x2.size(), "Input sizes must be equal."
assert x1.dim() == 2, "Input must be a 2D matrix."
diff = torch.abs(x1 - x2)
out = torch.pow(diff + eps, p).sum(dim=1)
return torch.pow(out, 1. / p)
def updateOutput(self, input):
assert input.dim() == 2
input_size = input.size()
if self._output is None:
self._output = input.new()
if self.norm is None:
self.norm = input.new()
if self.buffer is None:
self.buffer = input.new()
self._output.resize_as_(input)
# specialization for the infinity norm
if self.p == float('inf'):
if not self._indices:
self._indices = torch.cuda.FloatTensor() if torch.typename(self.output) == 'torch.cuda.FloatTensor' \
else torch.LongTensor()
torch.abs(input, out=self.buffer)
torch.max(self._indices, self.buffer, 1, out=self.norm)
self.norm.add_(self.eps)
else:
if self.normp is None:
self.normp = input.new()
if self.p % 2 != 0:
torch.abs(input, out=self.buffer).pow_(self.p)
else:
torch.pow(input, self.p, out=self.buffer)
torch.sum(self.buffer, 1, out=self.normp).add_(self.eps)
torch.pow(self.normp, 1. / self.p, out=self.norm)
torch.div(input, self.norm.view(-1, 1).expand_as(input), out=self._output)
self.output = self._output.view(input_size)
return self.output
def test_zero_grad(self):
i = Variable(torch.randn(2, 5), requires_grad=True)
module = nn.Linear(5, 5)
for p in module.parameters():
p.requires_grad = False
module.zero_grad()
module.weight.requires_grad = True
module.zero_grad()
self.assertIsNone(module.weight.grad) # uninitialized grad
module(i).sum().backward()
self.assertIsNotNone(module.weight.grad)
self.assertGreater(module.weight.grad.data.abs().sum(), 0)
module.zero_grad()
self.assertEqual(module.weight.grad.data, module.weight.data.clone().zero_())
module.bias.requires_grad = True
module.zero_grad()
self.assertIsNotNone(module.weight.grad)
self.assertIsNone(module.bias.grad)
module(i).sum().backward()
self.assertIsNotNone(module.weight.grad)
self.assertIsNotNone(module.bias.grad)
self.assertGreater(module.weight.grad.data.abs().sum(), 0)
self.assertGreater(module.bias.grad.data.abs().sum(), 0)
module.zero_grad()
self.assertEqual(module.weight.grad.data, module.weight.data.clone().zero_())
self.assertEqual(module.bias.grad.data, module.bias.data.clone().zero_())