You return four variables s1,s2,s3,s4 and receive them using a single variable obj. This is what is called a tuple, obj is associated with 4 values, the values of s1,s2,s3,s4. So, use index as you use in a list to get the value you want, in order.

Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
print obj[3] + " is a benefit of functions!"
Answer from Aswin Murugesh on Stack Overflow
🌐
GitHub
github.com › jasoncao11 › nlp-notebook › issues › 9
3-2 'tuple' object has no attribute 'last_hidden_state' · Issue #9 · jasoncao11/nlp-notebook
February 23, 2023 - [Train Epoch 0]: 0%| | 0/1584 [00:00<?, ?it/s] Traceback (most recent call last): File "demo_train.py", line 66, in run() File "demo_train.py", line 53, in run loss = model.neg_log_likelihood(input_ids, attention_mask, label_ids, real_lens) File "/home/z/code/nlp-notebook-master/3-2.Bert-CRF/model.py", line 137, in neg_log_likelihood feats = self.get_features(input_ids, attention_mask) File "/home/z/code/nlp-notebook-master/3-2.Bert-CRF/model.py", line 53, in get_features sequence_output, pooled_output = x.last_hidden_state, x.pooler_output AttributeError: 'tuple' object has no attribute 'last_hidden_state'
Author   orangejuice1007
Discussions

python - AttributeError: 'tuple' object has no attribute - Stack Overflow
I'm a beginner in python. I'm not able to understand what the problem is? def list_benefits(): s1 = "More organized code" s2 = "More readable code" s3 = "Easier code reuse... More on stackoverflow.com
🌐 stackoverflow.com
GridSearchCV AttributeError: 'tuple' object has no attribute 'to'
I want to use GridSearchCV. The first optimization works, the problem seems to be when it runs the second. In this case I get the error AttributeError: 'tuple' object has no attribute '... More on github.com
🌐 github.com
6
May 11, 2020
AttributeError: 'tuple' object has no attribute 'hidden_states'
2021-09-19 20:23:44:INFO: ***** Running dev ***** 2021-09-19 20:23:44:INFO: Num examples = 271 2021-09-19 20:23:44:INFO: Batch size = 16 dev: 0%| | 0/17 [00:02 More on github.com
🌐 github.com
1
September 19, 2021
python - Longformer get last_hidden_state - Stack Overflow
I am trying to follow this example in the huggingface documentation here https://huggingface.co/transformers/model_doc/longformer.html: import torch from transformers import LongformerModel, More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › barissayil › SentimentAnalysis › issues › 24
AttributeError: 'tuple' object has no attribute 'last_hidden_state' · Issue #24 · barissayil/SentimentAnalysis
May 29, 2021 - Hey, thanks for the code. When I was running, there was some error. Could you please help me to check it. Thank File "F:\PycharmProjects\SentimentAnalysis-master\modeling.py", line 25, in forward cls_reps = outputs.last_hidden_state[:, 0...
Published   May 29, 2021
Author   wenk207
🌐
GitHub
github.com › skorch-dev › skorch › issues › 633
GridSearchCV AttributeError: 'tuple' object has no attribute 'to' · Issue #633 · skorch-dev/skorch
May 11, 2020 - The first optimization works, the problem seems to be when it runs the second. In this case I get the error AttributeError: 'tuple' object has no attribute 'to' since I have a LSTM, the forward method returns 1 array and a tuple of array (the hidden states) so I modified the loss function in this way (following sklearn documentation):
Published   May 11, 2020
Author   brunomorampc
🌐
Bobby Hadz
bobbyhadz.com › blog › python-attributeerror-tuple-object-has-no-attribute
AttributeError: 'tuple' object has no attribute X in Python | bobbyhadz
April 8, 2024 - The Python AttributeError: 'tuple' object has no attribute occurs when we access an attribute that doesn't exist on a tuple.
Find elsewhere
🌐
GitHub
github.com › liuwei1206 › LEBERT › issues › 31
AttributeError: 'tuple' object has no attribute 'hidden_states' · Issue #31 · liuwei1206/LEBERT
September 19, 2021 - 2021-09-19 20:23:44:INFO: ***** Running dev ***** 2021-09-19 20:23:44:INFO: Num examples = 271 2021-09-19 20:23:44:INFO: Batch size = 16 dev: 0%| | 0/17 [00:02<?, ?it/s] Traceback (most recent call last): File "/root/python_proj/LEBERT/Trainer.py", line 601, in <module> main() File "/root/python_proj/LEBERT/Trainer.py", line 584, in main eval_output, _ = evaluate(model, args, dev_dataset, label_vocab, global_steps, "dev", write_file=True) File "/root/python_proj/LEBERT/Trainer.py", line 444, in evaluate outputs = model(**inputs) File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modul
Author   lvjiujin
🌐
Hugging Face
discuss.huggingface.co › beginners
The question-answering example in the doc throws an AttributeError exception. Please help - Beginners - Hugging Face Forums
December 5, 2020 - Hi. Admittedly I am a beginner to HuggingFace, though I do have some Python experience and general programming experience. I am using transformers version: 3.5.1 Platform: Windows-10-10.0.18362-SP0 Python version: 3.6.12 PyTorch version (GPU?): 1.7.0 (False) Tensorflow version (GPU?): not installed (NA) Using GPU in script?: No Using distributed or parallel set-up in script?: No I copy-pasted the following code (see at the bottom of the post) from the Transformers doc on Summary of the tas...
🌐
Python.org
discuss.python.org › python help
AttributeError: 'tuple' object has no attribute 'enter' - Python Help - Discussions on Python.org
March 5, 2025 - Help! I’m coding a game program for my daughter and I come up with the following error: Traceback (most recent call last): File “/home/roberto-padilla/mystuff/ella_game.py”, line 193, in a_game.play() File “/home/roberto-padilla/mystuff/ella_game.py”, line 22, in play next_scene_name = current_scene.enter() ^^^^^^^^^^^^^^^^^^^ AttributeError: ‘tuple’ object has no attribute ‘enter’ Here’s the code: class Engine(object): def __init__(self, scene_map): self.scene_map = scen...
🌐
Esri Community
community.esri.com › t5 › arcgis-api-for-python-questions › attributeerror-tuple-object-has-no-attribute › td-p › 1182615
Solved: AttributeError: 'tuple' object has no attribute 'c... - Esri Community
June 14, 2022 - Solved: Hi, We work on a dedicated hosted version, so not the cloud solution. When we launch the scripts we manage to load everything to start with. so: import
🌐
PyTorch Forums
discuss.pytorch.org › nlp
AttributeError: 'tuple' object has no attribute 'permute' error in XLM-R pytorch - nlp - PyTorch Forums
March 3, 2022 - I’m trying to convert a BERT-LSTM model to XLM-R - LSTM model. The complete code of BERT-LSTM worked fine without any bugs. The forward function of the BERT-LSTM is as follows. def forward(self, sents): sents_tensor, masks_tensor, sents_lengths = sents_to_tensor(self.tokenizer, sents, self.device) encoded_layers, pooled_output = self.bert(input_ids=sents_tensor, attention_mask=masks_tensor, output_all_encoded_layers=False) encoded_layers = encoded_layers.permute(1, 0, 2)...
🌐
Free Python Source Code
freepythonsourcecode.com › post › 117
With Examples Fix attributeerror: 'tuple' object has no attribute ...
September 29, 2024 - The AttributeError: 'tuple' object has no attribute, which occurs when accessing an attribute or method that doesn't exist for a tuple object in Python.
🌐
Python Forum
python-forum.io › thread-33245.html
AttributeError: 'tuple' object has no attribute 'format'
Confuse where is the error because execute some parts of codes half code run and half give me an error AttributeError: 'tuple' object has no attribute 'format' def __str__(self): return ('{} : {}','[]').format(self._word, self._coords)Erro...
🌐
GitHub
github.com › tensorflow › tensorflow › issues › 11077
something wrong with attentionwrapper? · Issue #11077 · tensorflow/tensorflow
June 27, 2017 - I am trying to write a seq2seq model with attention. But it gets the following error: cell_inputs = self._cell_input_fn(inputs, state.attention) AttributeError: 'tuple' object has no attrib...
Author   sunxiaoben
Top answer
1 of 2
4

The issue comes from the fact that hidden (in the forward definition) isn't a Torch.Tensor. Therefore, r_output, hidden = self.gru(nn_input, hidden) raises a rather confusing error without specifying exaclty what's wrong in the arguments. Altough you can see it's raised inside a nn.RNN function named check_hidden_size()...

I was confused at first, thinking that the second argument of nn.RNN: h0 was a tuple containing (hidden_state, cell_state). Same can be said ofthe second element returned by that call: hn. That's not the case h0 and hn are both Torch.Tensors. Interestingly enough though, you are able to unpack stacked tensors:

>>> z = torch.stack([torch.Tensor([1,2,3]), torch.Tensor([4,5,6])])
>>> a, b = z
>>> a, b
(tensor([1., 2., 3.]), tensor([4., 5., 6.]))

You are supposed to provide a tensor as the second argument of a nn.GRU __call__.


Edit - After further inspection of your code I found out that you are converting hidden back again to a tuple... In cell [14] you have hidden = tuple([each.data for each in hidden]). Which basically overwrites the modification you did in init_hidden with torch.stack.

Take a step back and look at the source code for RNNBase the base class for RNN modules. If the hidden state is not given to the forward it will default to:

if hx is None:
    num_directions = 2 if self.bidirectional else 1
    hx = torch.zeros(self.num_layers * num_directions,
                     max_batch_size, self.hidden_size,
                     dtype=input.dtype, device=input.device)

This is essentially the exact init as the one you are trying to implement. Granted you only want to reset the hidden states on every epoch, (I don't see why...). Anyhow, a basic alternative would be to set hidden to None at the start of an epoch, passed as it is to self.forward_back_prop then to rnn, then to self.rnn which will in turn default initialize it for you. Then overwrite hidden with the hidden state returned by that RNN forward call.

To summarize, I've only kept the relevant parts of the code. Remove the init_hidden function from AssetGRU and make those modifications:

def forward_back_prop(rnn, optimizer, criterion, inp, target, hidden):
    ...
    if hidden is not None:
        hidden = hidden.detach()
    ...
    output, hidden = rnn(inp, hidden)  
    ...
    return loss.item(), hidden


def train_rnn(rnn, batch_size, optimizer, criterion, n_epochs, show_every_n_batches):
    ...
    for epoch_i in range(1, n_epochs + 1):
        
        hidden = None
        
        for batch_i, (inputs, labels) in enumerate(train_loader, 1):
            loss, hidden = forward_back_prop(rnn, optimizer, criterion, 
                                             inputs, labels, hidden)
            ...

    ...
2 of 2
-1

There should be [] brackets instead of () around 0.

def forward(self, nn_input, hidden):
    ''' Forward pass through the network. 
        These inputs are x, and the hidden/cell state `hidden`. '''
    
    # batch_size equals the input's first dimension
    batch_size = nn_input.size(0)
🌐
Lemma Soft
lemmasoft.renai.us › forums › viewtopic.php
AttributeError: 'tuple' object has no attribute - Lemma Soft Forums
January 15, 2021 - Hi, Psion, 1) Your errors is because you use variable h in method GetTime. You need to use class attribute self.h 2) It is recommended to use the default statement to 'declare' variables Here is a better example of what you want to do:
🌐
Reddit
reddit.com › r/learnpython › 'tuple' object has no attribute 'value'
r/learnpython on Reddit: 'tuple' object has no attribute 'value'
May 18, 2020 -

hii folks,

i am trying to write data to a excel file.when using for loop,to assign the values to cells,it's giving me the error that 'tuple' object has no attribute 'value' at line 6.

import openpyxl
wb=openpyxl.Workbook()
ws=wb.active
for i in range(10):
	index="A"+str(i)
	ws[index]=i

please help me this error.