Enter the temperature value (1.0 is default, >1 is more random): 0.1 Traceback (most…
- User
Enter the temperature value (1.0 is default, >1 is more random): 0.1 Traceback (most recent call last): File "c:\Users<redacted>\assignment-7-neural-complete-\rnn_complete.py", line 236, in completed_text = generate_text(model, start_text, n, k, temperature) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users<redacted>\assignment-7-neural-complete-\rnn_complete.py", line 210, in generate_text predicted_char_idx = sample_from_output(last_char_logits, temperature) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users<redacted>\assignment-7-neural-complete-\rnn_complete.py", line 179, in sample_from_output probabilities = F.softmax(scaled_logits, dim=1) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users<redacted>\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\nn\functional.py", line 2140, in softmax ret = input.softmax(dim) ^^^^^^^^^^^^^^^^^^ IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) PS C:\Users<redacted>\assignment-7-neural-complete- help me fix this error
- Assistant
The error message indicates that you're trying to apply the
softmaxfunction along an invalid dimension. Since it seems thatlast_char_logitsis a 1D tensor, the correct dimension for softmax should bedim=0.To fix this issue, take the following steps:
Modify the
sample_from_outputFunctionLocate the
sample_from_outputfunction in your code and change the line whereF.softmaxis called. Specifically, change it fromdim=1todim=0.For example, you should find something like this in the
sample_from_outputfunction:pythondef sample_from_output(logits, temperature=1.0): # Scale the logits by the temperature scaled_logits = logits / temperature # Apply softmax on the second dimension (dim=1) probabilities = F.softmax(scaled_logits, dim=1) - User
how can I do this without changing sample by output
- Assistant
If you need to use the
sample_from_outputfunction without modifying it, you'll need to ensure that the input you pass to it has the correct shape that matches the expectation of the function.Approach
Since
sample_from_outputis expecting a 2D tensor (to usedim=1withF.softmax), you can reshape or unsqueeze yourlast_char_logitstensor before passing it tosample_from_output.Here's how you can do it:
-
Unsqueeze the Logits Tensor: Before calling , add an extra dimension to make the tensor 2D. You can do this using .
-