pytorch / torchtext

时间:2019-07-09 00:36:12

标签: python machine-learning deep-learning pytorch torchtext

我有一个来自Bucketiterator的{​​{1}},我将其馈送到torchtext中的一个模型中。构造迭代器的示例:

pytorch

然后将数据馈送到这样的模型中,在这里我使用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

但是,如果我希望将嵌入内容一键编码怎么办?我使用的是正式语言,保留令牌之间的正交性会很好。 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 pytorch似乎没有执行此操作的功能。

1 个答案:

答案 0 :(得分:0)

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

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)