当前位置: 首页>>代码示例>>Python>>正文


Python Variable.index_select方法代码示例

本文整理汇总了Python中torch.autograd.Variable.index_select方法的典型用法代码示例。如果您正苦于以下问题:Python Variable.index_select方法的具体用法?Python Variable.index_select怎么用?Python Variable.index_select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torch.autograd.Variable的用法示例。


在下文中一共展示了Variable.index_select方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _pack_and_unpack_lstm

# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import index_select [as 别名]
    def _pack_and_unpack_lstm(cls, input, lengths, seq_encoder):
        """
        LSTM, when using batches, should be called with a PackedSequence.
        Doing this will deal with the different lengths in the batch.
        PackedSequence must be created from batches where the sequences are
        stored with decreasing lengths.

        _pack_and_unpack_lstm handles this issue.
        It re-orders its input, pack it, sends it through the LSTM and finally
        restore the original order.

        This is not general purpose: in particular, it does not handle initial
        and final states.
        """
        s_lengths, indexes = lengths.sort(0, descending=True)
        s_input = input.index_select(0, indexes)

        i_range = torch.arange(lengths.size()[0]).type_as(lengths.data)
        i_range = Variable(i_range)
        _, reverses = indexes.sort(0, descending=False)
        reverses = i_range.index_select(0, reverses)

        packed = nn.utils.rnn.pack_padded_sequence(
            s_input, s_lengths.data.tolist(), batch_first=True)

        output, _ = seq_encoder(packed)
        # Unpack and apply reverse index.
        output, _ = nn.utils.rnn.pad_packed_sequence(
            output, batch_first=True)
        output = output.index_select(0, reverses)

        return output
开发者ID:zhouyonglong,项目名称:MSMARCOV2,代码行数:34,代码来源:bidaf.py

示例2: train_online

# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import index_select [as 别名]
def train_online(online_net, online_optimizer, loss_fn, pos_feats, neg_feats, maxiter, in_layer='fc'):
    online_net.train()
    
    batch_pos = opts['batch_pos']
    batch_neg = opts['batch_neg']
    batch_test = opts['batch_test']
    batch_neg_cand = max(opts['batch_neg_cand'], batch_neg)

    pos_idx = np.random.permutation(pos_feats.size(0))
    neg_idx = np.random.permutation(neg_feats.size(0))
    while(len(pos_idx) < batch_pos*maxiter):
        pos_idx = np.concatenate([pos_idx, np.random.permutation(pos_feats.size(0))])
    while(len(neg_idx) < batch_neg_cand*maxiter):
        neg_idx = np.concatenate([neg_idx, np.random.permutation(neg_feats.size(0))])
    pos_pointer = 0
    neg_pointer = 0

    for iter in range(maxiter):
        # select pos idx
        pos_next = pos_pointer+batch_pos
        pos_cur_idx = pos_idx[pos_pointer:pos_next]
        pos_cur_idx = pos_feats.new(pos_cur_idx.astype(float)).long()
        pos_pointer = pos_next
        # select neg idx
        neg_next = neg_pointer+batch_neg_cand
        neg_cur_idx = neg_idx[neg_pointer:neg_next]
        neg_cur_idx = neg_feats.new(neg_cur_idx.astype(float)).long()
        neg_pointer = neg_next
        # create batch
        batch_pos_feats = Variable(pos_feats.index_select(0, pos_cur_idx))
        batch_neg_feats = Variable(neg_feats.index_select(0, neg_cur_idx))
        # hard negative mining
        if batch_neg_cand > batch_neg:
            online_net.eval()
            for start in range(0,batch_neg_cand,batch_test):
                end = min(start+batch_test,batch_neg_cand)
                score = online_net.forward(batch_neg_feats[start:end], in_layer=in_layer)
                if start==0:
                    neg_cand_score = score.data[:,1].clone()
                else:
                    neg_cand_score = torch.cat((neg_cand_score, score.data[:,1].clone()),0)

            _, top_idx = neg_cand_score.topk(batch_neg)
            batch_neg_feats = batch_neg_feats.index_select(0, Variable(top_idx))
            online_net.train()
        # forward
        pos_score = online_net.forward(batch_pos_feats, in_layer=in_layer)
        neg_score = online_net.forward(batch_neg_feats, in_layer=in_layer)
        loss = loss_fn(pos_score,neg_score)
        online_net.zero_grad() 
        loss.backward()
        torch.nn.utils.clip_grad_norm(online_net.parameters(), opts['grad_clip'])
        online_optimizer.step()
    
    # testing
    pos_score = online_net.forward(batch_pos_feats, in_layer=in_layer)
    neg_score = online_net.forward(batch_neg_feats, in_layer=in_layer)
    after_loss = loss_fn(pos_score,neg_score)
    print("loss:%.4f, after loss:%.4f"%(loss.data[0], after_loss.data[0]))
开发者ID:XHWXD,项目名称:meta_trackers,代码行数:61,代码来源:run_tracker.py

示例3: sort_batch_by_length

# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import index_select [as 别名]
def sort_batch_by_length(tensor: torch.autograd.Variable, sequence_lengths: torch.autograd.Variable):
    """
    Sort a batch first tensor by some specified lengths.

    Parameters
    ----------
    tensor : Variable(torch.FloatTensor), required.
        A batch first Pytorch tensor.
    sequence_lengths : Variable(torch.LongTensor), required.
        A tensor representing the lengths of some dimension of the tensor which
        we want to sort by.

    Returns
    -------
    sorted_tensor : Variable(torch.FloatTensor)
        The original tensor sorted along the batch dimension with respect to sequence_lengths.
    sorted_sequence_lengths : Variable(torch.LongTensor)
        The original sequence_lengths sorted by decreasing size.
    restoration_indices : Variable(torch.LongTensor)
        Indices into the sorted_tensor such that
        ``sorted_tensor.index_select(0, restoration_indices) == original_tensor``
    """

    if not isinstance(tensor, Variable) or not isinstance(sequence_lengths, Variable):
        raise ConfigurationError("Both the tensor and sequence lengths must be torch.autograd.Variables.")

    sorted_sequence_lengths, permutation_index = sequence_lengths.sort(0, descending=True)
    sorted_tensor = tensor.index_select(0, permutation_index)

    # This is ugly, but required - we are creating a new variable at runtime, so we
    # must ensure it has the correct CUDA vs non-CUDA type. We do this by cloning and
    # refilling one of the inputs to the function.
    index_range = sequence_lengths.data.clone().copy_(torch.arange(0, len(sequence_lengths)))
    # This is the equivalent of zipping with index, sorting by the original
    # sequence lengths and returning the now sorted indices.
    index_range = Variable(index_range.long())
    _, reverse_mapping = permutation_index.sort(0, descending=False)
    restoration_indices = index_range.index_select(0, reverse_mapping)
    return sorted_tensor, sorted_sequence_lengths, restoration_indices
开发者ID:cyzhangAThit,项目名称:GLUE-baselines,代码行数:41,代码来源:util.py

示例4: train

# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import index_select [as 别名]
    def train(self):

        if self.T - self.target_sync_T > self.args.target:
            self.sync_target_network()
            self.target_sync_T = self.T

        info = {}

        for _ in range(self.args.iters):
            self.dqn.eval()

            batch, indices, is_weights = self.replay.Sample_N(self.args.batch_size, self.args.n_step, self.args.gamma)
            columns = list(zip(*batch))

            states = Variable(torch.from_numpy(np.array(columns[0])).float().transpose_(1, 3))
            actions = Variable(torch.LongTensor(columns[1]))
            terminal_states = Variable(torch.FloatTensor(columns[5]))
            rewards = Variable(torch.FloatTensor(columns[2]))
            # Have to clip rewards for DQN
            rewards = torch.clamp(rewards, -1, 1)
            steps = Variable(torch.FloatTensor(columns[4]))
            new_states = Variable(torch.from_numpy(np.array(columns[3])).float().transpose_(1, 3))

            target_dqn_qvals = self.target_dqn(new_states).cpu()
            # Make a new variable with those values so that these are treated as constants
            target_dqn_qvals_data = Variable(target_dqn_qvals.data)

            q_value_gammas = (Variable(torch.ones(terminal_states.size()[0])) - terminal_states)
            inter = Variable(torch.ones(terminal_states.size()[0]) * self.args.gamma)
            # print(steps)
            q_value_gammas = q_value_gammas * torch.pow(inter, steps)

            values = torch.linspace(self.args.v_min, self.args.v_max, steps=self.args.atoms)
            values = Variable(values)
            values = values.view(1, 1, self.args.atoms)
            values = values.expand(self.args.batch_size, self.args.actions, self.args.atoms)
            # print(values)

            q_value_gammas = q_value_gammas.view(self.args.batch_size, 1, 1)
            q_value_gammas = q_value_gammas.expand(self.args.batch_size, self.args.actions, self.args.atoms)
            # print(q_value_gammas)
            gamma_values = q_value_gammas * values
            # print(gamma_values)
            rewards = rewards.view(self.args.batch_size, 1, 1)
            rewards = rewards.expand(self.args.batch_size, self.args.actions, self.args.atoms)
            # print(rewards)
            operator_q_values = rewards + gamma_values
            # print(operator_q_values)

            clipped_operator_q_values = torch.clamp(operator_q_values, self.args.v_min, self.args.v_max)

            delta_z = (self.args.v_max - self.args.v_min) / (self.args.atoms - 1)
            # Using the notation from the categorical paper
            b_j = (clipped_operator_q_values - self.args.v_min) / delta_z
            # print(b_j)
            lower_bounds = torch.floor(b_j)
            upper_bounds = torch.ceil(b_j)

            # Work out the max action
            atom_values = Variable(torch.linspace(self.args.v_min, self.args.v_max, steps=self.args.atoms))
            atom_values = atom_values.view(1, 1, self.args.atoms)
            atom_values = atom_values.expand(self.args.batch_size, self.args.actions, self.args.atoms)

            # Sum over the atoms dimension
            target_expected_qvalues = torch.sum(target_dqn_qvals_data * atom_values, dim=2)
            # Get the maximum actions index across the batch size
            max_actions = target_expected_qvalues.max(dim=1)[1].view(-1)

            # Project back onto the original support for the max actions
            q_value_distribution_targets = torch.zeros(self.args.batch_size, self.args.atoms)

            # Distributions for the max actions
            # print(target_dqn_qvals_data, max_actions)
            q_value_max_actions_distribs = target_dqn_qvals_data.index_select(dim=1, index=max_actions)[:,0,:]
            # print(q_value_max_actions_distribs)

            # Lower_bounds_actions
            lower_bounds_actions = lower_bounds.index_select(dim=1, index=max_actions)[:,0,:]
            upper_bounds_actions = upper_bounds.index_select(dim=1, index=max_actions)[:,0,:]
            b_j_actions = b_j.index_select(dim=1, index=max_actions)[:,0,:]

            lower_bound_values_to_add = q_value_max_actions_distribs * (upper_bounds_actions - b_j_actions)
            upper_bound_values_to_add = q_value_max_actions_distribs * (b_j_actions - lower_bounds_actions)
            # print(lower_bounds_actions)
            # print(lower_bound_values_to_add)
            # Naive looping
            for b in range(self.args.batch_size):
                for l, pj in zip(lower_bounds_actions.data.type(torch.LongTensor)[b], lower_bound_values_to_add[b].data):
                    q_value_distribution_targets[b][l] += pj
                for u, pj in zip(upper_bounds_actions.data.type(torch.LongTensor)[b], upper_bound_values_to_add[b].data):
                    q_value_distribution_targets[b][u] += pj

            self.dqn.train()
            if self.args.gpu:
                actions = actions.cuda()
                # q_value_targets = q_value_targets.cuda()
                q_value_distribution_targets = q_value_distribution_targets.cuda()
            model_predictions = self.dqn(states).index_select(1, actions.view(-1))[:,0,:]
            q_value_distribution_targets = Variable(q_value_distribution_targets)
            # print(q_value_distribution_targets)
#.........这里部分代码省略.........
开发者ID:tabzraz,项目名称:RL,代码行数:103,代码来源:DQN_Distribution_Agent.py

示例5: forward

# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import index_select [as 别名]
    def forward(self, word_seq, pos_seq, rel_pos, sent_len, token_index_path, depend_path,
                rel_token_index_path, rel_depend_path, path_len, rel_path_len):
        # sort the input sequences by length
        sent_len, sent_len_sort_idx = torch.sort(sent_len, descending=True)
        _, sent_len_unsort_idx = torch.sort(sent_len_sort_idx)

        word_seq = word_seq.index_select(0, sent_len_sort_idx)
        pos_seq = pos_seq.index_select(0, sent_len_sort_idx)
        token_index_path = token_index_path.index_select(0, sent_len_sort_idx)
        depend_path = depend_path.index_select(0, sent_len_sort_idx)
        rel_token_index_path = rel_token_index_path.index_select(0, sent_len_sort_idx)
        rel_depend_path = rel_depend_path.index_select(0, sent_len_sort_idx)
        path_len = path_len.index_select(0, sent_len_sort_idx)
        rel_path_len = rel_path_len.index_select(0, sent_len_sort_idx)

        # Global Context Representation
        word_lex_repr = self.word_repr(word_seq, pos_seq)
        #print(word_lex_repr.size())
        packed_word_lex_repr = pack_padded_sequence(word_lex_repr, sent_len.cpu().data.numpy(), batch_first=True)
        hidden = self.init_hidden(len(sent_len), self.config['gcr_num_layers'] * 2, self.config['gcr_hidden_size'])
        packed_gcr, hidden = self.global_context_repr(packed_word_lex_repr, hidden)
        gcr, _ = pad_packed_sequence(packed_gcr, batch_first=True)

        # generate token and pos path by index path
        token_path = word_seq.unsqueeze(1).expand_as(token_index_path).gather(2, token_index_path)
        pos_path = pos_seq.unsqueeze(1).expand_as(token_index_path).gather(2, token_index_path)
        rel_token_path = word_seq.unsqueeze(1).expand_as(rel_token_index_path).gather(2, rel_token_index_path)
        rel_pos_path = pos_seq.unsqueeze(1).expand_as(rel_token_index_path).gather(2, rel_token_index_path)

        # sort the path sequences by length
        path_len = path_len.view(-1)
        rel_path_len = rel_path_len.view(-1)
        token_path = token_path.view(token_path.size(0) * token_path.size(1), token_path.size(2))
        pos_path = pos_path.view(pos_path.size(0) * pos_path.size(1), pos_path.size(2))
        depend_path = depend_path.view(depend_path.size(0) * depend_path.size(1), depend_path.size(2))
        rel_pos_path = rel_pos_path.view(rel_pos_path.size(0) * rel_pos_path.size(1), rel_pos_path.size(2))
        rel_token_path = rel_token_path.view(rel_token_path.size(0) * rel_token_path.size(1), rel_token_path.size(2))
        rel_depend_path = rel_depend_path.view(rel_depend_path.size(0) * rel_depend_path.size(1),
                                               rel_depend_path.size(2))

        path_len, path_len_sort_idx = torch.sort(path_len, descending=True)
        _, path_len_unsort_idx = torch.sort(path_len_sort_idx)
        rel_path_len, rel_path_len_sort_idx = torch.sort(rel_path_len, descending=True)
        _, rel_path_len_unsort_idx = torch.sort(rel_path_len_sort_idx)

        token_path = token_path.index_select(0, path_len_sort_idx)
        pos_path = pos_path.index_select(0, path_len_sort_idx)
        depend_path = depend_path.index_select(0, path_len_sort_idx)
        rel_token_path = rel_token_path.index_select(0, rel_path_len_sort_idx)
        rel_pos_path = rel_pos_path.index_select(0, rel_path_len_sort_idx)
        rel_depend_path = rel_depend_path.index_select(0, rel_path_len_sort_idx)

        # cut out the items with length 0
        former_len = path_len.size(0)
        num_all_paths = int(sum(sent_len))
        path_len = path_len.narrow(0, 0, num_all_paths)
        rel_path_len = rel_path_len.narrow(0, 0, num_all_paths)
        token_path = token_path.narrow(0, 0, num_all_paths)
        pos_path = pos_path.narrow(0, 0, num_all_paths)
        depend_path = depend_path.narrow(0, 0, num_all_paths)
        rel_token_path = rel_token_path.narrow(0, 0, num_all_paths)
        rel_pos_path = rel_pos_path.narrow(0, 0, num_all_paths)
        rel_depend_path = rel_depend_path.narrow(0, 0, num_all_paths)

        # Syntactic Path Representation
        token_path_embedding = self.word_repr(token_path, pos_path)
        depend_path_embedding = self.depend_embedding(depend_path)
        rel_token_path_embedding = self.word_repr(rel_token_path, rel_pos_path)
        rel_depend_path_embedding = self.depend_embedding(rel_depend_path)

        packed_token_path_embedding = pack_padded_sequence(token_path_embedding, path_len.cpu().data.numpy(),
                                                           batch_first=True)
        packed_depend_path_embedding = pack_padded_sequence(depend_path_embedding, path_len.cpu().data.numpy(),
                                                            batch_first=True)
        packed_rel_token_path_embedding = pack_padded_sequence(rel_token_path_embedding, rel_path_len.cpu().data.numpy(),
                                                               batch_first=True)
        packed_rel_depend_path_embedding = pack_padded_sequence(rel_depend_path_embedding, rel_path_len.cpu().data.numpy(),
                                                                batch_first=True)

        hidden = self.init_hidden(num_all_paths, self.config['gpr_num_layers'], self.config['gpr_hidden_size'])
        packed_token_path_repr, hidden = self.generic_path_repr_forward(packed_token_path_embedding, hidden)
        hidden = self.init_hidden(num_all_paths, self.config['rpr_num_layers'], self.config['rpr_hidden_size'])
        packed_depend_path_repr, hidden = self.relation_path_repr_forward(packed_depend_path_embedding, hidden)
        hidden = self.init_hidden(num_all_paths, self.config['gpr_num_layers'], self.config['gpr_hidden_size'])
        packed_rel_token_path_repr, hidden = self.generic_path_repr_backward(packed_rel_token_path_embedding, hidden)
        hidden = self.init_hidden(num_all_paths, self.config['rpr_num_layers'], self.config['rpr_hidden_size'])
        packed_rel_depend_path_repr, hidden = self.relation_path_repr_backward(packed_rel_depend_path_embedding, hidden)

        token_path_repr, _ = pad_packed_sequence(packed_token_path_repr, batch_first=True)
        depend_path_repr, _ = pad_packed_sequence(packed_depend_path_repr, batch_first=True)
        rel_token_path_repr, _ = pad_packed_sequence(packed_rel_token_path_repr, batch_first=True)
        rel_depend_path_repr, _ = pad_packed_sequence(packed_rel_depend_path_repr, batch_first=True)

        # select last hidden layer as feature
        token_path_repr = token_path_repr[range(0, token_path_repr.size(0)), path_len - 1, :]
        depend_path_repr = depend_path_repr[range(0, depend_path_repr.size(0)), path_len - 1, :]
        rel_token_path_repr = rel_token_path_repr[range(0, rel_token_path_repr.size(0)), path_len - 1, :]
        rel_depend_path_repr = rel_depend_path_repr[range(0, rel_depend_path_repr.size(0)), path_len - 1, :]

        gpr = torch.cat([token_path_repr, depend_path_repr], dim=-1)
#.........这里部分代码省略.........
开发者ID:ParkTong,项目名称:Unified-Architecture-for-Semantic-Role-Labeling-and-Relation-Classification,代码行数:103,代码来源:model.py


注:本文中的torch.autograd.Variable.index_select方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。