本文整理汇总了Python中torch.tensor函数的典型用法代码示例。如果您正苦于以下问题:Python tensor函数的具体用法?Python tensor怎么用?Python tensor使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tensor函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_gather_extended_gold_tokens
def test_gather_extended_gold_tokens(self):
vocab_size = self.model._target_vocab_size
end_index = self.model._end_index
pad_index = self.model._pad_index
oov_index = self.model._oov_index
tok_index = 6 # some other arbitrary token
assert tok_index not in [end_index, pad_index, oov_index]
# first sentence tokens:
# 1: oov but not copied
# 2: not oov and not copied
# 3: not copied
# 4: not copied
# second sentence tokens:
# 1: not oov and copied
# 2: oov and copied
# 3: not copied
# 4: not copied
# shape: (batch_size, target_sequence_length)
target_tokens = torch.tensor([[oov_index, tok_index, end_index, pad_index],
[tok_index, oov_index, tok_index, end_index]])
# shape: (batch_size, trimmed_source_length)
source_token_ids = torch.tensor([[0, 1, 2, 3],
[0, 1, 0, 2]])
# shape: (batch_size, target_sequence_length)
target_token_ids = torch.tensor([[4, 5, 6, 7],
[1, 0, 3, 4]])
# shape: (batch_size, target_sequence_length)
result = self.model._gather_extended_gold_tokens(target_tokens, source_token_ids, target_token_ids)
# shape: (batch_size, target_sequence_length)
check = np.array([[oov_index, tok_index, end_index, pad_index],
[tok_index, vocab_size, tok_index, end_index]])
np.testing.assert_array_equal(result.numpy(), check)
示例2: calc_loss
def calc_loss(batch, net, tgt_net, gamma, device="cpu", save_prefix=None):
states, actions, rewards, dones, next_states = common.unpack_batch(batch)
batch_size = len(batch)
states_v = torch.tensor(states).to(device)
actions_v = torch.tensor(actions).to(device)
next_states_v = torch.tensor(next_states).to(device)
# next state distribution
next_distr_v, next_qvals_v = tgt_net.both(next_states_v)
next_actions = next_qvals_v.max(1)[1].data.cpu().numpy()
next_distr = tgt_net.apply_softmax(next_distr_v).data.cpu().numpy()
next_best_distr = next_distr[range(batch_size), next_actions]
dones = dones.astype(np.bool)
# project our distribution using Bellman update
proj_distr = common.distr_projection(next_best_distr, rewards, dones, Vmin, Vmax, N_ATOMS, gamma)
# calculate net output
distr_v = net(states_v)
state_action_values = distr_v[range(batch_size), actions_v.data]
state_log_sm_v = F.log_softmax(state_action_values, dim=1)
proj_distr_v = torch.tensor(proj_distr).to(device)
if save_prefix is not None:
pred = F.softmax(state_action_values, dim=1).data.cpu().numpy()
save_transition_images(batch_size, pred, proj_distr, next_best_distr, dones, rewards, save_prefix)
loss_v = -state_log_sm_v * proj_distr_v
return loss_v.sum(dim=1).mean()
示例3: test_factory
def test_factory(self):
default_size = torch.Size([1, 3])
size = torch.Size([3, 3])
for include_size in [True, False]:
for use_tensor_idx in [True, False]:
for use_tensor_val in [True, False]:
for use_cuda in ([False] if not torch.cuda.is_available() else [True, False]):
# have to include size with cuda sparse tensors
include_size = include_size or use_cuda
dtype = torch.float64
long_dtype = torch.int64
device = torch.device('cpu') if not use_cuda else torch.device(torch.cuda.device_count() - 1)
indices = torch.tensor(([0], [2]), dtype=long_dtype) if use_tensor_idx else ([0], [2])
values = torch.tensor([1.], dtype=dtype) if use_tensor_val else 1.
if include_size:
sparse_tensor = torch.sparse_coo_tensor(indices, values, size, dtype=dtype,
device=device, requires_grad=True)
else:
sparse_tensor = torch.sparse_coo_tensor(indices, values, dtype=dtype,
device=device, requires_grad=True)
self.assertEqual(indices, sparse_tensor._indices())
self.assertEqual(values, sparse_tensor._values())
self.assertEqual(size if include_size else default_size, sparse_tensor.size())
self.assertEqual(dtype, sparse_tensor.dtype)
if use_cuda:
self.assertEqual(device, sparse_tensor._values().device)
self.assertEqual(True, sparse_tensor.requires_grad)
示例4: model
def model():
p2 = torch.tensor(torch.ones(2) / 2)
p3 = torch.tensor(torch.ones(3) / 3)
x2 = pyro.sample("x2", dist.OneHotCategorical(p2))
x3 = pyro.sample("x3", dist.OneHotCategorical(p3))
assert x2.shape == torch.Size([2]) + iarange_shape + p2.shape
assert x3.shape == torch.Size([3, 1]) + iarange_shape + p3.shape
示例5: generate_translation
def generate_translation(encoder, decoder, sentence, max_length, target_lang, search="greedy", k = None):
"""
@param max_length: the max # of words that the decoder can return
@returns decoded_words: a list of words in target language
"""
with torch.no_grad():
input_tensor = sentence
input_length = sentence.size()[1]
# encode the source sentence
encoder_hidden = encoder.init_hidden(1)
# input_tensor 1 by 12
#
encoder_outputs, encoder_hidden = encoder(input_tensor.view(1, -1),torch.tensor([input_length]))
# start decoding
decoder_input = torch.tensor([[SOS_token]], device=device) # SOS
decoder_hidden = encoder_hidden
decoded_words = []
if search == 'greedy':
decoded_words = greedy_search_batch(decoder, decoder_input, encoder_outputs, decoder_hidden, max_length)
elif search == 'beam':
if k == None:
k = 2 # since k = 2 preforms badly
decoded_words = beam_search(decoder, decoder_input, encoder_outputs, decoder_hidden, max_length, k, target_lang)
return decoded_words
示例6: test_index_setitem_bools_slices
def test_index_setitem_bools_slices(self):
true = torch.tensor(1, dtype=torch.uint8)
false = torch.tensor(0, dtype=torch.uint8)
tensors = [Variable(torch.randn(2, 3)), torch.tensor(3)]
for a in tensors:
# prefix with a 1,1, to ensure we are compatible with numpy which cuts off prefix 1s
# (some of these ops already prefix a 1 to the size)
neg_ones = torch.ones_like(a) * -1
neg_ones_expanded = neg_ones.unsqueeze(0).unsqueeze(0)
a[True] = neg_ones_expanded
self.assertEqual(a, neg_ones)
a[False] = 5
self.assertEqual(a, neg_ones)
a[true] = neg_ones_expanded * 2
self.assertEqual(a, neg_ones * 2)
a[false] = 5
self.assertEqual(a, neg_ones * 2)
a[None] = neg_ones_expanded * 3
self.assertEqual(a, neg_ones * 3)
a[...] = neg_ones_expanded * 4
self.assertEqual(a, neg_ones * 4)
if a.dim() == 0:
with self.assertRaises(RuntimeError):
a[:] = neg_ones_expanded * 5
示例7: diamond_guide
def diamond_guide(dim):
p0 = torch.tensor(math.exp(-0.70), requires_grad=True)
p1 = torch.tensor(math.exp(-0.43), requires_grad=True)
pyro.sample("a1", dist.Bernoulli(p0))
for i in pyro.irange("irange", dim):
pyro.sample("b{}".format(i), dist.Bernoulli(p1))
pyro.sample("c1", dist.Bernoulli(p0))
示例8: perform_val
def perform_val(multi_gpu, device, embedding_size, batch_size, backbone, carray, issame, nrof_folds = 10, tta = True):
if multi_gpu:
backbone = backbone.module # unpackage model from DataParallel
backbone = backbone.to(device)
else:
backbone = backbone.to(device)
backbone.eval() # switch to evaluation mode
idx = 0
embeddings = np.zeros([len(carray), embedding_size])
with torch.no_grad():
while idx + batch_size <= len(carray):
batch = torch.tensor(carray[idx:idx + batch_size][:, [2, 1, 0], :, :])
if tta:
fliped = hflip_batch(batch)
emb_batch = backbone(batch.to(device)).cpu() + backbone(fliped.to(device)).cpu()
embeddings[idx:idx + batch_size] = l2_norm(emb_batch)
else:
embeddings[idx:idx + batch_size] = backbone(batch.to(device)).cpu()
idx += batch_size
if idx < len(carray):
batch = torch.tensor(carray[idx:])
if tta:
fliped = hflip_batch(batch)
emb_batch = backbone(batch.to(device)).cpu() + backbone(fliped.to(device)).cpu()
embeddings[idx:] = l2_norm(emb_batch)
else:
embeddings[idx:] = backbone(batch.to(device)).cpu()
tpr, fpr, accuracy, best_thresholds = evaluate(embeddings, issame, nrof_folds)
buf = gen_plot(fpr, tpr)
roc_curve = Image.open(buf)
roc_curve_tensor = transforms.ToTensor()(roc_curve)
return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor
示例9: testDutyCycleUpdate
def testDutyCycleUpdate(self):
"""
Start with equal duty cycle, boost factor=0, k=4, batch size=2
"""
x = self.x2
expected = torch.zeros_like(x)
expected[0, 0, 1, 0] = 1.1
expected[0, 0, 1, 1] = 1.2
expected[0, 1, 0, 1] = 1.2
expected[0, 2, 1, 0] = 1.3
expected[1, 0, 0, 0] = 1.4
expected[1, 1, 0, 0] = 1.5
expected[1, 1, 0, 1] = 1.6
expected[1, 2, 1, 1] = 1.7
dutyCycle = torch.zeros((1, 3, 1, 1))
dutyCycle[:] = 1.0 / 3.0
updateDutyCycleCNN(expected, dutyCycle, 2, 2)
newDuty = torch.tensor([1.5000, 1.5000, 1.0000]) / 4.0
diff = (dutyCycle.reshape(-1) - newDuty).abs().sum()
self.assertLessEqual(diff, 0.001)
dutyCycle[:] = 1.0 / 3.0
updateDutyCycleCNN(expected, dutyCycle, 4, 4)
newDuty = torch.tensor([0.3541667, 0.3541667, 0.2916667])
diff = (dutyCycle.reshape(-1) - newDuty).abs().sum()
self.assertLessEqual(diff, 0.001)
示例10: test_advance_with_all_repeats_gets_blocked
def test_advance_with_all_repeats_gets_blocked(self):
# all beams repeat (beam >= 1 repeat dummy scores)
beam_sz = 5
n_words = 100
repeat_idx = 47
ngram_repeat = 3
for batch_sz in [1, 3]:
beam = BeamSearch(
beam_sz, batch_sz, 0, 1, 2, 2,
torch.device("cpu"), GlobalScorerStub(), 0, 30,
False, ngram_repeat, set(),
torch.randint(0, 30, (batch_sz,)), False, 0.)
for i in range(ngram_repeat + 4):
# predict repeat_idx over and over again
word_probs = torch.full(
(batch_sz * beam_sz, n_words), -float('inf'))
word_probs[0::beam_sz, repeat_idx] = 0
attns = torch.randn(1, batch_sz * beam_sz, 53)
beam.advance(word_probs, attns)
if i <= ngram_repeat:
expected_scores = torch.tensor(
[0] + [-float('inf')] * (beam_sz - 1))\
.repeat(batch_sz, 1)
self.assertTrue(beam.topk_log_probs.equal(expected_scores))
else:
self.assertTrue(
beam.topk_log_probs.equal(
torch.tensor(self.BLOCKED_SCORE)
.repeat(batch_sz, beam_sz)))
示例11: test_optimizers
def test_optimizers(factory):
optim = factory()
def model(loc, cov):
x = pyro.param("x", torch.randn(2))
y = pyro.param("y", torch.randn(3, 2))
z = pyro.param("z", torch.randn(4, 2).abs(), constraint=constraints.greater_than(-1))
pyro.sample("obs_x", dist.MultivariateNormal(loc, cov), obs=x)
with pyro.iarange("y_iarange", 3):
pyro.sample("obs_y", dist.MultivariateNormal(loc, cov), obs=y)
with pyro.iarange("z_iarange", 4):
pyro.sample("obs_z", dist.MultivariateNormal(loc, cov), obs=z)
loc = torch.tensor([-0.5, 0.5])
cov = torch.tensor([[1.0, 0.09], [0.09, 0.1]])
for step in range(100):
tr = poutine.trace(model).get_trace(loc, cov)
loss = -tr.log_prob_sum()
params = {name: pyro.param(name).unconstrained() for name in ["x", "y", "z"]}
optim.step(loss, params)
for name in ["x", "y", "z"]:
actual = pyro.param(name)
expected = loc.expand(actual.shape)
assert_equal(actual, expected, prec=1e-2,
msg='{} in correct: {} vs {}'.format(name, actual, expected))
示例12: run_episode
def run_episode(self, episode, steps_accumulated=0):
start_time = time.time()
observation = self.env.reset()
state = torch.from_numpy(observation).to(self.config.device, dtype=torch.float32).unsqueeze(0)
for step in range(MAX_STEPS):
action = self.agent.get_action(state, step + steps_accumulated)
observation_next, _, done, _ = self.env.step(action.item())
if done:
state_next = None
self.total_step = np.hstack((self.total_step[1:], step + 1))
if self.is_success_episode(step):
reward = torch.tensor([1.0], dtype=torch.float32, device=self.config.device)
else:
reward = torch.tensor([-1.0], dtype=torch.float32, device=self.config.device)
else:
reward = torch.tensor([0.0], dtype=torch.float32, device=self.config.device)
state_next = torch.from_numpy(observation_next).to(self.config.device, dtype=torch.float32).unsqueeze(0)
self.agent.observe(state, action, state_next, reward)
if step % self.config.replay_interval == 0:
self.agent.learn(episode)
state = state_next
if done:
elapsed_time = round(time.time() - start_time, 3)
print('episode: {0}, steps: {1}, mean steps {2}, time: {3}'.format(episode, step, self.total_step.mean(), elapsed_time))
return step + 1
return MAX_STEPS
示例13: load
def load(self, fdata, use_char=False, n_context=1, max_len=10):
sentences = self.preprocess(fdata)
x, y, char_x, lens = [], [], [], []
for wordseq, tagseq in sentences:
wiseq = [self.wdict.get(w, self.unk_wi) for w in wordseq]
tiseq = [self.tdict[t] for t in tagseq]
# 获取每个词汇的上下文
if n_context > 1:
x.append(self.get_context(wiseq, n_context))
else:
x.append(torch.tensor(wiseq, dtype=torch.long))
y.append(torch.tensor(tiseq, dtype=torch.long))
# 不足最大长度的部分用0填充
char_x.append(torch.tensor([
[self.cdict.get(c, self.unk_ci)
for c in w[:max_len]] + [0] * (max_len - len(w))
for w in wordseq
]))
lens.append(len(tiseq))
x = pad_sequence(x, True)
y = pad_sequence(y, True)
char_x = pad_sequence(char_x, True)
lens = torch.tensor(lens)
if use_char:
dataset = TensorDataset(x, y, char_x, lens)
else:
dataset = TensorDataset(x, y, lens)
return dataset
示例14: guide
def guide():
q1 = pyro.param("q1", torch.tensor(pi1, requires_grad=True))
q2 = pyro.param("q2", torch.tensor(pi2, requires_grad=True))
with pyro.iarange("particles", num_particles):
y = pyro.sample("y", dist.Bernoulli(q1).expand_by([num_particles]), infer={"enumerate": enumerate1})
if include_z:
pyro.sample("z", dist.Normal(q2 * y + 0.10, 1.0))
示例15: __init__
def __init__(self, mean, std):
super(Normalization, self).__init__()
# .view the mean and std to make them [C x 1 x 1] so that they can
# directly work with image Tensor of shape [B x C x H x W].
# B is batch size. C is number of channels. H is height and W is width.
self.mean = torch.tensor(mean).view(-1, 1, 1)
self.std = torch.tensor(std).view(-1, 1, 1)