Feeding new data to a trained model

username: pedram1
cooked: <p>Hi there</p>
<p>I have trained a conv NN, for 1D (signal) data. my data size is (65535,1,94) which 65535 is number of my samples, 1 is input channel and 94 is length of the signal.</p>
<p>Now I am going to use my trained model for another data set that has the same dimension.<br>
I tried to run this but it did not work:</p>
<p>out_put = torch.zeros(65535,1,94)<br>
for i in range (65535):<br>
out_out[i,1,94]  = model (in_put [i,1,94])</p>
<p>below u can also see my network<br>
Autoencoder(<br>
(encoder): Sequential(<br>
(0): Conv1d(1, 5, kernel_size=(5,), stride=(2,))<br>
(1): MaxPool1d(kernel_size=3, stride=1, padding=0, dilation=1, ceil_mode=False)<br>
(2): ReLU(inplace=True)<br>
(3): Conv1d(5, 10, kernel_size=(5,), stride=(2,))<br>
(4): MaxPool1d(kernel_size=3, stride=1, padding=0, dilation=1, ceil_mode=False)<br>
(5): ReLU(inplace=True)<br>
(6): Conv1d(10, 15, kernel_size=(5,), stride=(2,))<br>
(7): MaxPool1d(kernel_size=3, stride=1, padding=0, dilation=1, ceil_mode=False)<br>
(8): ReLU(inplace=True)<br>
(9): Conv1d(15, 20, kernel_size=(4,), stride=(1,))<br>
(10): ReLU(inplace=True)<br>
)<br>
(decoder): Sequential(<br>
(0): ConvTranspose1d(20, 15, kernel_size=(1,), stride=(4,))<br>
(1): ReLU(inplace=True)<br>
(2): ConvTranspose1d(15, 10, kernel_size=(2,), stride=(4,))<br>
(3): ReLU(inplace=True)<br>
(4): ConvTranspose1d(10, 5, kernel_size=(9,), stride=(2,))<br>
(5): ReLU(inplace=True)<br>
(6): ConvTranspose1d(5, 1, kernel_size=(10,), stride=(2,))<br>
(7): ReLU(inplace=True)<br>
)<br>
)</p>
<p>is anyone has any idea about this ?<br>
thank you</p>

username: InnovArul
cooked: <aside class="quote no-group" data-username="pedram1" data-post="1" data-topic="65004">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/48/21032_2.png" class="avatar"> pedram1:</div>
<blockquote>
<p>I tried to run this but it did not work:</p>
</blockquote>
</aside>
<p>You would be able to get help if you post the error message as well.</p>

username: pedram1
cooked: <p>Hi</p>
<p>below you can see that</p>
<hr>
<p>IndexError Traceback (most recent call last)</p>
<p><a href="https://9kfmmcf91rd-b5278c2458b95a70-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20191219-082400-RC00_286397102#" rel="nofollow noopener"></a> in () 1 for i in (data_pixel2[0]) : ----&gt; 2 out_put[i,1,94] = model (data_pixel2[i,1,94])</p>
<p>IndexError: index 1 is out of bounds for dimension 1 with size 1</p>

username: InnovArul
cooked: <aside class="quote no-group" data-username="pedram1" data-post="3" data-topic="65004">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/48/21032_2.png" class="avatar"> pedram1:</div>
<blockquote>
<p>out_put[i,1,94] = model (data_pixel2[i,1,94])</p>
</blockquote>
</aside>
<p>Shouldn’t it be <code>out_put[i,0,94] = model (data_pixel2[i,0,94])</code>?</p>
<p>Python is 0-indexed.</p>

username: pedram1
cooked: <p>Hi</p>
<p>Actually I got this error again<br>
IndexError Traceback (most recent call last)</p>
<p><a href="https://ghga5mjmd64-b5278c2458b95a70-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20191219-082400-RC00_286397102#" rel="nofollow noopener"></a> in () 1 for i in (data_pixel2[0]) : ----&gt; 2 out_put[i,0,94] = model (data_pixel2[i,0,94])</p>
<p>IndexError: index 94 is out of bounds for dimension 2 with size 94’</p>

username: Donk10
cooked: <p>Python is zero-based as InnovArul already pointed out, index 94 doesn’t exist. That should solve the index problem:<br>
<code>out_put[i] = model(data_pixel2[i])</code> which is the same as<br>
<code>out_put[i,:,:] = model(data_pixel2[i,:,:])</code><br>
since you want to input an array of shape (1,94).</p>
<p>In general, I would recommend looking at a pytorch tutorial regarding the structure optimizer and co., e.g. <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html</a></p>

username: pedram1
cooked: <p>Thank you for replying me back<br>
actually it didn’t work also, I got below error <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
RuntimeError Traceback (most recent call last)</p>
<p><a href="https://ghga5mjmd64-b5278c2458b95a70-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20191219-082400-RC00_286397102#" rel="nofollow noopener"></a> in () 1 for i in range(65536) : ----&gt; 2 out_put[i,:,:] = model (data_pixel2[i,:,:])</p>
<p>5 frames</p>
<p><a href="https://ghga5mjmd64-b5278c2458b95a70-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20191219-082400-RC00_286397102#" rel="nofollow noopener">/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py</a> in forward(self, input) 200 _single(0), self.dilation, self.groups) 201 return F.conv1d(input, self.weight, self.bias, self.stride, --&gt; 202 self.padding, self.dilation, self.groups) 203 204</p>
<p>RuntimeError: Expected 3-dimensional input for 3-dimensional weight 5 1 5, but got 2-dimensional input of size [1, 94] instead</p>

username: Donk10
cooked: <p>Oke, the input to the 1D Convolutional layer must be of shape batch size x channel x spatial_size. In your case, it’s only channel x spatial_size. This can be fixed by model(data_pixel2[i].unsqueeze(0)) which adds another dimension in the tensor to be of shape (1,1,94)</p>

username: pedram1
cooked: <p>My data_pixel2 is already (65535,1,94)</p>
<p>here 65536 means number of samples that I do have</p>

username: Donk10
cooked: <p>you’re slicing the first dimension with data_pixel2[i] which leads to a shape of (1,94)</p>

username: pedram1
cooked: <p>I see, what should I do now? <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

username: pedram1
cooked: <aside class="quote no-group" data-username="Donk10" data-post="8" data-topic="65004">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/donk10/48/16624_2.png" class="avatar"> Donk10:</div>
<blockquote>
<p>model(data_pixel2[i].unsqueeze(0))</p>
</blockquote>
</aside>
<p>I actually fixed that error, however I now face with this one :<br>
RuntimeError Traceback (most recent call last)</p>
<p><a href="https://ghga5mjmd64-b5278c2458b95a70-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20191219-082400-RC00_286397102#" rel="noopener nofollow ugc"></a> in () 1 for i in range(65536) : ----&gt; 2 out_put[i] = model(data_pixel2[i].unsqueeze(0))</p>
<p>5 frames</p>
<p><a href="https://ghga5mjmd64-b5278c2458b95a70-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20191219-082400-RC00_286397102#" rel="noopener nofollow ugc">/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py</a> in forward(self, input) 200 _single(0), self.dilation, self.groups) 201 return F.conv1d(input, self.weight, self.bias, self.stride, → 202 self.padding, self.dilation, self.groups) 203 204</p>
<p>RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same</p>

username: Donk10
cooked: <p>load your input into VRAM with .cuda()</p>

username: pedram1
cooked: <p>I did below<br>
out_put = torch.zeros([65536,1,94],dtype=torch.float)</p>
<p>out_put = out_put.cuda()</p>
<p>still same error …</p>

username: Donk10
cooked: <p>Please have a look into the Pytorch tutorials</p>

username: ptrblck
cooked: <p>Could you push <code>data_pixel2</code> also to the GPU?<br>
The error points to a CPU input tensor, while your model parameters are on the GPU.</p>

username: pedram1
cooked: <p>Thanks Patrick<br>
U were right, it fixed <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
U are really an expert (Y)</p>