我有一个形状为的张量torch.Size([4, 161, 325])
。如何删除沿 dim=2 的第一个元素,以便生成的张量的形状为torch.Size([4, 161, 324])
?
我想应用 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, …
我不明白为什么我的 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) 在 PyTorch 中,torch.nn.function.embedding_bag 似乎是负责执行嵌入查找的实际工作的主要函数。在 PyTorch 的文档中,提到 embedding_bag 可以完成其工作 > 无需实例化中间嵌入。这究竟意味着什么?这是否意味着例如当模式为“sum”时它会进行就地求和?或者它只是意味着在调用 embedding_bag 时不会产生额外的张量,但仍然从系统的角度来看,所有中间行向量已经被提取到处理器中以用于计算最终张量?
现在我正在使用 训练模型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倍,这真的让我很困惑。
非常感谢您的帮助!
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
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) 我有一个简单的卷积网络:
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
层的权重产生影响?
我错过了什么吗?
例如,我定义了一个模型,如下所示:
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)
问题是线性层,我不想计算线性层输入的大小,但定义模型需要指定它。我怎么解决这个问题?
我正在尝试将微调 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)