def __init__(self, mode, anchors=9, classes=80, depth=4,
base_activation=F.relu,
output_activation=F.sigmoid):
super(SubNet, self).__init__()
self.anchors = anchors
self.classes = classes
self.depth = depth
self.base_activation = base_activation
self.output_activation = output_activation
self.subnet_base = nn.ModuleList([conv3x3(256, 256, padding=1)
for _ in range(depth)])
if mode == 'boxes':
self.subnet_output = conv3x3(256, 4 * self.anchors, padding=1)
elif mode == 'classes':
# add an extra dim for confidence
self.subnet_output = conv3x3(256, (1 + self.classes) * self.anchors, padding=1)
self._output_layer_init(self.subnet_output.bias.data)
python类add()的实例源码
def test_csub(self):
# with a tensor
a = torch.randn(100,90)
b = a.clone().normal_()
res_add = torch.add(a, -1, b)
res_csub = a.clone()
res_csub.sub_(b)
self.assertEqual(res_add, res_csub)
# with a scalar
a = torch.randn(100,100)
scalar = 123.5
res_add = torch.add(a, -scalar)
res_csub = a.clone()
res_csub.sub_(scalar)
self.assertEqual(res_add, res_csub)
def assertIsOrdered(self, order, x, mxx, ixx, task):
SIZE = 4
if order == 'descending':
check_order = lambda a, b: a >= b
elif order == 'ascending':
check_order = lambda a, b: a <= b
else:
error('unknown order "{}", must be "ascending" or "descending"'.format(order))
are_ordered = True
for j, k in product(range(SIZE), range(1, SIZE)):
self.assertTrue(check_order(mxx[j][k-1], mxx[j][k]),
'torch.sort ({}) values unordered for {}'.format(order, task))
seen = set()
indicesCorrect = True
size = x.size(x.dim()-1)
for k in range(size):
seen.clear()
for j in range(size):
self.assertEqual(x[k][ixx[k][j]], mxx[k][j],
'torch.sort ({}) indices wrong for {}'.format(order, task))
seen.add(ixx[k][j])
self.assertEqual(len(seen), size)
def test_abs(self):
size = 1000
max_val = 1000
original = torch.rand(size).mul(max_val)
# Tensor filled with values from {-1, 1}
switch = torch.rand(size).mul(2).floor().mul(2).add(-1)
types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor']
for t in types:
data = original.type(t)
switch = switch.type(t)
res = torch.mul(data, switch)
self.assertEqual(res.abs(), data, 1e-16)
# Checking that the right abs function is called for LongTensor
bignumber = 2^31 + 1
res = torch.LongTensor((-bignumber,))
self.assertGreater(res.abs()[0], 0)
def forward(self, x):
if not self.active:
self.eval()
if not self.equalInOut:
x = self.relu1(self.bn1(x))
else:
out = self.relu1(self.bn1(x))
out = self.relu2(self.bn2(self.conv1(out if self.equalInOut else x)))
if self.droprate > 0:
out = F.dropout(out, p=self.droprate, training=self.training)
out = self.conv2(out)
out = torch.add(x if self.equalInOut else self.convShortcut(x), out)
if self.active:
return out
else:
return out.detach()
# note: we call it DenseNet for simple compatibility with the training code.
# similar we call it growthRate instead of widen_factor
def _test_spadd_shape(self, shape_i, shape_v=None):
shape = shape_i + (shape_v or [])
x, _, _ = self._gen_sparse(len(shape_i), 10, shape)
y = self.randn(*shape)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * x.to_dense()
self.assertEqual(res, expected)
# Non contiguous dense tensor
s = list(shape)
s[0] = shape[-1]
s[-1] = shape[0]
y = self.randn(*s)
y.transpose_(0, len(s) - 1)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * x.to_dense()
self.assertEqual(res, expected)
def test_abs(self):
size = 1000
max_val = 1000
original = torch.rand(size).mul(max_val)
# Tensor filled with values from {-1, 1}
switch = torch.rand(size).mul(2).floor().mul(2).add(-1)
types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor']
for t in types:
data = original.type(t)
switch = switch.type(t)
res = torch.mul(data, switch)
# abs is used in assertEqual so we use the slow version instead
self.assertTensorsSlowEqual(res.abs(), data, 1e-16)
# Checking that the right abs function is called for LongTensor
bignumber = 2 ^ 31 + 1
res = torch.LongTensor((-bignumber,))
self.assertGreater(res.abs()[0], 0)
def forward(self, x):
upblock = True
# Downsizing layer - Large Kernel ensures large receptive field on the residual blocks
h = F.relu(self.b2(self.c1(x)))
# Residual Layers
for r in self.rs:
h = r(h) # will go through all residual blocks in this loop
if upblock:
# Upsampling Layers - improvement suggested by [2] to remove "checkerboard pattern"
for u in self.up:
h = u(h) # will go through all upsampling blocks in this loop
else:
# As recommended by [1]
h = F.relu(self.bc2(self.dc2(h)))
h = F.relu(self.bc3(self.dc3(h)))
# Last layer and scaled tanh activation - Scaled from 0 to 1 instead of 0 - 255
h = F.tanh(self.c3(h))
h = torch.add(h, 1.)
h = torch.mul(h, 0.5)
return h
def _test_spadd_shape(self, shape_i, shape_v=None):
shape = shape_i + (shape_v or [])
x, _, _ = self._gen_sparse(len(shape_i), 10, shape)
y = self.randn(*shape)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * x.to_dense()
self.assertEqual(res, expected)
# Non contiguous dense tensor
s = list(shape)
s[0] = shape[-1]
s[-1] = shape[0]
y = self.randn(*s)
y.transpose_(0, len(s) - 1)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * x.to_dense()
self.assertEqual(res, expected)
def test_abs(self):
size = 1000
max_val = 1000
original = torch.rand(size).mul(max_val)
# Tensor filled with values from {-1, 1}
switch = torch.rand(size).mul(2).floor().mul(2).add(-1)
types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor']
for t in types:
data = original.type(t)
switch = switch.type(t)
res = torch.mul(data, switch)
# abs is used in assertEqual so we use the slow version instead
self.assertTensorsSlowEqual(res.abs(), data, 1e-16)
# Checking that the right abs function is called for LongTensor
bignumber = 2 ^ 31 + 1
res = torch.LongTensor((-bignumber,))
self.assertGreater(res.abs()[0], 0)
def _test_spadd_shape(self, shape_i, shape_v=None):
shape = shape_i + (shape_v or [])
x, _, _ = self._gen_sparse(len(shape_i), 10, shape)
y = self.randn(*shape)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * x.to_dense()
self.assertEqual(res, expected)
# Non contiguous dense tensor
s = list(shape)
s[0] = shape[-1]
s[-1] = shape[0]
y = self.randn(*s)
y.transpose_(0, len(s) - 1)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * x.to_dense()
self.assertEqual(res, expected)
def _test_neg(self, cast):
float_types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor']
int_types = ['torch.IntTensor', 'torch.ShortTensor']
for t in float_types + int_types:
if t in float_types:
a = cast(torch.randn(100, 90).type(t))
else:
a = cast(torch.Tensor(100, 90).type(t).random_())
zeros = cast(torch.Tensor().type(t)).resize_as_(a).zero_()
res_add = torch.add(zeros, -1, a)
res_neg = a.clone()
res_neg.neg_()
self.assertEqual(res_neg, res_add)
# test out of place as well
res_neg_out_place = a.clone().neg()
self.assertEqual(res_neg_out_place, res_add)
# test via __neg__ operator
res_neg_op = -a.clone()
self.assertEqual(res_neg_op, res_add)
def test_abs(self):
size = 1000
max_val = 1000
original = torch.rand(size).mul(max_val)
# Tensor filled with values from {-1, 1}
switch = torch.rand(size).mul(2).floor().mul(2).add(-1)
types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor']
for t in types:
data = original.type(t)
switch = switch.type(t)
res = torch.mul(data, switch)
# abs is used in assertEqual so we use the slow version instead
self.assertTensorsSlowEqual(res.abs(), data, 1e-16)
# Checking that the right abs function is called for LongTensor
bignumber = 2 ^ 31 + 1
res = torch.LongTensor((-bignumber,))
self.assertGreater(res.abs()[0], 0)
def train(epoch):
for e_ in range(epoch):
if (e_ + 1) % 10 == 0:
adjust_learning_rate(optimizer, e_)
cnt = 0
loss = Variable(torch.Tensor([0]))
for i_q, i_k, i_v, i_cand, i_a in zip(train_q, train_key,train_value, train_cand, train_a):
cnt += 1
i_q = i_q.unsqueeze(0) # add dimension
probs = model.forward(i_q, i_k, i_v,i_cand)
i_a = Variable(i_a)
curr_loss = loss_function(probs, i_a)
loss = torch.add(loss, torch.div(curr_loss, config.batch_size))
# naive batch implemetation, the lr is divided by batch size
if cnt % config.batch_size == 0:
print "Training loss", loss.data.sum()
loss.backward()
optimizer.step()
loss = Variable(torch.Tensor([0]))
model.zero_grad()
if cnt % config.valid_every == 0:
print "Accuracy:",eval()
def forward(self, qu, w, cand):
qu = Variable(qu)
w = Variable(w)
cand = Variable(cand)
embed_q = self.embed_B(qu)
embed_w1 = self.embed_A(w)
embed_w2 = self.embed_C(w)
embed_c = self.embed_C(cand)
#pdb.set_trace()
q_state = torch.sum(embed_q, 1).squeeze(1)
w1_state = torch.sum(embed_w1, 1).squeeze(1)
w2_state = torch.sum(embed_w2, 1).squeeze(1)
for _ in range(self.config.hop):
sent_dot = torch.mm(q_state, torch.transpose(w1_state, 0, 1))
sent_att = F.softmax(sent_dot)
a_dot = torch.mm(sent_att, w2_state)
a_dot = self.H(a_dot)
q_state = torch.add(a_dot, q_state)
f_feat = torch.mm(q_state, torch.transpose(embed_c, 0, 1))
score = F.log_softmax(f_feat)
return score
def train(epoch):
for e_ in range(epoch):
if (e_ + 1) % 10 == 0:
adjust_learning_rate(optimizer, e_)
cnt = 0
loss = Variable(torch.Tensor([0]))
for i_q, i_w, i_e_p, i_a in zip(train_q, train_w, train_e_p, train_a):
cnt += 1
i_q = i_q.unsqueeze(0) # add dimension
probs = model.forward(i_q, i_w, i_e_p)
i_a = Variable(i_a)
curr_loss = loss_function(probs, i_a)
loss = torch.add(loss, torch.div(curr_loss, config.batch_size))
# naive batch implemetation, the lr is divided by batch size
if cnt % config.batch_size == 0:
print "Training loss", loss.data.sum()
loss.backward()
optimizer.step()
loss = Variable(torch.Tensor([0]))
model.zero_grad()
if cnt % config.valid_every == 0:
print "Accuracy:",eval()
def _test_spadd_shape(self, shape_i, shape_v=None):
shape = shape_i + (shape_v or [])
x, _, _ = self._gen_sparse(len(shape_i), 10, shape)
y = self.randn(*shape)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * self.safeToDense(x)
self.assertEqual(res, expected)
# Non contiguous dense tensor
s = list(shape)
s[0] = shape[-1]
s[-1] = shape[0]
y = self.randn(*s)
y.transpose_(0, len(s) - 1)
r = random.random()
res = torch.add(y, r, x)
expected = y + r * self.safeToDense(x)
self.assertEqual(res, expected)
def _test_neg(self, cast):
float_types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor']
int_types = ['torch.IntTensor', 'torch.ShortTensor', 'torch.ByteTensor',
'torch.CharTensor']
for t in float_types + int_types:
if t in float_types:
a = cast(torch.randn(100, 90).type(t))
else:
a = cast(torch.Tensor(100, 90).type(t).random_())
zeros = cast(torch.Tensor().type(t)).resize_as_(a).zero_()
res_add = torch.add(zeros, -1, a)
res_neg = a.clone()
res_neg.neg_()
self.assertEqual(res_neg, res_add)
# test out of place as well
res_neg_out_place = a.clone().neg()
self.assertEqual(res_neg_out_place, res_add)
# test via __neg__ operator
res_neg_op = -a.clone()
self.assertEqual(res_neg_op, res_add)
def test_abs(self):
size = 1000
max_val = 1000
original = torch.rand(size).mul(max_val)
# Tensor filled with values from {-1, 1}
switch = torch.rand(size).mul(2).floor().mul(2).add(-1)
types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor']
for t in types:
data = original.type(t)
switch = switch.type(t)
res = torch.mul(data, switch)
# abs is used in assertEqual so we use the slow version instead
self.assertTensorsSlowEqual(res.abs(), data, 1e-16)
# Checking that the right abs function is called for LongTensor
bignumber = 2 ^ 31 + 1
res = torch.LongTensor((-bignumber,))
self.assertGreater(res.abs()[0], 0)
def mix(w: T.FloatingPoint,
x: T.FloatTensor,
y: T.FloatTensor) -> None:
"""
Compute a weighted average of two matrices (x and y) and return the result.
Multilinear interpolation.
Note:
Modifies x in place.
Args:
w: The mixing coefficient (float or tensor) between 0 and 1.
x: A tensor.
y: A tensor:
Returns:
tensor = w * x + (1-w) * y
"""
return torch.add(torch.mul(x, w), torch.mul(1-w, y))
def pdist(x: T.FloatTensor, y: T.FloatTensor) -> T.FloatTensor:
"""
Compute the pairwise distance matrix between the rows of x and y.
Args:
x (tensor (num_samples_1, num_units))
y (tensor (num_samples_2, num_units))
Returns:
tensor (num_samples_1, num_samples_2)
"""
inner = dot(x, transpose(y))
x_mag = norm(x, axis=1) ** 2
y_mag = norm(y, axis=1) ** 2
squared = add(unsqueeze(y_mag, axis=0), add(unsqueeze(x_mag, axis=1), -2*inner))
return torch.sqrt(clip(squared, a_min=0))
def forward(self, x):
# don't need resnet_feature_2 as it is too large
_, resnet_feature_3, resnet_feature_4, resnet_feature_5 = self.resnet(x)
pyramid_feature_6 = self.pyramid_transformation_6(resnet_feature_5)
pyramid_feature_7 = self.pyramid_transformation_7(F.relu(pyramid_feature_6))
pyramid_feature_5 = self.pyramid_transformation_5(resnet_feature_5)
pyramid_feature_4 = self.pyramid_transformation_4(resnet_feature_4)
upsampled_feature_5 = self._upsample(pyramid_feature_5, pyramid_feature_4)
pyramid_feature_4 = self.upsample_transform_1(
torch.add(upsampled_feature_5, pyramid_feature_4)
)
pyramid_feature_3 = self.pyramid_transformation_3(resnet_feature_3)
upsampled_feature_4 = self._upsample(pyramid_feature_4, pyramid_feature_3)
pyramid_feature_3 = self.upsample_transform_2(
torch.add(upsampled_feature_4, pyramid_feature_3)
)
return (pyramid_feature_3,
pyramid_feature_4,
pyramid_feature_5,
pyramid_feature_6,
pyramid_feature_7)
def updateOutput(self, input, target):
# - log(input) * target - log(1 - input) * (1 - target)
if input.nelement() != target.nelement():
raise RuntimeError("input and target size mismatch")
self.buffer = self.buffer or input.new()
buffer = self.buffer
weights = self.weights
buffer.resize_as_(input)
if weights is not None and target.dim() != 1:
weights = self.weights.view(1, target.size(1)).expand_as(target)
# log(input) * target
torch.add(buffer, input, self.eps).log_()
if weights is not None:
buffer.mul_(weights)
output = torch.dot(target, buffer)
# log(1 - input) * (1 - target)
torch.mul(buffer, input, -1).add_(1+self.eps).log_()
if weights is not None:
buffer.mul_(weights)
output = output + torch.sum(buffer)
output = output - torch.dot(target, buffer)
if self.sizeAverage:
output = output / input.nelement()
self.output = - output
return self.output
def updateGradInput(self, input, target):
# - (target - input) / ( input (1 - input) )
# The gradient is slightly incorrect:
# It should have be divided by (input + self.eps) (1 - input + self.eps)
# but it is divided by input (1 - input + self.eps) + self.eps
# This modification requires less memory to be computed.
if input.nelement() != target.nelement():
raise RuntimeError("input and target size mismatch")
self.buffer = self.buffer or input.new()
buffer = self.buffer
weights = self.weights
gradInput = self.gradInput
if weights is not None and target.dim() != 1:
weights = self.weights.view(1, target.size(1)).expand_as(target)
buffer.resize_as_(input)
# - x ( 1 + self.eps -x ) + self.eps
torch.add(buffer, input, -1).add_(-self.eps).mul_(input).add_(-self.eps)
gradInput.resize_as_(input)
# y - x
torch.add(gradInput, target, -1, input)
# - (y - x) / ( x ( 1 + self.eps -x ) + self.eps )
gradInput.div_(buffer)
if weights is not None:
gradInput.mul_(weights)
if self.sizeAverage:
gradInput.div_(target.nelement())
return gradInput
def updateOutput(self, input):
self.output.resize_(1)
assert input[0].dim() == 2
self.diff = self.diff or input[0].new()
torch.add(self.diff, input[0], -1, input[1]).abs_()
self.output.resize_(input[0].size(0))
self.output.zero_()
self.output.add_(self.diff.pow_(self.norm).sum(1))
self.output.pow_(1./self.norm)
return self.output
def test_clamp(self):
m1 = torch.rand(100).mul(5).add(-2.5) # uniform in [-2.5, 2.5]
# just in case we're extremely lucky.
min_val = -1
max_val = 1
m1[1] = min_val
m1[2] = max_val
res1 = m1.clone()
res1.clamp_(min_val, max_val)
res2 = m1.clone()
for i in iter_indices(res2):
res2[i] = max(min_val, min(max_val, res2[i]))
self.assertEqual(res1, res2)
def test_xcorr3_xcorr2_eq(self):
def reference(x, k, o3, o32):
for i in range(o3.size(1)):
for j in range(k.size(1)):
o32[i].add(torch.xcorr2(x[i+j-1], k[j]))
self._test_conv_corr_eq(lambda x, k: torch.xcorr3(x, k), reference)
def test_xcorr3_xcorr2_eq(self):
def reference(x, k, o3, o32):
for i in range(x.size(1)):
for j in range(k.size(1)):
o32[i].add(torch.xcorr2(x[i], k[k.size(1) - j + 1], 'F'))
self._test_conv_corr_eq(lambda x, k: torch.xcorr3(x, k, 'F'), reference)
def test_conv3_conv2_eq(self):
def reference(x, k, o3, o32):
for i in range(o3.size(1)):
for j in range(k.size(1)):
o32[i].add(torch.conv2(x[i+j-1], k[k.size(1)-j+1]))
self._test_conv_corr_eq(lambda x, k: torch.conv3(x, k), reference)
def test_pstrf(self):
def checkPsdCholesky(a, uplo, inplace):
if inplace:
u = torch.Tensor(a.size())
piv = torch.IntTensor(a.size(0))
args = [u, piv, a]
else:
args = [a]
if uplo is not None:
args += [uplo]
u, piv = torch.pstrf(*args)
if uplo is False:
a_reconstructed = torch.mm(u, u.t())
else:
a_reconstructed = torch.mm(u.t(), u)
piv = piv.long()
a_permuted = a.index_select(0, piv).index_select(1, piv)
self.assertEqual(a_permuted, a_reconstructed, 1e-14)
dimensions = ((5, 1), (5, 3), (5, 5), (10, 10))
for dim in dimensions:
m = torch.Tensor(*dim).uniform_()
a = torch.mm(m, m.t())
# add a small number to the diagonal to make the matrix numerically positive semidefinite
for i in range(m.size(0)):
a[i][i] = a[i][i] + 1e-7
for inplace in (True, False):
for uplo in (None, True, False):
checkPsdCholesky(a, uplo, inplace)