英文:
'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="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 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["input_ids"][0].split(510)
print(type(inputs_id_chunks)) # this is <class 'tuple'>
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论