标签: pytorch

如何使用 PyTorch 从 3D 张量中删除元素?

我有一个形状为的张量torch.Size([4, 161, 325])。如何删除沿 dim=2 的第一个元素,以便生成的张量的形状为torch.Size([4, 161, 324])

python pytorch tensor

1
推荐指数
1
解决办法
3699
查看次数

Roberta 模型预处理文本中的混乱

我想应用 Roberta 模型来实现文本相似度。给定一对句子,输入的格式应为<s> A </s></s> B </s>。我想出了两种可能的方法来生成输入ID,即

A)

from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained('roberta-base')

list1 = tokenizer.encode('Very severe pain in hands')

list2 = tokenizer.encode('Numbness of upper limb')

sequence = list1+[2]+list2[1:]

Run Code Online (Sandbox Code Playgroud)

在这种情况下,顺序是[0, 12178, 3814, 2400, 11, 1420, 2, 2, 234, 4179, 1825, 9, 2853, 29654, 2]

b)

from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained('roberta-base')

list1 = tokenizer.encode('Very severe pain in hands', add_special_tokens=False)

list2 = tokenizer.encode('Numbness of upper limb', add_special_tokens=False)

sequence = [0]+list1+[2,2]+list2+[2]
Run Code Online (Sandbox Code Playgroud)

在这种情况下,顺序是[0, 25101, …

nlp pytorch bert-language-model huggingface-transformers

1
推荐指数
1
解决办法
1678
查看次数

BERT 编码层在评估期间为所有输入生成相同的输出 (PyTorch)

我不明白为什么我的 BERT 模型在评估期间返回相同的输出。我的模型在训练期间的输出似乎是正确的,因为值不同,但在评估期间却完全相同。 评估期间的输出

这是我的 BERT 模型类

class BERTBaseUncased(nn.Module):
    def __init__(self):
        super(BERTBaseUncased, self).__init__()
        self.bert = BertModel.from_pretrained("bert-base-uncased")
        self.bert_drop = nn.Dropout(0.3)
        self.out = nn.Linear(768, 4)

    def forward(self, ids, mask, token_type_ids):
        _, o2 = self.bert(ids, attention_mask=mask, token_type_ids=token_type_ids) # Use one of the outputs
        bo = self.bert_drop(o2)
        return self.out(bo)
Run Code Online (Sandbox Code Playgroud)

我的数据集类

class BERTDataset:
    def __init__(self, review, target, tokenizer, classes=4):
        self.review = review
        self.target = target
        self.tokenizer = tokenizer
        self.max_len = max_len
        self.classes = classes

    def __len__(self):
        return len(self.review)

    def __getitem__(self, item):
        review = str(self.review)
        review = …
Run Code Online (Sandbox Code Playgroud)

python nlp pre-trained-model pytorch bert-language-model

1
推荐指数
1
解决办法
2524
查看次数

embedding_bag 在 PyTorch 中的具体工作原理

在 PyTorch 中,torch.nn.function.embedding_bag 似乎是负责执行嵌入查找的实际工作的主要函数。在 PyTorch 的文档中,提到 embedding_bag 可以完成其工作 > 无需实例化中间嵌入。这究竟意味着什么?这是否意味着例如当模式为“sum”时它会进行就地求和?或者它只是意味着在调用 embedding_bag 时不会产生额外的张量,但仍然从系统的角度来看,所有中间行向量已经被提取到处理器中以用于计算最终张量?

embedding python-embedding neural-network torch pytorch

1
推荐指数
1
解决办法
4465
查看次数

PyTorch分布式训练时如何设置随机种子?

现在我正在使用 训练模型torch.distributed,但我不确定如何设置随机种子。例如,这是我当前的代码:

def main():
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    cudnn.enabled = True
    cudnn.benchmark = True
    cudnn.deterministic = True 

    mp.spawn(main_worker, nprocs=args.ngpus, args=(args,))
Run Code Online (Sandbox Code Playgroud)

我应该移动

    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    cudnn.enabled = True
    cudnn.benchmark = True
    cudnn.deterministic = True 
Run Code Online (Sandbox Code Playgroud)

进入函数main_worker()以确保每个进程都有正确的种子和 cudnn 设置?顺便说一句,我已经尝试过这个,这种行为会让训练速度慢2倍,这真的让我很困惑。

非常感谢您的帮助!

python parallel-processing distributed pytorch

1
推荐指数
1
解决办法
2764
查看次数

torch的torch.cat与tensorflow的等价物是什么?

def cxcy_to_xy(cxcy):
    """
    Convert bounding boxes from center-size coordinates (c_x, c_y, w, h) to boundary coordinates (x_min, y_min, x_max, y_max).

    :param cxcy: bounding boxes in center-size coordinates, a tensor of size (n_boxes, 4)
    :return: bounding boxes in boundary coordinates, a tensor of size (n_boxes, 4)
    """
    return torch.cat([cxcy[:, :2] - (cxcy[:, 2:] / 2),  # x_min, y_min
                      cxcy[:, :2] + (cxcy[:, 2:] / 2)], 1)  # x_max, y_max
Run Code Online (Sandbox Code Playgroud)

我想用tensorflow 2.0改变这个torch.cat

torch tensorflow pytorch

1
推荐指数
1
解决办法
3045
查看次数

TypeError: __call__() 需要 2 个位置参数,但给出了 3 个。通过迁移学习使用 FastRCNN 训练 Raccoon 预测模型

 from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
 from engine import train_one_epoch, evaluate
 import utils
 import torchvision.transforms as T

 num_epochs = 10
 for epoch in range(num_epochs):
    train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
    lr_scheduler.step()
    evaluate(model, data_loader_test, device=device)
Run Code Online (Sandbox Code Playgroud)

我使用的代码与此链接中提供的构建浣熊模型相同,但我的代码不起作用。

这是我在 () 中收到 TypeError Traceback(最近一次调用最后一次)的错误消息

  2 for epoch in range(num_epochs):

  3    # train for one epoch, printing every 10 iterations

  4   ----> train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)

  5     # update the learning rate

  6   lr_scheduler.step()
Run Code Online (Sandbox Code Playgroud)

7帧

getitem (self, idx)中

 29         target["iscrowd"] = iscrowd

 30 …
Run Code Online (Sandbox Code Playgroud)

image-processing computer-vision pytorch

1
推荐指数
1
解决办法
3005
查看次数

卷积层有多少个权值?

我有一个简单的卷积网络:

import torch.nn as nn 

class model(nn.Module):
def __init__(self, ks=1):
    super(model, self).__init__()
    self.conv1 = nn.Conv2d(in_channels=4, out_channels=32, kernel_size=ks, stride=1)

    self.fc1 = nn.Linear(8*8*32*ks, 64)
    self.fc2 = nn.Linear(64, 64)

def forward(self, x):
    x = F.relu(self.conv1(x))
    x = x.view(x.size(0), -1)
    x = F.relu(self.fc1(x))
    x = self.fc2(x)
    return x

cnn = model(1)
Run Code Online (Sandbox Code Playgroud)

由于内核大小为1且输出通道为32,我假设32*1*1该层中应该有权重。但是,当我询问pytorch权重矩阵的形状时cnn.conv1.weight.shape,它返回torch.Size([32, 4, 1, 1])。为什么输入通道的数量会对conv2d层的权重产生影响?

我错过了什么吗?

python conv-neural-network pytorch

1
推荐指数
1
解决办法
1042
查看次数

如何指定pytorch nn.Linear的输入维度?

例如,我定义了一个模型,如下所示:

class Net(nn.module):
   def __init__():
      self.conv11 = nn.Conv2d(in_channel,out1_channel,3)
      self.conv12 = nn.Conv2d(...)
      self.conv13 = nn.Conv2d(...)
      self.conv14 = nn.Conv2d(...)
      ...
      #Here is the point
      flat = nn.Flatten()
      #I don't want to compute the size of data after flatten, but I need a linear layer.

      fc_out = nn.Linear(???,out_dim)
Run Code Online (Sandbox Code Playgroud)

问题是线性层,我不想计算线性层输入的大小,但定义模型需要指定它。我怎么解决这个问题?

python deep-learning pytorch

1
推荐指数
1
解决办法
4737
查看次数

使用 SageMaker Pytorch 图像进行训练

我正在尝试将微调 BERT 模型的训练过程容器化,并在 SageMaker 上运行。我计划使用预构建的 SageMaker Pytorch GPU 容器 ( https://aws.amazon.com/releasenotes/available-deep-learning-containers-images/ ) 作为起点,但在提取图像时遇到问题我的构建过程。

我的 Dockerfile 如下所示:

# SageMaker PyTorch image
FROM 763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-training:1.5.0-gpu-py36-cu101-ubuntu16.04


ENV PATH="/opt/ml/code:${PATH}"

# /opt/ml and all subdirectories are utilized by SageMaker, we use the /code subdirectory to store our user code.
COPY /bert /opt/ml/code

# this environment variable is used by the SageMaker PyTorch container to determine our user code directory.
ENV SAGEMAKER_SUBMIT_DIRECTORY /opt/ml/code

# this environment variable is used by the SageMaker PyTorch container to determine our …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services pytorch amazon-sagemaker

1
推荐指数
1
解决办法
1993
查看次数