Search code examples
pythonpytorchtorchbert-language-model

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


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.


Solution

  • 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