首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pytorch/torchtext中的One-hot编码

pytorch/torchtext中的One-hot编码
EN

Stack Overflow用户
提问于 2019-07-09 08:36:12
回答 1查看 5.3K关注 0票数 3

我有一个来自torchtextBucketiterator,我将其提供给pytorch中的一个模型。如何构造迭代器的一个示例:

代码语言:javascript
复制
train_iter, val_iter = BucketIterator.splits((train,val),
                                             batch_size=batch_size,
                                             sort_within_batch = True, 
                                             device = device, 
                                             shuffle=True, 
                                             sort_key=lambda x: (len(x.src), len(x.trg)))

然后将数据提供给类似这样的模型,在该模型中我使用nn.Embedding层。

代码语言:javascript
复制
class encoder(nn.Module):
    def __init__(self, input_dim, emb_dim, hid_dim, n_layers, dropout):
        super().__init__()

        self.input_dim = input_dim
        self.emb_dim = emb_dim
        self.hid_dim = hid_dim
        self.n_layers = n_layers
        self.dropout = dropout

        self.embedding = nn.Embedding(input_dim, emb_dim)

        self.rnn = nn.LSTM(emb_dim, hid_dim, n_layers, dropout = dropout)

        self.dropout = nn.Dropout(dropout)

    def forward(self, src):

        #src = [src sent len, batch size]

        embedded = self.dropout(self.embedding(src))

        #embedded = [src sent len, batch size, emb dim]
        hidden_enc = []
        outputs, hidden = self.rnn(embedded[0,:,:].unsqueeze(0))
        for i in range(1,len(embedded[:,1,1])):
            outputs, hidden = self.rnn(embedded[i,:,:].unsqueeze(0),hidden)
            hidden_cpu = []
            for k in range(len(hidden)):
                hidden_cpu.append(hidden[k])
                hidden_cpu[k] = hidden[k].cpu()
            hidden_enc.append(tuple(hidden_cpu))



        #outputs, hidden = self.rnn(embedded)

        #outputs = [src sent len, batch size, hid dim * n directions]
        #hidden = [n layers * n directions, batch size, hid dim]
        #cell = [n layers * n directions, batch size, hid dim]
        None
        #outputs are always from the top hidden layer

        return hidden, hidden_enc

但是,如果我希望嵌入是独一无二的编码呢?我从事形式语言的工作,如果能保持标记之间的正交性就好了。看起来pytorchtorchtext没有任何功能可以做到这一点。

EN

回答 1

Stack Overflow用户

发布于 2020-08-30 02:33:32

def get_one_hot_torch_tensor(in_tensor):“函数将一维或二维火炬张量转换为单热编码”

代码语言:javascript
复制
n_channels = torch.max(in_tensor)+1  # maximum number of channels
if in_tensor.ndim == 2:
    out_one_hot = torch.zeros((n_channels, in_tensor.shape[0], in_tensor.shape[1]))
    # print(out_one_hot)
    index = np.indices((in_tensor.shape[0], in_tensor.shape[1]))  # create an array of indices
    x, y = index[0], index[1]
    print(x, y)

    out_one_hot[in_tensor, x, y] = 1
    print(out_one_hot)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56944018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档