本文整理汇总了Python中torch.autograd.Variable.eq方法的典型用法代码示例。如果您正苦于以下问题:Python Variable.eq方法的具体用法?Python Variable.eq怎么用?Python Variable.eq使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.autograd.Variable
的用法示例。
在下文中一共展示了Variable.eq方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_one_hot
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import eq [as 别名]
def get_one_hot(labels, num_classes):
one_hot = Variable(torch.range(0, num_classes-1)).unsqueeze(0).expand(labels.size(0), num_classes)
if (type(labels.data) is torch.cuda.FloatTensor) or (type(labels.data) is torch.cuda.LongTensor):
one_hot = one_hot.cuda()
one_hot = one_hot.eq(labels.unsqueeze(1).expand_as(one_hot).float()).float()
return one_hot
示例2: _score_candidates
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import eq [as 别名]
def _score_candidates(self, cands, xe, encoder_output, hidden):
# score each candidate separately
# cands are exs_with_cands x cands_per_ex x words_per_cand
# cview is total_cands x words_per_cand
cview = cands.view(-1, cands.size(2))
cands_xes = xe.expand(xe.size(0), cview.size(0), xe.size(2))
sz = hidden.size()
cands_hn = (
hidden.view(sz[0], sz[1], 1, sz[2])
.expand(sz[0], sz[1], cands.size(1), sz[2])
.contiguous()
.view(sz[0], -1, sz[2])
)
sz = encoder_output.size()
cands_encoder_output = (
encoder_output.contiguous()
.view(sz[0], 1, sz[1], sz[2])
.expand(sz[0], cands.size(1), sz[1], sz[2])
.contiguous()
.view(-1, sz[1], sz[2])
)
cand_scores = Variable(
self.cand_scores.resize_(cview.size(0)).fill_(0))
cand_lengths = Variable(
self.cand_lengths.resize_(cview.size(0)).fill_(0))
for i in range(cview.size(1)):
output = self._apply_attention(cands_xes, cands_encoder_output, cands_hn) \
if self.use_attention else cands_xes
output, cands_hn = self.decoder(output, cands_hn)
preds, scores = self.hidden_to_idx(output, dropout=False)
cs = cview.select(1, i)
non_nulls = cs.ne(self.NULL_IDX)
cand_lengths += non_nulls.long()
score_per_cand = torch.gather(scores, 1, cs.unsqueeze(1))
cand_scores += score_per_cand.squeeze() * non_nulls.float()
cands_xes = self.lt2dec(self.lt(cs).unsqueeze(0))
# set empty scores to -1, so when divided by 0 they become -inf
cand_scores -= cand_lengths.eq(0).float()
# average the scores per token
cand_scores /= cand_lengths.float()
cand_scores = cand_scores.view(cands.size(0), cands.size(1))
srtd_scores, text_cand_inds = cand_scores.sort(1, True)
text_cand_inds = text_cand_inds.data
return text_cand_inds
示例3: _score_candidates
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import eq [as 别名]
def _score_candidates(self, cands, cand_inds, start, encoder_output, hidden, attn_mask):
"""Rank candidates by their likelihood according to the decoder."""
if type(self.decoder) == nn.LSTM:
hidden, cell = hidden
# score each candidate separately
# cands are exs_with_cands x cands_per_ex x words_per_cand
# cview is total_cands x words_per_cand
cview = cands.view(-1, cands.size(2))
c_xes = start.expand(cview.size(0), start.size(0), start.size(1))
if len(cand_inds) != hidden.size(1):
# only use hidden state from inputs with associated candidates
cand_indices = torch.LongTensor([i for i, _, _ in cand_inds])
if self.use_cuda:
cand_indices = cand_indices.cuda()
cand_indices = Variable(cand_indices)
hidden = hidden.index_select(1, cand_indices)
sz = hidden.size()
cands_hn = (
hidden.view(sz[0], sz[1], 1, sz[2])
.expand(sz[0], sz[1], cands.size(1), sz[2])
.contiguous()
.view(sz[0], -1, sz[2])
)
if type(self.decoder) == nn.LSTM:
if len(cand_inds) != cell.size(1):
# only use cell state from inputs with associated candidates
cell = cell.index_select(1, cand_indices)
cands_hn = (cands_hn, cell.view(sz[0], sz[1], 1, sz[2])
.expand(sz[0], sz[1], cands.size(1), sz[2])
.contiguous()
.view(sz[0], -1, sz[2]))
cand_scores = Variable(
self.cand_scores.resize_(cview.size(0)).fill_(0))
cand_lengths = Variable(
self.cand_lengths.resize_(cview.size(0)).fill_(0))
if self.attention != 'none':
# using attention
# select only encoder output matching xs we want
if len(cand_inds) != len(encoder_output):
indices = torch.LongTensor([i[0] for i in cand_inds])
if self.use_cuda:
indices = indices.cuda()
indices = Variable(indices)
encoder_output = encoder_output.index_select(0, indices)
attn_mask = attn_mask.index_select(0, indices)
sz = encoder_output.size()
cands_encoder_output = (
encoder_output.contiguous()
.view(sz[0], 1, sz[1], sz[2])
.expand(sz[0], cands.size(1), sz[1], sz[2])
.contiguous()
.view(-1, sz[1], sz[2])
)
msz = attn_mask.size()
cands_attn_mask = (
attn_mask.contiguous()
.view(msz[0], 1, msz[1])
.expand(msz[0], cands.size(1), msz[1])
.contiguous()
.view(-1, msz[1])
)
for i in range(cview.size(1)):
# process one token at a time
h_att = cands_hn[0] if type(self.decoder) == nn.LSTM else cands_hn
output = self._apply_attention(c_xes, cands_encoder_output, h_att, cands_attn_mask)
output, cands_hn = self.decoder(output, cands_hn)
_preds, scores = self.hidden_to_idx(output, is_training=False)
cs = cview.select(1, i)
non_nulls = cs.ne(self.NULL_IDX)
cand_lengths += non_nulls.long()
score_per_cand = torch.gather(scores.squeeze(), 1, cs.unsqueeze(1))
cand_scores += score_per_cand.squeeze() * non_nulls.float()
c_xes = self.dec_lt(cs).unsqueeze(1)
else:
# process entire sequence at once
if cview.size(1) > 1:
# feed in START + cands[:-2]
cands_in = cview.narrow(1, 0, cview.size(1) - 1)
c_xes = torch.cat([c_xes, self.dec_lt(cands_in)], 1)
output, cands_hn = self.decoder(c_xes, cands_hn)
_preds, scores = self.hidden_to_idx(output, is_training=False)
for i in range(cview.size(1)):
# calculate score at each token
cs = cview.select(1, i)
non_nulls = cs.ne(self.NULL_IDX)
cand_lengths += non_nulls.long()
score_per_cand = torch.gather(scores.select(1, i), 1, cs.unsqueeze(1))
cand_scores += score_per_cand.squeeze() * non_nulls.float()
# set empty scores to -1, so when divided by 0 they become -inf
cand_scores -= cand_lengths.eq(0).float()
# average the scores per token
cand_scores /= cand_lengths.float()
#.........这里部分代码省略.........
示例4: forward
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import eq [as 别名]
def forward(self, cands, cand_inds, start, hidden, enc_out, attn_mask):
cell = None
if type(hidden) == tuple:
# for lstms, split hidden state into parts
hidden, cell = hidden
num_hid = hidden.size(0)
bsz = hidden.size(1)
esz = hidden.size(2)
cands_per_ex = cands.size(1)
words_per_cand = cands.size(2)
# score each candidate separately
# cands are exs_with_cands x cands_per_ex x words_per_cand
# cview is total_cands x words_per_cand
cview = cands.view(-1, words_per_cand)
total_cands = cview.size(0)
starts = start.expand(total_cands).unsqueeze(1)
if len(cand_inds) != hidden.size(1):
# select hidden states which have associated cands
cand_indices = Variable(start.data.new([i[0] for i in cand_inds]))
hidden = hidden.index_select(1, cand_indices)
h_exp = (
# expand hidden states so each cand has an initial hidden state
# cands for the same input have the same initial hidden state
hidden.unsqueeze(2)
.expand(num_hid, bsz, cands_per_ex, esz)
.contiguous()
.view(num_hid, -1, esz))
if cell is None:
cands_hn = h_exp
if cell is not None:
if len(cand_inds) != cell.size(1):
# only use cell state from inputs with associated candidates
cell = cell.index_select(1, cand_indices)
c_exp = (
cell.unsqueeze(2)
.expand(num_hid, bsz, cands_per_ex, esz)
.contiguous()
.view(num_hid, -1, esz))
cands_hn = (h_exp, c_exp)
cand_scores = Variable(self.buffer(hidden, 'cand_scores', total_cands))
cand_lens = Variable(self.buffer(start, 'cand_lens', total_cands))
if self.attn_type == 'none':
# process entire sequence at once
if cview.size(1) > 1:
# feed in START + cands[:-2]
cands_in = cview.narrow(1, 0, cview.size(1) - 1)
starts = torch.cat([starts, self.dec_lt(cands_in)], 1)
_preds, score, _h = self.decoder(starts, cands_hn, enc_out, attn_mask)
for i in range(cview.size(1)):
# calculate score at each token
cs = cview.select(1, i)
non_nulls = cs.ne(self.NULL_IDX)
cand_lens += non_nulls.long()
score_per_cand = torch.gather(score.select(1, i), 1,
cs.unsqueeze(1))
cand_scores += score_per_cand.squeeze() * non_nulls.float()
else:
# using attention
if len(cand_inds) != len(enc_out):
# select only encoder output matching xs we want
indices = Variable(start.data.new([i[0] for i in cand_inds]))
enc_out = enc_out.index_select(0, indices)
attn_mask = attn_mask.index_select(0, indices)
seq_len = enc_out.size(1)
cands_enc_out = (
enc_out.unsqueeze(1)
.expand(bsz, cands_per_ex, seq_len, esz)
.contiguous()
.view(-1, seq_len, esz)
)
cands_attn_mask = (
attn_mask.unsqueeze(1)
.expand(bsz, cands_per_ex, seq_len)
.contiguous()
.view(-1, seq_len)
)
cs = starts
for i in range(cview.size(1)):
# process one token at a time
_preds, score, _h = self.decoder(cs, cands_hn, cands_enc_out,
cands_attn_mask)
cs = cview.select(1, i)
non_nulls = cs.ne(self.NULL_IDX)
cand_lens += non_nulls.long()
score_per_cand = torch.gather(score.squeeze(), 1,
cs.unsqueeze(1))
cand_scores += score_per_cand.squeeze() * non_nulls.float()
# set empty scores to -1, so when divided by 0 they become -inf
cand_scores -= cand_lens.eq(0).float()
#.........这里部分代码省略.........
示例5: range
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import eq [as 别名]
net.eval();
npts_test=data_test.size();
test_acc=[];
for i in range(0,npts_test,params.batch):
r=min(npts_test,i+params.batch);
for j in range(0,params.bnn):
eid=int(ensemble_ind[j]);
seed=float(seeds[j]);
w=pms[eid](seed);
pms[eid].impose(w,net.parameters());
im,label=data_test.batch_eval(i,r);
im=Variable(im.cuda());
score_test[i:r,j,:]=F.log_softmax(net(im),dim=1).data;
score=util.math.logmeanexp(score_test[i:r,:,:],dim=1);
_,pred=score.max(dim=1);
test_acc.append(label.eq(pred).float().sum()/npts_test);
test_acc=sum(test_acc);
session.log('Iter %d, test acc %f, time %f'%(iter_al,test_acc,time.time()-t0));
#Evaluate scores for training pool examples
net.eval();
npts=data_train.size();
for i in range(0,npts,params.batch):
r=min(npts,i+params.batch);
for j in range(0,params.bnn):
eid=int(ensemble_ind[j]);
seed=float(seeds[j]);
w=pms[eid](seed);
pms[eid].impose(w,net.parameters());
im,label=data_train.batch_eval(i,r);
im=Variable(im.cuda());
示例6: train
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import eq [as 别名]
def train(train_x, train_y, valid_x, valid_y):
epochs = 30
batch_size = 32# 100
train_x = torch.from_numpy(train_x).float().type(torch.FloatTensor).cuda()
train_y = torch.from_numpy(train_y)
valid_x = torch.from_numpy(valid_x).float().type(torch.FloatTensor).cuda()
valid_y = torch.from_numpy(valid_y)
train_ = torch.utils.data.TensorDataset(train_x, train_y)
train_loader = torch.utils.data.DataLoader(train_, batch_size=batch_size, shuffle=True)
valid_ = torch.utils.data.TensorDataset(valid_x, valid_y)
valid_loader = torch.utils.data.DataLoader(valid_, batch_size=batch_size, shuffle=True)
prev_accs_train = deque(maxlen=10)
prev_accs_valid = deque(maxlen=10)
start = time.time()
model.train()#for BN
for epoch in range(epochs):
for batch_idx, (data, target) in enumerate(train_loader):
batch = Variable(data)#.type(model.dtype)
target = Variable(target).cuda()#.type(model.dtype)
optimizer.zero_grad()
output = model.forward(batch)
loss = criterion(output, target)
loss.backward()
optimizer.step()
if batch_idx % 200 == 0:
pred = torch.max(output, dim=1)[1]
train_batch_acc = target.eq(pred).float().mean()
# print (train_batch_acc.shape)
prev_accs_train.append(train_batch_acc.data.cpu().numpy()) #[0])
model.eval()
for batch_idx_valid, (data, target) in enumerate(valid_loader):
batch = Variable(data, requires_grad=False)#.type(model.dtype)
target = Variable(target, requires_grad=False).cuda()#.type(model.dtype)
output = model.forward(batch)
loss_valid = criterion(output, target)
pred = torch.max(output, dim=1)[1]
valid_batch_acc = target.eq(pred).float().mean()
prev_accs_valid.append(valid_batch_acc.data.cpu().numpy()) #[0])
break
model.train()#for BN
batch_time = time.time() - start
print ('Epoch:{:3d}/{:3d}'.format(epoch, epochs),
'batch:{:4d}'.format(batch_idx),
'time:{:.3f}'.format(batch_time),
' Train loss:{:.3f}'.format(loss.data.cpu().numpy()), #[0]),
'acc:{:.3f}'.format(train_batch_acc.data.cpu().numpy()), #[0]),
'avgacc:{:.3f}'.format(np.mean(prev_accs_train)),
' Valid loss:{:.3f}'.format(loss_valid.data.cpu().numpy()), #[0]),
'acc:{:.3f}'.format(valid_batch_acc.data.cpu().numpy()), #[0]),
'avgacc:{:.3f}'.format(np.mean(prev_accs_valid))
)
start = time.time()