‘tuple’ object does not support item assignment in torch.cat()

huangapple go评论74阅读模式
英文:

'tuple' object does not support item assignment in torch.cat()

问题

I am trying to use the torch.cat() to concatenate the torch tensor. However, I face the error message with --> 'tuple' object does not support item assignment.

Here are my code:

inputs = tokenizer.encode_plus(txt, add special tokens=False, return tensors="pt")
input id chunks = inputs["input ids"][0].split(510)
mask chunks = inputs["attention mask"][0].split(510)

print(type(input id chunks))

for i in range(len(input id chunks)):
    print(type(input id chunks[i]))
    print(input id chunks[i])

    input id chunks[i] = torch.cat([
        torch Tensor([101]), input id chunks[i], torch Tensor([102])
    ])

The outputs look fine, the input id chunks[i] is a torch.Tensor:

<class 'tuple'>

<class 'torch.Tensor'>

But I got the following print and error message:

TypeError: 'tuple' object does not support item assignment

in torch.cat()

I have used a small testing code for torch.cat() and it works fine, but I don't know what is missing in my original code.

英文:

I am trying to use the torch.cat() to contenate the torch tensor. However, I face the error messagge with --> 'tuple' object does not support item assignment.

Here are my code:

inputs = tokenizer.encode_plus(txt, add_special_tokens=False, return_tensors=&quot;pt&quot;)
input_id_chunks = inputs[&quot;input_ids&quot;][0].split(510)
mask_chunks = inputs[&quot;attention_mask&quot;][0].split(510)

print(type(input_id_chunks))

for i in range(len(input_id_chunks)):
    print(type(input_id_chunks[i]))
    print(input_id_chunks[i])

    input_id_chunks[i] = torch.cat([
        torch.Tensor([101]), input_id_chunks[i], torch.Tensor([102])
    ])

The outputs looks fine, the inputs_id_chunks[i] is torch.Tensor:

`<class 'tuple'>

<class 'torch.Tensor'>`

But I got the following print and error message:

TypeError: 'tuple' object does not support item assignment

in torch.cat()

I have using the small testing code for torch.cat() and it works fine, but I don't know what is missing in my original codes.

答案1

得分: 0

你无法更改元组的值,相反,你可以将其赋值给列表,然后向列表附加新值,然后在进行所有想要实现的更改之后,应将其再次赋值给元组。

请查看此链接:link

英文:

you can't change tuple value, instead you can assign it to list, then append new value to it and then after all changes you want to implement, you should assign again it to tuple.

please check this link

答案2

得分: 0

更明确地说,input_id_chunks 是一个元组:

input_id_chunks = inputs["input_ids"][0].split(510)
print(type(inputs_id_chunks)) # 这是<class 'tuple'>

然后在 for 循环中,你正在赋值给一个元组的元素:

input_id_chunks[i] = torch.cat([
        torch.Tensor([101]), input_id_chunks[i], torch.Tensor([102])
    ])

这就是你得到错误的原因。

input_id_chunks = list(input_id_chunks)
英文:

to be more clear, input_id_chunks is a tuple:

input_id_chunks = inputs[&quot;input_ids&quot;][0].split(510)
print(type(inputs_id_chunks)) # this is &lt;class &#39;tuple&#39;&gt;

then in for loop you are assigning a tuple value

input_id_chunks[i] = torch.cat([
        torch.Tensor([101]), input_id_chunks[i], torch.Tensor([102])
    ])

that is why you are getting error.

input_id_chunks=list(input_id_chunks)

huangapple
  • 本文由 发表于 2023年2月6日 19:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定