I am building a DecoderRNN using PyTorch (This is an image-caption decoder):
class DecoderRNN(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_size):
super(DecoderRNN, self).__init__()
self.hidden_size = hidden_size
self.gru = nn.GRU(embed_size, hidden_size, hidden_size)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, features, captions):
print (features.shape)
print (captions.shape)
output, hidden = self.gru(features, captions)
output = self.softmax(self.out(output[0]))
return output, hidden
The data have the following shapes:
torch.Size([10, 200]) <- features.shape (10 for batch size)
torch.Size([10, 12]) <- captions.shape (10 for batch size)
Then I got the following errors. Any ideas what I missed here? Thanks!
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-2-76e05ba08b1d> in <module>()
44 # Pass the inputs through the CNN-RNN model.
45 features = encoder(images)
---> 46 outputs = decoder(features, captions)
48 # Calculate the batch loss.
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
--> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
/home/workspace/model.py in forward(self, features, captions)
37 print (captions.shape)
38 # features = features.unsqueeze(1)
---> 39 output, hidden = self.gru(features, captions)
40 output = self.softmax(self.out(output[0]))
41 return output, hidden
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
--> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/rnn.py in forward(self, input, hx)
167 flat_weight=flat_weight
168 )
--> 169 output, hidden = func(input, self.all_weights, hx)
170 if is_packed:
171 output = PackedSequence(output, batch_sizes)
/opt/conda/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward(input, *fargs, **fkwargs)
383 return hack_onnx_rnn((input,) + fargs, output, args, kwargs)
384 else:
--> 385 return func(input, *fargs, **fkwargs)
387 return forward
/opt/conda/lib/python3.6/site-packages/torch/autograd/function.py in _do_forward(self, *input)
326 self._nested_input = input
327 flat_input = tuple(_iter_variables(input))
--> 328 flat_output = super(NestedIOFunction, self)._do_forward(*flat_input)
329 nested_output = self._nested_output
330 nested_variables = _unflatten(flat_output, self._nested_output)
/opt/conda/lib/python3.6/site-packages/torch/autograd/function.py in forward(self, *args)
348 def forward(self, *args):
349 nested_tensors = _map_variable_tensor(self._nested_input)
--> 350 result = self.forward_extended(*nested_tensors)
351 del self._nested_input
352 self._nested_output = result
/opt/conda/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward_extended(self, input, weight, hx)
292 hy = tuple(h.new() for h in hx)
--> 294 cudnn.rnn.forward(self, input, hx, weight, output, hy)
296 self.save_for_backward(input, hx, weight, output)
/opt/conda/lib/python3.6/site-packages/torch/backends/cudnn/rnn.py in forward(fn, input, hx, weight, output, hy)
206 if (not is_input_packed and input.dim() != 3) or (is_input_packed and input.dim() != 2):
207 raise RuntimeError(
--> 208 'input must have 3 dimensions, got {}'.format(input.dim()))
209 if fn.input_size != input.size(-1):
210 raise RuntimeError('input.size(-1) must be equal to input_size. Expected {}, got {}'.format(
RuntimeError: input must have 3 dimensions, got 2
input of shape (seq_len, batch, input_size): tensor containing the features of the input sequence.
Further you need to provide the hidden state (last encoder hidden state in this case) as second parameter:
self.gru(input, h_0)
Where input
is your actual input and h_0
the hidden state which needs to be 3-dimensional as well:
h_0 of shape (num_layers * num_directions, batch, hidden_size): tensor
containing the initial hidden state for each element in the batch.
Defaults to zero if not provided.
https://pytorch.org/docs/master/nn.html#torch.nn.GRU
PyTorch: DecoderRNN: RuntimeError: input must have 3 dimensions, got 2
PyTorch ValueError: Target and input must have the same number of elements
Pytorch RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got CUDAType instead
expected input to have 4 dimensions, but got array with shape
ValueError: Dimensions must be equal, but are 784 and 500 for 'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [500,500]
Pytorch BiLSTM POS Tagging Issue: RuntimeError: input.size(-1) must be equal to input_size. Expected 6, got 12
ValueError: Dimensions must be equal, but are 100 and 19 with input shapes: [?,100], [?,100,19]
ValueError: 'images' must have either 3 or 4 dimensions
Error when checking target: expected dense_3 to have shape (3,) but got array with shape (1,)
TypeError: slice indices must be integers or None or have an __index__ method
TypeError: argument 1 must have a "write" method
Efficient PyTorch DataLoader collate_fn function for inputs of various dimensions
If an object doesn't have `__dict__`, must its class have a `__slots__` attribute?
Pytorch - Stack dimension must be exactly the same?
FuzzyWuzzy error: WARNING:root:Applied processor reduces input query to empty string, all comparisons will have score 0. [Query: '/']
(Python 3) Spider must return Request, BaseItem, dict or None, got 'generator'
Keras ValueError: ValueError: Error when checking target: expected dense_4 to have shape (None, 2) but got array with shape (2592, 1) Python3
TypeError argument must be an int or have a fileno() method
Tensorflow, expected conv2d_input to have 4 dimensions
InvalidArgumentError: logits and labels must have the same first dimension seq2seq Tensorflow
Reducing input dimensions for a deep learning model
How to get ffmpeg configuration similar to that of youtube for 480p and 1080p video? I have got a function but the output quality is too low
Pytorch expected type Long but got type int
How to have multiple strings in one input to print in one line?
Pytorch Grayscale input to Vgg
Make it so you don't have to hit "Enter" after entering input - Python
Value Error: Input arrays should have the same number of samples as target arrays. Found 166 input samples and 4 target samples
ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (6,)
Keras: ValueError: Error when checking target: expected dense_2 to have shape (10,) but got array with shape (1,)
how to fix this error "Encoders require their input to be uniformly strings or numbers. Got ['float', 'str']"
3D SparseTensor matrix multiplication with 2D Tensor :InvalidArgumentError: Tensor 'a_shape' must have 2 elements [Op:SparseTensorDenseMatMul]
How to have an updating prompt while waiting for input in python?
How do I fix ' ValueError: callback must be a callable, got None' on schedule Kivy.clock callback function?
raise Value Error('Custom files must have an attribution')
TypeError: backward() got an unexpected keyword argument 'grad_tensors' in pytorch
RuntimeError: Given groups=3, weight of size 12 64 3 768, expected input[32, 12, 30, 768] to have 192 channels, but got 12 channels instead
Can an object have a `Callable` type hint without specifying input arguments and return value?
tf.nn.static_rnn - input must be a sequence
ValueError: Argument must be a dense tensor: [['1', '2', '3'], ['1']] - got shape [2], but wanted [2, 3]
Argument must be bytes or unicode, got '_Element'
I have an input file that I am trying to build a dictionary from
ValueError: f(a) and f(b) must have different sign
MS SQL Connect gives error mssql: A SQL editor must have focus
Tensorflow LSTM : ValueError: Shape must have rank at least 3
keras multi dimensions input to simpleRNN: dimension mismatch
Pytorch ValueError: Expected target size (2, 13), got torch.Size([2]) when calling CrossEntropyLoss
Python3 Pytorch RuntimeError on GCP - no msg
ValueError: logits and labels must have the same shape ((None, 6, 8, 1) vs (None, 1))
Tags in scraped content must have the same order as they have in the original HTML file
How to fix 'ValueError("input must have more than one sentence")' Error
More Query from same tag
How to loop list and sum particular items in python
eval("a += b") in python 3.5.2
Keras tensors - Get values with indices coming from another tensor
AttributeError: 'str' object has no attribute 'author'
Wx.stc.StyledTextCtrl ScrollBar
Can __radd__ work with the operands in any order?
Python: create a directory with 777 permissions
How to return results of web scraping in loop and save to Excel file?
PyGObject on Windows "ERROR:root:Could not find any typelib for Gtk"
TypeError: Subprocess on linux
tkinter execution dies after about 140 iterations with no error message (mem leak?)
Why does my value change when I assign it to list's first index?
python unpacking a text file into separate variables
Trying to pass PIL image to a function, getting TypeError: 'tuple' object is not callable
Detecting Exception Type
Moving a turtle to the center of a circle
Upload Image by TKinter Filedialog functions and stored by another button
How do local variables work with Python closures?
Python3 how is mro defined in object?
Can't get function to return false?
Fill in a field with a date that hides in right-click to inspect element
Python: recursive method rollbacks changes?
How to copy a directory to google cloud storage using google cloud Python API?
Traceback (most recent call last) when docx.Document() is used
Python - how can I use generators more succinctly?
The def boo(list_products) doesn´t work as it should, any hint to make it works
DLL load failed : in installing opencv2?
How to create a non-field instance-level variable on a dataclass?
AttributeError: module 'urwid' has no attribute 'Text'
Regular expressions: How to make my code match the '+' character OR digits
Discord.py event loop closed error when trying to load token from file
I am getting permission error to a folder when I try opening it with io.open
modifying int and float in python
Printing the content of all html files in a directory with BeautifulSoup
Simulation task, I'm stuck with producing correct output
Why aren't all the images from a folder displayed on my tkinter label within a frame on a canvas?
Python Tkinter pause screen redraw
Unknow option for tkinter scale widget
select an image to show with dialog
Create a class for tkinter widgets to call default attributes
Python GUI callback functions
First Time using tkinter, unknown error: TclError
Get search terms from input box
Why did Run best_estimator_ from GridSearch using cross-validation produce different accuracy score?
roc_curve from multilabel classification has slope
Limitations of sklearn Truncated SVD (LSA) Implementation
Clusters features median values using Python
How to deal with name column in Scikitlearn randomforest classifier. python 3
Re-fit a decision tree to add a layer
I can`t import scikit -learn on python3. how can i import it?
Logistic Regression - ValueError: classification metrics can't handle a mix of continuous-multi output and binary targets
Cannot Install Sci-Kit Learn Package on Mac OSX Error
Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (224, 224, 3)
Multiple Softmax in Dense Layer
Workaround for the error while_loop() got an unexpected keyword argument 'maximum_iterations'
Confusion Matrix giving Bad Results but Validation Accuracy ~95%
How can I change the following code from pytorch to tensorflow?
How are the batches iterated in the PTB LSTM example of Tensorflow?