I get this error: Input tensor shape: torch.Size([112, 40]) Target tensor shape:…
- User
I get this error: Input tensor shape: torch.Size([112, 40]) Target tensor shape: torch.Size([112, 40]) Hidden state shape after initialization: torch.Size([1, 128, 128]) Hidden state shape at error location: torch.Size([1, 128, 128]) Hidden state shape before RNN/LSTM: torch.Size([1, 128, 128]) Traceback (most recent call last): File "c:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\RNN-byte.py", line 71, in output, hidden = model(input_batch, hidden.detach()) File "C:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\GPT_2_Math\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "C:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\GPT_2_Math\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs) File "c:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\RNN-byte.py", line 48, in forward out, hidden = self.rnn(x, hidden) File "C:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\GPT_2_Math\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "C:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\GPT_2_Math\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs) File "C:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\GPT_2_Math\lib\site-packages\torch\nn\modules\rnn.py", line 549, in forward self.check_forward_args(input, hx, batch_sizes) File "C:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\GPT_2_Math\lib\site-packages\torch\nn\modules\rnn.py", line 273, in check_forward_args
self.check_hidden_size(hidden, expected_hidden_size) File "C:\Users\Dell-PC\Desktop\Projets\Finetuned_Language_models\content\GPT_2_Math\lib\site-packages\torch\nn\modules\rnn.py", line 256, in check_hidden_size
raise RuntimeError(msg.format(expected_hidden_size, list(hx.size()))) RuntimeError: Expected hidden size (1, 112, 128), got [1, 128, 128] when i run this code; import torch import torch.nn as nn from torch.utils.data import DataLoader, TensorDataset import matplotlib.pyplot as pltParameters
SEQUENCE_LENGTH = 40 BATCH_SIZE = 128 # Ensure consistency across code VOCAB_SIZE = 128 # Number of ASCII characters we consider in the vocabulary HIDDEN_SIZE = 128
Sample text data for training
text = "This is a simple example text to demonstrate the RNN. Try using longer text if you want to get better results, the life of computers is a bit confusing."
Convert text to ASCII values
ascii_characters = [ord(c) for c in text]
Prepare datasets
inputs = [] targets = [] for i in range(0, len(ascii_characters) - SEQUENCE_LENGTH): input_seq = ascii_characters[i:i+SEQUENCE_LENGTH] target = ascii_characters[i+1:i+SEQUENCE_LENGTH+1] inputs.append(torch.tensor(input_seq, dtype=torch.long)) targets.append(torch.tensor(target, dtype=torch.long))
- Assistant
The error you're encountering is due to a mismatch between the expected hidden state size of the RNN and the actual hidden state size you're providing to it.
When you initialize your hidden state with:
pythonhidden = model.init_hidden(BATCH_SIZE) - User
The window that shows the plot should show the evolution of the loss of each epoch, i shouldn't need to close the window each time, it should update regularely
- Assistant
- User
the text variable should contain the content of a txt file, if the txt file contains blank lines, they should be ignored
- Assistant
To read the content of a
.txtfile into thetextvariable and ignore blank lines, you can open the file, read the lines, and filter out any lines that are blank or contain only whitespace. Here's how you can do it:python# Replace 'your_file.txt' with the path to your actual text file. file_path = 'your_file.txt' # Read the file and filter out blank lines with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() text = "".join(line for line in lines if not line.isspace()) # Now 'text' contains the content from 'your_file.txt' without any blank lines.In the above snippet:
- The
openfunction is used to read the file specified byfile_path. readlines()reads all lines in the file and returns a list of strings.
- The