本文整理匯總了Python中torch.range方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.range方法的具體用法?Python torch.range怎麽用?Python torch.range使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.range方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: dynamic_evaluate
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def dynamic_evaluate(model, test_loader, val_loader, args):
tester = Tester(model, args)
if os.path.exists(os.path.join(args.save, 'logits_single.pth')):
val_pred, val_target, test_pred, test_target = \
torch.load(os.path.join(args.save, 'logits_single.pth'))
else:
val_pred, val_target = tester.calc_logit(val_loader)
test_pred, test_target = tester.calc_logit(test_loader)
torch.save((val_pred, val_target, test_pred, test_target),
os.path.join(args.save, 'logits_single.pth'))
flops = torch.load(os.path.join(args.save, 'flops.pth'))
with open(os.path.join(args.save, 'dynamic.txt'), 'w') as fout:
for p in range(1, 40):
print("*********************")
_p = torch.FloatTensor(1).fill_(p * 1.0 / 20)
probs = torch.exp(torch.log(_p) * torch.range(1, args.nBlocks))
probs /= probs.sum()
acc_val, _, T = tester.dynamic_eval_find_threshold(
val_pred, val_target, probs, flops)
acc_test, exp_flops = tester.dynamic_eval_with_threshold(
test_pred, test_target, flops, T)
print('valid acc: {:.3f}, test acc: {:.3f}, test flops: {:.2f}M'.format(acc_val, acc_test, exp_flops / 1e6))
fout.write('{}\t{}\n'.format(acc_test, exp_flops.item()))
示例2: dynamic_eval_with_threshold
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def dynamic_eval_with_threshold(self, logits, targets, flops, T):
n_stage, n_sample, _ = logits.size()
max_preds, argmax_preds = logits.max(dim=2, keepdim=False) # take the max logits as confidence
acc_rec, exp = torch.zeros(n_stage), torch.zeros(n_stage)
acc, expected_flops = 0, 0
for i in range(n_sample):
gold_label = targets[i]
for k in range(n_stage):
if max_preds[k][i].item() >= T[k]: # force to exit at k
_g = int(gold_label.item())
_pred = int(argmax_preds[k][i].item())
if _g == _pred:
acc += 1
acc_rec[k] += 1
exp[k] += 1
break
acc_all, sample_all = 0, 0
for k in range(n_stage):
_t = exp[k] * 1.0 / n_sample
sample_all += exp[k]
expected_flops += _t * flops[k]
acc_all += acc_rec[k]
return acc * 100.0 / n_sample, expected_flops
示例3: testClassErrorMeter
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def testClassErrorMeter(self):
mtr = meter.ClassErrorMeter(topk=[1])
output = torch.eye(3)
if hasattr(torch, "arange"):
target = torch.arange(0, 3)
else:
target = torch.range(0, 2)
mtr.add(output, target)
err = mtr.value()
self.assertEqual(err, [0], "All should be correct")
target[0] = 1
target[1] = 0
target[2] = 0
mtr.add(output, target)
err = mtr.value()
self.assertEqual(err, [50.0], "Half should be correct")
示例4: ScoreRANSAC
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def ScoreRANSAC(match1, match2, matchSetT, sampleIndex, score, tolerance, nbSamplePoint, paramEstimate) :
#All The Data
nbMatch = len(matchSetT)
sampleIndex = np.array(matchSetT)[sampleIndex]
X = match1[sampleIndex]
Y = match2[sampleIndex]
H21 = paramEstimate(X, Y)
error = Prediction(match1, match2, H21)
isInlier = error < tolerance
inlier = {}
for i in range(len(match1)) :
if isInlier[i] :
score_i = score[i] * np.exp(-1 * error[i] ** 2 / tolerance ** 2)
#score_i = score[i]
key = (match1[i][0], match1[i][1])
if inlier.has_key(key) and inlier[key][1] < score_i :
inlier[key] = [match2[i], score_i]
elif not inlier.has_key(key) :
inlier[key] = [match2[i], score_i]
return H21, sum([item[1] for item in inlier.values()]), matchSetT
示例5: GetCC
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def GetCC(match1, match2, mask1, mask2, score) :
match1_arr = np.array(match1).astype(int)
mask1[match1_arr[:, 0].flatten(), match1_arr[:, 1].flatten()] = 1
label1, nb_label1 = measure.label(mask1, connectivity=2, return_num=True)
dict_match = dict(zip(match1, match2))
dict_score = dict(zip(match1, score))
CC = []
CC_score = np.zeros(nb_label1)
for i in range(1, nb_label1 + 1) :
CC.append({})
posx, posy = np.where(label1 == i)
tmp_match1 = [(posx[j], posy[j]) for j in range(len(posx))]
tmp_match2 = [dict_match[item] for item in tmp_match1]
CC[i-1] = dict(zip(tmp_match1, tmp_match2))
CC[i-1]['mask2shape'] = mask2.shape
CC_score[i -1 ] = sum(dict_score[item] for item in tmp_match1)
return CC, CC_score
示例6: get_points
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def get_points(self, featmap_sizes, dtype, device):
"""Get points according to feature map sizes.
Args:
featmap_sizes (list[tuple]): Multi-level feature map sizes.
dtype (torch.dtype): Type of points.
device (torch.device): Device of points.
Returns:
tuple: points of each image.
"""
mlvl_points = []
for i in range(len(featmap_sizes)):
mlvl_points.append(
self.get_points_single(featmap_sizes[i], self.strides[i],
dtype, device))
return mlvl_points
示例7: test_forward_backward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def test_forward_backward(self):
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from reid.loss import OIMLoss
criterion = OIMLoss(3, 3, scalar=1.0, size_average=False)
criterion.lut = torch.eye(3)
x = Variable(torch.randn(3, 3), requires_grad=True)
y = Variable(torch.range(0, 2).long())
loss = criterion(x, y)
loss.backward()
probs = F.softmax(x)
grads = probs.data - torch.eye(3)
abs_diff = torch.abs(grads - x.grad.data)
self.assertEquals(torch.log(probs).diag().sum(), -loss)
self.assertTrue(torch.max(abs_diff) < 1e-6)
示例8: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def __init__(self, num_features, num_classes, hidden_size, num_layers, dropout, sample_size):
super(ASGCN, self).__init__()
self.num_features = num_features
self.num_classes = num_classes
self.hidden_size = hidden_size
self.num_layers = num_layers
self.dropout = dropout
self.sample_size = sample_size
self.w_s0 = Parameter(torch.FloatTensor(num_features))
self.w_s1 = Parameter(torch.FloatTensor(num_features))
shapes = [num_features] + [hidden_size] * (num_layers - 1) + [num_classes]
self.convs = nn.ModuleList(
[
GraphConvolution(shapes[layer], shapes[layer + 1])
for layer in range(num_layers)
]
)
self.reset_parameters()
示例9: add_conj
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def add_conj(fact):
l1 = fact[3].size()[0]
l2 = fact[3].size()[1]
index1_ = torch.cat((fact[2], torch.range(0, l1 - 1).long()), 0)
index2_ = torch.cat((fact[1], torch.zeros(l1).long() + l1), 0)
mat_ = torch.Tensor(l1, l1 + l2)
a = 1 / (fact[3] + 1e-10) + 1
mat_[:, l2:] = (1 / a.min(1)[0].resize_(l1)).diag()
mat_[:, :l2] = 1 / a - 1e-10
return [fact[0], index1_, index2_, mat_]
示例10: forward_step
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def forward_step(net, mid_net, loss_fn, loader, args):
pair_total = 0
catfeat = []
for i in range(args.loss_step):
catfeat.append([])
label = []
epoch_end = False
while pair_total < args.max_pair / args.worker:
pair_total, epoch_end = embed_step(pair_total, net, mid_net, loss_fn, catfeat,
label, loader, args, volatile=args.fix_net)
losses = []
for k in range(len(catfeat)):
if args.fix_net:
for feat in catfeat[k]:
if isinstance(feat, tuple):
feat[0].volatile = False
feat[0].requires_grad = False
feat[1].volatile = False
feat[1].requires_grad = False
else:
feat.volatile = False
feat.requires_grad = False
losses.append(loss_fn[k](catfeat[k], label))
loss = sum(losses)
loss_total = loss.data[0]
correct = []
for k in range(len(loss_fn)):
correct.append(loss_fn[k].check_result(label))
total = len(label)
net.zero_grad()
if mid_net != None:
mid_net.zero_grad()
for l in loss_fn:
l.zero_grad()
loss.backward()
return loss_total, correct, total, epoch_end
示例11: __getitem__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def __getitem__(self, index):
assert index <= len(self), 'index range error'
index += 1
with self.env.begin(write=False) as txn:
img_key = b'image-%09d' % index
imgbuf = txn.get(img_key)
buf = six.BytesIO()
buf.write(imgbuf)
buf.seek(0)
try:
img = Image.open(buf).convert('L')
except IOError:
print('Corrupted image for %d' % index)
return self[index + 1]
if self.transform is not None:
img = self.transform(img)
label_key = b'label-%09d' % index
label = str(txn.get(label_key))
if self.target_transform is not None:
label = self.target_transform(label)
return (img, label.split('\'')[1])
示例12: __iter__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def __iter__(self):
n_batch = len(self) // self.batch_size
tail = len(self) % self.batch_size
index = torch.LongTensor(len(self)).fill_(0)
for i in range(n_batch):
random_start = random.randint(0, len(self) - self.batch_size)
batch_index = random_start + torch.range(0, self.batch_size - 1)
index[i * self.batch_size:(i + 1) * self.batch_size] = batch_index
# deal with tail
if tail:
random_start = random.randint(0, len(self) - self.batch_size)
tail_index = random_start + torch.range(0, tail - 1)
index[(i + 1) * self.batch_size:] = tail_index
return iter(index)
示例13: calc_logit
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def calc_logit(self, dataloader):
self.model.eval()
n_stage = self.args.nBlocks
logits = [[] for _ in range(n_stage)]
targets = []
for i, (input, target) in enumerate(dataloader):
targets.append(target)
with torch.no_grad():
input_var = torch.autograd.Variable(input)
output = self.model(input_var)
if not isinstance(output, list):
output = [output]
for b in range(n_stage):
_t = self.softmax(output[b])
logits[b].append(_t)
if i % self.args.print_freq == 0:
print('Generate Logit: [{0}/{1}]'.format(i, len(dataloader)))
for b in range(n_stage):
logits[b] = torch.cat(logits[b], dim=0)
size = (n_stage, logits[0].size(0), logits[0].size(1))
ts_logits = torch.Tensor().resize_(size).zero_()
for b in range(n_stage):
ts_logits[b].copy_(logits[b])
targets = torch.cat(targets, dim=0)
ts_targets = torch.Tensor().resize_(size[1]).copy_(targets)
return ts_logits, ts_targets
示例14: sequence_mask
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def sequence_mask(sequence_length, max_len=None):
if max_len is None:
max_len = sequence_length.data.max()
batch_size = sequence_length.size(0)
# seq_range = torch.range(0, max_len - 1).long()
seq_range = torch.arange(0, max_len).long() # andy
seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
seq_range_expand = Variable(seq_range_expand)
if sequence_length.is_cuda:
seq_range_expand = seq_range_expand.cuda()
seq_length_expand = (sequence_length.unsqueeze(1)
.expand_as(seq_range_expand))
return seq_range_expand < seq_length_expand
示例15: pesudo_nms_poly
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import range [as 別名]
def pesudo_nms_poly(dets, iou_thr):
keep = torch.range(0, len(dets))
return dets, keep