AttributeError: 'numpy.ndarray' object has no attribute 'dim'

username: Abdel
cooked: <p>Hello Team,</p>
<p>i am new to the forum and to Pytorch, i want to predict 3 real values Y1, Y2 &amp; Y3 from input values X1, X2…X10 using the below model, but i get the error in title:</p>
<p>File “/home/abdelmoula/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py”, line 833, in linear<br>
if input.dim() == 2 and bias is not None:</p>
<p>AttributeError: ‘numpy.ndarray’ object has no attribute ‘dim’</p>
<p>Can you please on what is wrong ? thank you<br>
########################</p>
<p>MODEL:</p>
<p>import pandas as pd<br>
import torch<br>
from torch.autograd import Variable</p>
<p>dataset = pd.read_csv(‘Welding.csv’)<br>
dataset_test = pd.read_csv(‘Welding_test.csv’)<br>
x=dataset.iloc[:,:-3].values<br>
y=dataset.iloc[:,-3:].values<br>
x_test=dataset.iloc[:,:].values</p>
<p>model = torch.nn.Sequential(<br>
torch.nn.Linear(10, 20),<br>
torch.nn.ReLU(),<br>
torch.nn.Linear(20, 3),<br>
)<br>
loss_fn = torch.nn.MSELoss(size_average=False)</p>
<p>learning_rate = 1e-2<br>
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)<br>
for t in range(5000):<br>
y_pred = model(x)<br>
loss = loss_fn(y_pred, y)<br>
print(t, loss.data[0])</p>
<pre><code>optimizer.zero_grad()
loss.backward()
optimizer.step()
</code></pre>
<p>print (model(x_test))</p>

username: ptrblck
cooked: <p>You have to cast the numpy array into a <code>Tensor</code> and then wrap it into a <code>Variable</code> with <code>x = Variable(torch.from_numpy(x))</code>.</p>

username: Islem_Maatar
cooked: <p>Hello Team,<br>
I have the same problem in the implementation of the AutoEncoder<br>
this is the class AE:<br>
import torch.nn as nn<br>
import torch<br>
class AE(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(AE,self).<strong>init</strong>()<br>
input_shape=256<br>
self.encoder_hidden_layer=nn.Linear(input_shape,10)<br>
self.encoder_output_layer=nn.Linear(10,2)<br>
self.decoder_hidden_layer=nn.Linear(2,10)<br>
self.decoder_output_layer=nn.Linear(10,input_shape)<br>
self.encoder_hidden_layer1 = nn.Linear(input_shape, 10)<br>
self.encoder_output_layer1= nn.Linear(10, 2)<br>
self.decoder_hidden_layer1 = nn.Linear(2, 10)<br>
self.decoder_output_layer1= nn.Linear(10, input_shape)<br>
def forward(self,features):<br>
activation=self.encoder_hidden_layer(features)<br>
activation=torch.relu(activation)<br>
code=self.encoder_output_layer(activation)<br>
code = torch.relu(code)<br>
activation=self.decoder_hidden_layer(code)<br>
activation=torch.relu(activation)<br>
activation=self.decoder_output_layer(activation)<br>
reconstructed=torch.sigmoid(activation)</p>
<pre><code>    activation1 = self.encoder_hidden_layer1(reconstructed)
    activation = torch.relu(activation1)
    code = self.encoder_output_layer1(activation)
    code = torch.relu(code)
    activation = self.decoder_hidden_layer1(code)
    activation = torch.relu(activation)
    activation = self.decoder_output_layer1(activation)
    reconstructed = torch.sigmoid(activation)
    return reconstructed
</code></pre>
<p>when i train the model, the error in the first line of forward function<br>
please how i can solve the problem?</p>

username: ptrblck
cooked: <p>It seems you are trying to pass a numpy array to your model, while you should use PyTorch tensors.<br>
You could check it via:</p>
<pre><code class="lang-python">print(type(input))
</code></pre>
<p>before feeding <code>input</code> to your model.<br>
To transform the numpy array to a tensor use <code>input = torch.from_numpy(input)</code>.</p>

username: Islem_Maatar
cooked: <p>thanks but still the same error,<br>
this is my model : i want that the output of the DCT is the input of the first autoencoder and the out put of this autoencoder is the input of the second autoencoder</p>
<p>if <strong>name</strong>==‘<strong>main</strong>’:</p>
<h1>Load the training and testing dataset separately by calling the function for each of their root folder locations</h1>
<pre><code>X_train, y_train = load_data(root_folder_train)
X_test, y_test = load_data(root_folder_test)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
</code></pre>
<h1>Creating the model for EEG DATA:</h1>
<h1>1. DCT Application</h1>
<pre><code>tf_DCT=[]
N_samples = 256

for i in X_train:
    tf_DCT_train = DCT_funtion(i,N_samples)
    tf_DCT.append(tf_DCT_train)
input=torch.from_numpy(tf_DCT)
#print(tf_DCT_train.size)
#tf_DCT_test= DCT_funtionn(X_test,N_samples)
print("successful step")
</code></pre>
<p><span class="hashtag">#Model:</span><br>
<span class="hashtag">#Application</span> of autoencoder:</p>
<p>model=AE()<br>
classifiier= torch.nn.Softmax(model)<br>
optimizer=optim.Adam(classifiier.parameters(),lr=1e-3)<br>
criterion =torch.nn.MSELoss()</p>

username: ptrblck
cooked: <p>Could you post the stack trace and check the <code>type</code> of all inputs?</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging a bit easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: Islem_Maatar
cooked: <p>this is a stack trace of all type of variable<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/4/842c65dd215d5309c24de3f798b814684e6f6372.png" data-download-href="https://discuss.pytorch.org/uploads/default/842c65dd215d5309c24de3f798b814684e6f6372" title="Screenshot from 2020-06-06 19-41-23"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/842c65dd215d5309c24de3f798b814684e6f6372_2_690x249.png" alt="Screenshot from 2020-06-06 19-41-23" data-base62-sha1="iRg9bzLtYHOWqmQdyQ0Obrnh44y" width="690" height="249" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/842c65dd215d5309c24de3f798b814684e6f6372_2_690x249.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/842c65dd215d5309c24de3f798b814684e6f6372_2_1035x373.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/4/842c65dd215d5309c24de3f798b814684e6f6372.png 2x" data-dominant-color="44484A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2020-06-06 19-41-23</span><span class="informations">1050×379 81.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p><code>X_train</code>, <code>X_test</code>, <code>y_train</code>, and <code>y_test</code> are still numpy arrays, so you would have to transform them to tensors.</p>

username: Islem_Maatar
cooked: <p>Thanks for you, but a new error has appeared after the change of the array to tensor and the change i made in the implementation of the AE<br>
this is a stack trace of the error and the AE<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/6/9698bd51469e08c65d7e92bb82df521311f44485.png" data-download-href="https://discuss.pytorch.org/uploads/default/9698bd51469e08c65d7e92bb82df521311f44485" title="Screenshot from 2020-06-07 11-49-55"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/6/9698bd51469e08c65d7e92bb82df521311f44485.png" alt="Screenshot from 2020-06-07 11-49-55" data-base62-sha1="lueRxe7BsIubl37WBSVvUAuLYtD" width="690" height="85" data-dominant-color="323032"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2020-06-07 11-49-55</span><span class="informations">1038×128 18.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>This error is raised, if you try to pass a <code>Size</code> object to as an argument to the layer creation:</p>
<pre><code class="lang-python">x = torch.randn(10, 10)
nn.Linear(x.size(), 10)
&gt; TypeError: new(): argument 'size' must be tuple of ints, but found element of type torch.Size at pos 2
</code></pre>
<p>Based on your previous code snippet, <code>input_shape</code> should be defined as <code>input_shape = 256</code>, so I assume you might have changed the code in the meantime.</p>
<p>PS: Please don’t post screenshots, but paste the code directly and wrap it into three backticks ```.</p>

username: Islem_Maatar
cooked: <p>ok , but i want that the shape of DCT function be the input of the autoencoder that’s why i  define the input_shape as the the output of DCT.shape<br>
what advises you can give me and thanks a lot</p>

username: ptrblck
cooked: <p>You could either pass the size for all arguments as:</p>
<pre><code class="lang-python">x = torch.randn(10, 20)
nn.Linear(*x.size())
</code></pre>
<p>or index the desired shapes and add the other arguments manually:</p>
<pre><code class="lang-python">x = torch.randn(10, 20)
nn.Linear(x.size()[0], 30)
</code></pre>

username: Islem_Maatar
cooked: <p>thanks but a new error is appeared also,<br>
AttributeError: ‘int’ object has no attribute ‘softmax’<br>
How i solve this problem?</p>

username: ptrblck
cooked: <p>This line of code might yield errors, as you are passing the complete model as an argument to <code>nn.Softmax</code>, which is wrong:</p>
<pre><code class="lang-python">classifiier= torch.nn.Softmax(model)
</code></pre>
<p>Feel free to post executable code by wrapping in into three backticks ```, so that we can debug it.</p>