- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个来自 torchtext
的 Bucketiterator
,我将它提供给 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
或 torchtext
似乎没有执行此操作的任何功能。
最佳答案
def get_one_hot_torch_tensor(in_tensor):"""函数将 1d 或 2d torch 张量转换为单热编码"""
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)
关于python - pytorch/torchtext 中的单热编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944018/
我有一个数据框,其中有两列(评论和情绪)。我正在使用 pytorch 和 torchtext 库来预处理数据。是否可以使用 dataframe 作为源来读取 torchtext 中的数据?我正在寻找类
我的机器上安装了 PyTorch,但每当我尝试执行以下操作时 - from torchtext import data from torchtext import datasets 我收到以下错误。
torchtext 能够读取包含一些列的文件,每一列对应一个字段。如果我想创建一个新列(我将其用作一项功能)怎么办?例如,假设文件有两列,文本和目标,我想从文本中提取一些信息并生成一个新特征(例如,如
我有一个来自 torchtext 的 Bucketiterator,我将它提供给 pytorch 中的模型。如何构造迭代器的示例: train_iter, val_iter = BucketItera
我正在处理文本并使用 torchtext.data.Dataset .创建数据集需要花费大量时间。对于只是运行程序,这仍然是可以接受的。但我想调试神经网络的 torch 代码。如果 python 在
我在 torchtext 中遇到了一个问题,并为此苦苦挣扎了很长时间。我试图使用 torchtext 和 spacy 对文本进行标记和数字化。我将我的分词器定义如下: def Sp_Tokenizer
我正在尝试使用 torchtext 进行序列标记,其中序列是句子。因此,每个 Example 都是句子及其标签的列表: [ { 'texts': ["An example.", "It's
据我所知,从 torchtext 0.9.0 开始,torchtext.data 和 torchtext.dataset 被移动到 torchtext.legacy 但我的 0.12.0 torcht
torchtext.data.TabularDataset 可以从 TSV/JSON/CSV 文件创建,然后可用于从 Glove、FastText 或任何其他嵌入构建词汇表。但我的要求是直接从 lis
使用 pytorch 的 torchtext 构建文本分类模型。词汇表对象在 data.field 中: def create_tabularDataset_object(self,csv_path)
我最近开始使用 PyTorch 进行 ML/DL。以下 pytorch 示例说明了我们如何训练一个简单的模型来将德语翻译成英语。 https://pytorch.org/tutorials/begin
我想用 pytorch 从 tsv 文件创建一个数据集。 我正在考虑使用 torchtext.data.TabularDataset.splits 但我收到一条错误消息。 AttributeError
我想运行一个 git project使用 pytorch 和 torchtext 但是当我运行它时,它会引发错误: File "main.py", line 60, in main()
torchtext 0.4.0 库存在(可以通过 pip 下载),但是 conda install torchtext=0.4.0不管用。如何将 torchtext 下载到 anaconda 环境?
看起来之前声明字段、示例和使用 BucketIterator 的范式已被弃用,并将在 0.8 中转移到旧版。但是,我似乎无法找到不使用 Field 的自定义数据集的新范式示例(例如,不是 torch.
我正在使用 RNN 并使用 Pytorch 和 Torchtext。我在 RNN 中构建词汇时遇到问题。我的代码如下: TEXT = Field(tokenize=tokenizer, lower=T
如何在pytorrch中读入.txt文件(语料库)到torchtext? 我只看到 data.Dataset 的示例数据集和 data.TabularData 的 csv、json 和 tsv。 ht
当我尝试查看批处理时,通过打印 BucketIterator 对象的下一次迭代,抛出了 AttributeError。 tv_datafields=[("Tweet",TEXT), ("Anger",
我有 64 位 Windows 10 操作系统我已经安装了python 3.6.8我已经使用 pip 安装了 torch 和 torchtext。手电筒版本为1.2.0 我正在尝试使用以下代码加载 A
我是一名优秀的程序员,十分优秀!