def _score_sentence(self, input, tags):
bsz, sent_len, l_size = input.size()
score = Variable(self.torch.FloatTensor(bsz).fill_(0.))
s_score = Variable(self.torch.LongTensor([[START]]*bsz))
tags = torch.cat([s_score, tags], dim=-1)
input_t = input.transpose(0, 1)
for i, words in enumerate(input_t):
temp = self.transitions.index_select(1, tags[:, i])
bsz_t = gather_index(temp.transpose(0, 1), tags[:, i + 1])
w_step_score = gather_index(words, tags[:, i+1])
score = score + bsz_t + w_step_score
temp = self.transitions.index_select(1, tags[:, -1])
bsz_t = gather_index(temp.transpose(0, 1),
Variable(self.torch.LongTensor([STOP]*bsz)))
return score+bsz_t
python类cat()的实例源码
def forward(self, input):
bsz, sent_len, l_size = input.size()
init_alphas = self.torch.FloatTensor(bsz, self.label_size).fill_(-10000.)
init_alphas[:, START].fill_(0.)
forward_var = Variable(init_alphas)
input_t = input.transpose(0, 1)
for words in input_t:
alphas_t = []
for next_tag in range(self.label_size):
emit_score = words[:, next_tag].contiguous()
emit_score = emit_score.unsqueeze(1).expand_as(words)
trans_score = self.transitions[next_tag, :].view(1, -1).expand_as(words)
next_tag_var = forward_var + trans_score + emit_score
alphas_t.append(log_sum_exp(next_tag_var, True))
forward_var = torch.cat(alphas_t, dim=-1)
return log_sum_exp(forward_var)
def _word_repre_layer(self, input):
"""
args:
- input: (q_sentence, q_words)|(a_sentence, a_words)
q_sentence - [batch_size, sent_length]
q_words - [batch_size, sent_length, words_len]
return:
- output: [batch_size, sent_length, context_dim]
"""
sentence, words = input
# [batch_size, sent_length, corpus_emb_dim]
s_encode = self.corpus_emb(sentence)
# [batch_size, sent_length, word_lstm_dim]
w_encode = self._word_repre_forward(words)
w_encode = F.dropout(w_encode, p=self.dropout, training=True, inplace=False)
out = torch.cat((s_encode, w_encode), 2)
return out
def _aggre(self, q_aware_reps, a_aware_reps):
"""
Aggregation Layer handle
Args:
q_aware_reps - [batch_size, question_len, 11*mp_dim+6]
a_aware_reps - [batch_size, answer_len, 11*mp_dim+6]
Return:
size - [batch_size, aggregation_lstm_dim*4]
"""
_aggres = []
_, (q_hidden, _) = self.aggre_lstm(q_aware_reps)
_, (a_hidden, _) = self.aggre_lstm(a_aware_reps)
# [batch_size, aggregation_lstm_dim]
_aggres.append(q_hidden[-2])
_aggres.append(q_hidden[-1])
_aggres.append(a_hidden[-2])
_aggres.append(a_hidden[-1])
return torch.cat(_aggres, dim=1)
def __next__(self):
def img2variable(img_files):
tensors = [self._encode(Image.open(self._path + img_name)).unsqueeze(0)
for img_name in img_files]
v = Variable(torch.cat(tensors, 0))
if self._is_cuda: v = v.cuda()
return v
if self._step == self._stop_step:
self._step = 0
raise StopIteration()
_start = self._step*self._batch_size
self._step += 1
return img2variable(self._img_files[_start:_start+self._batch_size])
def __init__(self):
self.data_location = 'cat.npz'
self.enc_hidden_size = 256
self.dec_hidden_size = 512
self.Nz = 128
self.M = 20
self.dropout = 0.9
self.batch_size = 100
self.eta_min = 0.01
self.R = 0.99995
self.KL_min = 0.2
self.wKL = 0.5
self.lr = 0.001
self.lr_decay = 0.9999
self.min_lr = 0.00001
self.grad_clip = 1.
self.temperature = 0.4
self.max_seq_length = 200
def forward(self, inputs, batch_size, hidden_cell=None):
if hidden_cell is None:
# then must init with zeros
if use_cuda:
hidden = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size).cuda())
cell = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size).cuda())
else:
hidden = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size))
cell = Variable(torch.zeros(2, batch_size, hp.enc_hidden_size))
hidden_cell = (hidden, cell)
_, (hidden,cell) = self.lstm(inputs.float(), hidden_cell)
# hidden is (2, batch_size, hidden_size), we want (batch_size, 2*hidden_size):
hidden_forward, hidden_backward = torch.split(hidden,1,0)
hidden_cat = torch.cat([hidden_forward.squeeze(0), hidden_backward.squeeze(0)],1)
# mu and sigma:
mu = self.fc_mu(hidden_cat)
sigma_hat = self.fc_sigma(hidden_cat)
sigma = torch.exp(sigma_hat/2.)
# N ~ N(0,1)
z_size = mu.size()
if use_cuda:
N = Variable(torch.normal(torch.zeros(z_size),torch.ones(z_size)).cuda())
else:
N = Variable(torch.normal(torch.zeros(z_size),torch.ones(z_size)))
z = mu + sigma*N
# mu and sigma_hat are needed for LKL loss
return z, mu, sigma_hat
def make_target(self, batch, lengths):
if use_cuda:
eos = Variable(torch.stack([torch.Tensor([0,0,0,0,1])]\
*batch.size()[1]).cuda()).unsqueeze(0)
else:
eos = Variable(torch.stack([torch.Tensor([0,0,0,0,1])]\
*batch.size()[1])).unsqueeze(0)
batch = torch.cat([batch, eos], 0)
mask = torch.zeros(Nmax+1, batch.size()[1])
for indice,length in enumerate(lengths):
mask[:length,indice] = 1
if use_cuda:
mask = Variable(mask.cuda()).detach()
else:
mask = Variable(mask).detach()
dx = torch.stack([Variable(batch.data[:,:,0])]*hp.M,2).detach()
dy = torch.stack([Variable(batch.data[:,:,1])]*hp.M,2).detach()
p1 = Variable(batch.data[:,:,2]).detach()
p2 = Variable(batch.data[:,:,3]).detach()
p3 = Variable(batch.data[:,:,4]).detach()
p = torch.stack([p1,p2,p3],2)
return mask,dx,dy,p
def select_last(inputs, lengths, hidden_size):
"""
:param inputs: [T * B * D] D = 2 * hidden_size
:param lengths: [B]
:param hidden_size: dimension
:return: [B * D]
"""
batch_size = inputs.size(1)
batch_out_list = []
for b in range(batch_size):
batch_out_list.append(torch.cat((inputs[lengths[b] - 1, b, :hidden_size],
inputs[0, b, hidden_size:])
)
)
out = torch.stack(batch_out_list)
return out
def pack_to_matching_matrix(s1, s2, cat_only=[False, False]):
t1 = s1.size(0)
t2 = s2.size(0)
batch_size = s1.size(1)
d = s1.size(2)
expanded_p_s1 = s1.expand(t2, t1, batch_size, d)
expanded_p_s2 = s2.view(t2, 1, batch_size, d)
expanded_p_s2 = expanded_p_s2.expand(t2, t1, batch_size, d)
if not cat_only[0] and not cat_only[1]:
matrix = torch.cat((expanded_p_s1, expanded_p_s2), dim=3)
elif not cat_only[0] and cat_only[1]:
matrix = torch.cat((expanded_p_s1, expanded_p_s2, expanded_p_s1 * expanded_p_s2), dim=3)
else:
matrix = torch.cat((expanded_p_s1,
expanded_p_s2,
torch.abs(expanded_p_s1 - expanded_p_s2),
expanded_p_s1 * expanded_p_s2), dim=3)
# matrix = torch.cat((expanded_p_s1,
# expanded_p_s2), dim=3)
return matrix
def expand_z_where(z_where):
# Take a batch of three-vectors, and massages them into a batch of
# 2x3 matrices with elements like so:
# [s,x,y] -> [[s,0,x],
# [0,s,y]]
n = z_where.size(0)
out = torch.cat((ng_zeros([1, 1]).type_as(z_where).expand(n, 1), z_where), 1)
ix = Variable(expansion_indices)
if z_where.is_cuda:
ix = ix.cuda()
out = torch.index_select(out, 1, ix)
out = out.view(n, 2, 3)
return out
# Scaling by `1/scale` here is unsatisfactory, as `scale` could be
# zero.
def forward(self, *inputs):
dim = inputs[0].dim()
assert_(dim in [4, 5],
'Input tensors must either be 4 or 5 '
'dimensional, but inputs[0] is {}D.'.format(dim),
ShapeError)
# Get resize function
spatial_dim = {4: 2, 5: 3}[dim]
resize_function = getattr(F, 'adaptive_{}_pool{}d'.format(self.pool_mode,
spatial_dim))
target_size = pyu.as_tuple_of_len(self.target_size, spatial_dim)
# Do the resizing
resized_inputs = []
for input_num, input in enumerate(inputs):
# Make sure the dim checks out
assert_(input.dim() == dim,
"Expected inputs[{}] to be a {}D tensor, got a {}D "
"tensor instead.".format(input_num, dim, input.dim()),
ShapeError)
resized_inputs.append(resize_function(input, target_size))
# Concatenate along the channel axis
concatenated = torch.cat(tuple(resized_inputs), 1)
# Done
return concatenated
def __init__(self, shared_resources: SharedResources):
super(FastQAPyTorchModule, self).__init__()
self._shared_resources = shared_resources
input_size = shared_resources.config["repr_dim_input"]
size = shared_resources.config["repr_dim"]
self._size = size
self._with_char_embeddings = self._shared_resources.config.get("with_char_embeddings", False)
# modules & parameters
if self._with_char_embeddings:
self._conv_char_embedding = embedding.ConvCharEmbeddingModule(
len(shared_resources.char_vocab), size)
self._embedding_projection = nn.Linear(size + input_size, size)
self._embedding_highway = Highway(size, 1)
self._v_wiq_w = nn.Parameter(torch.ones(1, 1, input_size + size))
input_size = size
else:
self._v_wiq_w = nn.Parameter(torch.ones(1, 1, input_size))
self._bilstm = BiLSTM(input_size + 2, size)
self._answer_layer = FastQAAnswerModule(shared_resources)
# [size, 2 * size]
self._question_projection = nn.Parameter(torch.cat([torch.eye(size), torch.eye(size)], dim=1))
self._support_projection = nn.Parameter(torch.cat([torch.eye(size), torch.eye(size)], dim=1))
def backward(ctx, grad_outputs):
size = grad_outputs.size(1)
segm_sorted = torch.sort(ctx.rev_segm_sorted)[1]
grad_outputs = torch.index_select(grad_outputs, 0, segm_sorted)
offset = [ctx.num_zeros]
def backward_segment(l, n):
segment_grad = grad_outputs.narrow(0, offset[0], n // l)
if l > 1:
segment_grad = _MyMax.backward(ctx.maxes[l], segment_grad)[0].view(n, size)
offset[0] += n // l
return segment_grad
segment_grads = [backward_segment(l, n) for l, n in enumerate(ctx.num_lengths) if n > 0]
grads = torch.cat(segment_grads, 0)
rev_length_sorted = torch.sort(ctx.lengths_sorted)[1]
grads = torch.index_select(grads, 0, rev_length_sorted)
return grads, None, None, None
def forward(self, sent1_idx, sent2_idx, ext_feats=None):
# Select embedding
sent1 = self.embedding(sent1_idx).transpose(1, 2)
sent2 = self.embedding(sent2_idx).transpose(1, 2)
# Sentence modeling module
sent1_block_a, sent1_block_b = self._get_blocks_for_sentence(sent1)
sent2_block_a, sent2_block_b = self._get_blocks_for_sentence(sent2)
# Similarity measurement layer
feat_h = self._algo_1_horiz_comp(sent1_block_a, sent2_block_a)
feat_v = self._algo_2_vert_comp(sent1_block_a, sent2_block_a, sent1_block_b, sent2_block_b)
combined_feats = [feat_h, feat_v, ext_feats] if self.ext_feats else [feat_h, feat_v]
feat_all = torch.cat(combined_feats, dim=1)
preds = self.final_layers(feat_all)
return preds
def __call__(self, batch):
images, labels = zip(*batch)
imgH = self.imgH
imgW = self.imgW
if self.keep_ratio:
ratios = []
for image in images:
w, h = image.size
ratios.append(w / float(h))
ratios.sort()
max_ratio = ratios[-1]
imgW = int(np.floor(max_ratio * imgH))
imgW = max(imgH * self.min_ratio, imgW) # assure imgH >= imgW
transform = resizeNormalize((imgW, imgH))
images = [transform(image) for image in images]
images = torch.cat([t.unsqueeze(0) for t in images], 0)
return images, labels
def prepare_batches(self, batch_data, chunks, **kwargs):
x, x_lens, ys, ys_lens = batch_data
batch_dim = 0 if self.batch_first else 1
x_list = x.chunk(chunks, 0)
x_lens_list = x_lens.chunk(chunks, 0)
ys_list = ys.chunk(chunks, batch_dim)
ys_lens_list = ys_lens.chunk(chunks, batch_dim)
inp_list = [x_list, x_lens_list, ys_list, ys_lens_list]
data_list = []
for inp in zip(*inp_list):
data = self.prepare_batch(inp, **kwargs)
data_list.append(data)
data_list = list(zip(*data_list))
ret_list = []
for data in data_list:
data = [d.unsqueeze(0) for d in data]
data = torch.cat(data)
ret_list.append(data)
return ret_list
def forward(self, x):
en0 = self.c0(x)
en1 = self.bnc1(self.c1(F.leaky_relu(en0, negative_slope=0.2)))
en2 = self.bnc2(self.c2(F.leaky_relu(en1, negative_slope=0.2)))
en3 = self.bnc3(self.c3(F.leaky_relu(en2, negative_slope=0.2)))
en4 = self.bnc4(self.c4(F.leaky_relu(en3, negative_slope=0.2)))
en5 = self.bnc5(self.c5(F.leaky_relu(en4, negative_slope=0.2)))
en6 = self.bnc6(self.c6(F.leaky_relu(en5, negative_slope=0.2)))
en7 = self.c7(F.leaky_relu(en6, negative_slope=0.2))
de7 = self.bnd7(self.d7(F.relu(en7)))
de6 = F.dropout(self.bnd6(self.d6(F.relu(torch.cat((en6, de7),1)))))
de5 = F.dropout(self.bnd5(self.d5(F.relu(torch.cat((en5, de6),1)))))
de4 = F.dropout(self.bnd4(self.d4(F.relu(torch.cat((en4, de5),1)))))
de3 = self.bnd3(self.d3(F.relu(torch.cat((en3, de4),1))))
de2 = self.bnd2(self.d2(F.relu(torch.cat((en2, de3),1))))
de1 = self.bnd1(self.d1(F.relu(torch.cat((en1, de2),1))))
de0 = F.tanh(self.d0(F.relu(torch.cat((en0, de1),1))))
return de0
def discount(rewards, gamma):
tensor = False
if not isinstance(rewards, list):
tensor = True
rewards = rewards.split(1)
R = 0.0
discounted = []
for r in rewards[::-1]:
R = r + gamma * R
discounted.insert(0, R)
if tensor:
return th.cat(discounted).view(-1)
return T(discounted)
def generalized_advantage_estimations(rewards, values, terminal=None, gamma=0.99, tau=0.95):
gae = 0.0
advantages = []
values = th.cat([values, V(T([0.0077]))])
for i in reversed(range(len(rewards))):
nonterminal = 1.0 - terminal[i]
delta = rewards[i] + gamma * values[i+1] * nonterminal - values[i]
gae = delta + gamma * tau * gae * nonterminal
advantages.insert(0, gae + values[i])
return th.cat(advantages)