How to solve Pytorch problem?

username: twpann
cooked: <p>I want to run pytorch program with my dataset.Please help me in solving the problem.The error is as follows:<br>
Traceback (most recent call last):<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 311, in <br>
fire.Fire(demo)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 138, in Fire<br>
component_trace = _Fire(component, args, parsed_flag_args, context, name)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 468, in _Fire<br>
target=component.<strong>name</strong>)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 672, in _CallAndUpdateTrace<br>
component = fn(*varargs, **kwargs)<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 289, in demo<br>
n_epochs=n_epochs, batch_size=batch_size, seed=seed)<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 168, in train<br>
n_epochs=n_epochs,<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 42, in train_epoch<br>
for batch_idx, (input, target) in enumerate(loader):<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\utils\data\dataloader.py”, line 346, in <strong>next</strong><br>
data = self._next_data()<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\utils\data\dataloader.py”, line 386, in _next_data<br>
data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\utils\data_utils\fetch.py”, line 47, in fetch<br>
return self.collate_fn(data)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\utils\data_utils\collate.py”, line 87, in default_collate<br>
return [default_collate(samples) for samples in transposed]<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\utils\data_utils\collate.py”, line 87, in <br>
return [default_collate(samples) for samples in transposed]<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\utils\data_utils\collate.py”, line 72, in default_collate<br>
return default_collate([torch.as_tensor(b) for b in batch])<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\torch\utils\data_utils\collate.py”, line 63, in default_collate<br>
return torch.stack(batch, 0, out=out)<br>
RuntimeError: stack expects each tensor to be equal size, but got [650] at entry 0 and [108] at entry 1<br>
Thanks in advance!!</p>

username: ptrblck
cooked: <p>Based on the error message if seems that the tensors stored in <code>batch</code>, which should be created in the <code>Dataset.__getitem__</code> method, do not have the same shape.<br>
If you are dealing with variable input shapes, you could use a custom collate function as described <a href="https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278/3">here</a>.</p>

username: twpann
cooked: <p>Thank you very much for answering.But the error appears:<br>
Traceback (most recent call last):<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 320, in <br>
fire.Fire(demo)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 138, in Fire<br>
component_trace = _Fire(component, args, parsed_flag_args, context, name)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 468, in _Fire<br>
target=component.<strong>name</strong>)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 672, in _CallAndUpdateTrace<br>
component = fn(*varargs, **kwargs)<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 298, in demo<br>
n_epochs=n_epochs, batch_size=batch_size, seed=seed)<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 177, in train<br>
n_epochs=n_epochs,<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 54, in train_epoch<br>
input = input.cuda()<br>
AttributeError: ‘numpy.ndarray’ object has no attribute 'cuda<br>
Although ‘input’ is converted as torch.tensor,ValueError: expected sequence of length 528 at dim 1 (got 76) appears.Please answer how can I solve.Thank a lot!</p>

username: ptrblck
cooked: <p>The first error is created, since <code>input</code> seems to be a numpy array.<br>
You can check the type via <code>print(type(input))</code>.</p>
<p>Could you post the complete error message for the second issue and which line of code is raising this error?</p>

username: twpann
cooked: <p>Thank you very much!When’print(type(input))’,the line shows :type(input) &lt;type(input) &lt;class ‘list’&gt;<br>
The code rising this error:<br>
for batch_idx,(input, target) in enumerate(loader):<br>
# Create vaiables<br>
if torch.cuda.is_available():<br>
print(‘type(input)’,type(input))<br>
input = input.cuda()<br>
The error message is :Traceback (most recent call last):<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 323, in <br>
fire.Fire(demo)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 138, in Fire<br>
component_trace = _Fire(component, args, parsed_flag_args, context, name)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 468, in _Fire<br>
target=component.<strong>name</strong>)<br>
File “C:\Users\hp\Anaconda3\envs\tf-gpu\lib\site-packages\fire\core.py”, line 672, in _CallAndUpdateTrace<br>
component = fn(*varargs, **kwargs)<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 301, in demo<br>
n_epochs=n_epochs, batch_size=batch_size, seed=seed)<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 180, in train<br>
n_epochs=n_epochs,<br>
File “C:\Users\hp\Downloads\efficient_densenet_pytorch-master\demoEmotion.py”, line 56, in train_epoch<br>
input = input.cuda()<br>
AttributeError: ‘list’ object has no attribute ‘cuda’<br>
Now when I run again,list object has no ‘cuda’. Please answer!Thanks a lot in advance!</p>

username: ptrblck
cooked: <p>As the error explains, <code>list</code> objects do not have the <code>.cuda()</code> method, which is a tensor method.<br>
Could you unpack the list and call <code>cuda()</code> on each tensor inside the list?</p>

username: twpann
cooked: <p>Thanks!How to unpack and call?Please explain.When I convert to torch.tensor,ValueError: expected sequence of length 528 at dim 1 (got 76) appears.</p>

username: ptrblck
cooked: <p>Since it’s a <code>list</code>, you could get each value via:</p>
<pre><code class="lang-python">tensor0 = input[0]
tensor1 = input[1]
tensor2 = input[2]
...
</code></pre>

username: twpann
cooked: <p>Thank you very much!The problem above is solved.But ‘RuntimeError: Expected 4-dimensional input for 4-dimensional weight [24, 3, 3, 3], but got 3-dimensional input of size [3, 3, 158] instead’ error  appears.Thanks a lot!</p>

username: ptrblck
cooked: <p>The input tensor might be missing the batch dimension, if the current shapes correspond to <code>[channels, height, width]</code>.<br>
If so, you could add the batch dimension via <code>x = x.unsqueeze(0)</code>.</p>

username: twpann
cooked: <p>Thanks a lot!However the error ‘RuntimeError: Given input size: (150x1x79). Calculated output size: (150x0x39). Output size is too small’ appears.Why does the error appear?Thanks!</p>