是否可以不使用dict,将这三个张量组合成一个大张量作为模型输入?

问题描述 投票:0回答:1

我有一个ANN网络,而不是我需要一个结合这三个张量的dict作为输入,例如,

model = MyNetwork("mypath")
dummy_input = {}
dummy_input["input_ids"] = torch.randint(1, 512,(1,345))
dummy_input["attention_mask"] = torch.randint(1, 512,(1,345))
dummy_input["bbox"] = torch.randint(1, 512,(1,345,4))
torch_out = model(**dummy_input) #need to decompress before input

因此,我想知道是否可以将上述张量合并为一个男高音, 例如,

input_ids = torch.randint(1, 512,(1,345))
attention_mask = torch.randint(1, 512,(1,345))
bbox = torch.randint(1, 512,(1,345,4))
torch_out = model(input_ids = input_ids, attention_mask = attention_mask, bbox = bbox) #is it possible to input only one tensor to replace these three?
print(torch_out)

顺便说一句,我的模型的前向函数就是这样,只有这三个张量是必要的,

def forward(
    self,
    input_ids=None,
    bbox=None,
    attention_mask=None,
    token_type_ids=None,
    valid_span=None,
    position_ids=None,
    head_mask=None,
    inputs_embeds=None,
    encoder_hidden_states=None,
    encoder_attention_mask=None,
    past_key_values=None,
    use_cache=None,
    output_attentions=None,
    output_hidden_states=None,
    return_dict=None,
    images=None,
):
python deep-learning pytorch neural-network
1个回答
0
投票

如果你想将这三个张量组合成一个,看起来你可以 通过连接来解决这个问题,因为它们大致具有相同的形状:

  • input_ids
    (b, f)
  • attention_mask
    (b, f)
  • bbox
    (b, f, 4)

如果你沿着三维连接,你会得到一个张量的形状

(b, f, 4+1+1)

>>> x = torch.cat([input_ids.unsqueeze(-1), attention_mask.unsqueeze(-1), bbox], dim=-1)
© www.soinside.com 2019 - 2024. All rights reserved.