'numpy.ndarray' object has no attribute 'cuda'

username: Sarra_Bouzidi
cooked: <p>en tapant :<br>
biasRestaurant = to_np(m.ib(V(topRestIdx))) <span class="hashtag">#converting</span> the torch embedding to numpy matrix</p>
<h2>j’aurai cette erreur :</h2>
<p>AttributeError                            Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 biasRestaurant = to_np(m.ib(V(topRestIdx))) <span class="hashtag">#converting</span> the torch embedding to numpy matrix</p>
<p>2 frames<br>
/usr/local/lib/python3.6/dist-packages/fastai/core.py in to_gpu(x, *args, **kwargs)<br>
28 def to_gpu(x, *args, **kwargs):<br>
29     if torch.cuda.is_available():<br>
—&gt; 30         return x.cuda(*args, **kwargs)<br>
31     else:<br>
32         return x</p>
<hr>
<p>voici le code core.py</p>
<hr>
<p>AttributeError: ‘numpy.ndarray’ object has no attribute ‘cuda’</p>
<p>from .imports import *</p>
<p>from .torch_imports import *</p>
<p>def sum_geom(a,r,n):</p>
<pre><code>return a*n if r==1 else math.ceil(a*(1-r**n)/(1-r))
</code></pre>
<p>conv_dict = {np.dtype(‘int8’): torch.LongTensor, np.dtype(‘int16’): torch.LongTensor,</p>
<pre><code>np.dtype('int32'): torch.LongTensor, np.dtype('int64'): torch.LongTensor,

np.dtype('float32'): torch.FloatTensor, np.dtype('float64'): torch.FloatTensor}
</code></pre>
<p>def T(a):</p>
<pre><code>a = np.array(a)

if a.dtype in (np.int8, np.int16, np.int32, np.int64):

    return torch.LongTensor(a.astype(np.int64))

if a.dtype in (np.float32, np.float64):

    return torch.FloatTensor(a.astype(np.float32))

raise NotImplementedError
</code></pre>
<p>def V_(x):  return to_gpu(x, async=True) if isinstance(x, Variable) else Variable(to_gpu(x, async=True))</p>
<p>def V(x):   return [V_(o) for o in x] if isinstance(x,list) else V_(x)</p>
<p>def VV_(x): return to_gpu(x, async=True) if isinstance(x, Variable) else Variable(to_gpu(x, async=True), volatile=True)</p>
<p>def VV(x):  return [VV_(o) for o in x] if isinstance(x,list) else VV_(x)</p>
<p>def to_np(v):</p>
<pre><code>if isinstance(v, Variable): v=v.data

return v.cpu().Numpy ( )
</code></pre>
<p>def to_gpu(x, *args, **kwargs):</p>
<pre><code>if torch.cuda.is_available():

    return x.cuda(*args, **kwargs)

else:

    return x
</code></pre>
<p>def noop(*args, **kwargs): return</p>
<p>def split_by_idxs(seq, idxs):</p>
<pre><code>last, sl = 0, len(seq)

for idx in idxs:

    yield seq[last:idx]

    last = idx

yield seq[last:]
</code></pre>
<p>def trainable_params_(m):</p>
<pre><code>return [p for p in m.parameters() if p.requires_grad]
</code></pre>
<p>def chain_params§:</p>
<pre><code>if isinstance(p, (list,tuple)):

    return list(chain(*[trainable_params_(o) for o in p]))

return trainable_params_(p)
</code></pre>
<p>def set_trainable_attr(m,b):</p>
<pre><code>m.trainable=b

for p in m.parameters(): p.requires_grad=b
</code></pre>
<p>def apply_leaf(m, f):</p>
<pre><code>c = children(m)

if isinstance(m, nn.Module): f(m)

if len(c)&gt;0:

    for l in c: apply_leaf(l,f)
</code></pre>
<p>def set_trainable(l, b):</p>
<pre><code>apply_leaf(l, lambda m: set_trainable_attr(m,b))
</code></pre>
<p>def SGD_Momentum(momentum):</p>
<pre><code>return lambda *args, **kwargs: optim.SGD(*args, momentum=momentum, **kwargs)
</code></pre>
<p>def one_hot(a,c): return np.eye©[a]</p>

username: ptrblck
cooked: <p>The <code>cuda()</code> method is defined for tensors, while it seems you are calling it on a numpy array.<br>
Try to transform the numpy array to a tensor before calling <code>tensor.cuda()</code> via: <code>tensor = torch.from_numpy(array)</code>.</p>

username: Sarra_Bouzidi
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="80260">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>tensor = torch.from_numpy(array)</code> .</p>
</blockquote>
</aside>
<p>ou j’ajoute ( tensor = torch.from_numpy(array)) ? dans le code source s’il vous plait ?</p>

username: ptrblck
cooked: <p>Hi Sarra,</p>
<p>could you use a translation service, please, as my French is quite bad? <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=12" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>
<p>From Deepl:</p>
<blockquote>
<p>“or I add ( tensor = torch.from_numpy(array)) ? in the source code please ?”</p>
</blockquote>
<p>If I understand it correctly, you would like to know, where to add this line of code?<br>
Try to add it right before the <code>.cuda()</code> call on the array.</p>

username: Sarra_Bouzidi
cooked: <p>yes this is my question <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> ok i’ll try<br>
but can i get your email ok other way to contact you please ? because i didn’t know where to put it <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

username: ptrblck
cooked: <p>I guess <code>topRestIdx</code> might be a numpy array, so you could try to call:</p>
<pre><code class="lang-python">topRestIdx = torch.from_numpy(topRestIdx)
</code></pre>
<p>before using it.</p>
<p>Also, <code>Variables</code> are deprecated since PyTorch <code>0.4</code>, so you can use tensors now.</p>
<p>If your code still doesn’t work, could you please post a small code snippet to reproduce this issue by wrapping it into three backticks ```? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Sarra_Bouzidi
cooked: <p>you saved my life <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> thank you verry much sir <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

username: ptrblck
cooked: <p>Haha, good to know it’s working now! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Islem_Maatar
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="80260">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>tensor = torch.from_numpy(array)</code> .</p>
</blockquote>
</aside>
<p>Hi, i have the same error and when i use this instruction new error is appear , " RuntimeError: mat1 dim 1 must match mat2 dim 0 "<br>
and this my code<br>
for batch_idx, inputs in enumerate(validloader):<br>
inputs= torch.from_numpy(inputs)<br>
if use_cuda:<br>
inputs = inputs.cuda()<br>
inputs = Variable(inputs)<br>
hidden = self.encode(inputs)<br>
Any help please and thanks a lot</p>

username: ptrblck
cooked: <p>Could you check the stack trace and see which function is raising this error?<br>
Once you’ve isolated this function, could you post the shapes of all input tensors, so that we can have a look why this issue is raised?<br>
If you cannot isolate the function, which raises the error, please post the complete stack trace here. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Islem_Maatar
cooked: <p>this is the full code<br>
class DenoisingAutoencoder(nn.Module):<br>
def <strong>init</strong>(self, in_features, out_features, activation=“relu”,<br>
dropout=0.2, tied=False):<br>
super(self.<strong>class</strong>, self).<strong>init</strong>()<br>
self.weight = Parameter(torch.Tensor(out_features, in_features))<br>
if tied:<br>
self.deweight = self.weight.t()<br>
else:<br>
self.deweight = Parameter(torch.Tensor(in_features, out_features))<br>
self.bias = Parameter(torch.Tensor(out_features))<br>
self.vbias = Parameter(torch.Tensor(in_features))</p>
<pre><code>    if activation=="relu":
        self.enc_act_func = nn.ReLU()
    elif activation=="sigmoid":
        self.enc_act_func = nn.Sigmoid()
    self.dropout = nn.Dropout(p=dropout)

    self.reset_parameters()

def reset_parameters(self):
    stdv = 1. / math.sqrt(self.weight.size(1))
    self.weight.data.uniform_(-stdv, stdv)
    self.bias.data.uniform_(-stdv, stdv)
    stdv = 1. / math.sqrt(self.deweight.size(1))
    self.deweight.data.uniform_(-stdv, stdv)
    self.vbias.data.uniform_(-stdv, stdv)

def forward(self, x):
    return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

def encode(self, x, train=True):
    if train:
        self.dropout.train()
    else:
        self.dropout.eval()
    return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

def encodeBatch(self, dataloader):
    use_cuda = torch.cuda.is_available()
    encoded = []
    for batch_idx, inputs in enumerate(dataloader):
        inputs = np.reshape(inputs, [-1, 512])
        inputs = torch.Tensor(inputs)
        #inputs = inputs.view(inputs.size(0), -1).float()
        if use_cuda:
            inputs = inputs.cuda()
        inputs = Variable(inputs)
        hidden = self.encode(inputs, train=False)
        encoded.append(hidden.data.cpu())

    encoded = torch.cat(encoded, dim=0)
    return encoded

def decode(self, x, binary=False):
    if not binary:
        return F.linear(x, self.deweight, self.vbias)
    else:
        return F.sigmoid(F.linear(x, self.deweight, self.vbias))

def fit(self, trainloader, validloader, lr=0.001, batch_size=128, num_epochs=10, corrupt=0.3,
    loss_type="mse"):
    """
    data_x: FloatTensor
    valid_x: FloatTensor
    """
    use_cuda = torch.cuda.is_available()
    if use_cuda:
        self.cuda()
    print("=====Denoising Autoencoding layer=======")
    optimizer = optim.Adam(filter(lambda p: p.requires_grad, self.parameters()), lr=lr)
    if loss_type=="mse":
        criterion = MSELoss()
    elif loss_type=="cross-entropy":
        criterion = BCELoss()

    # validate
    total_loss = 0.0
    total_num = 0
    for batch_idx, inputs in enumerate(validloader):
        #inputs = torch.Tensor(inputs)
        #inputs = np.reshape(inputs,[-1,512])
        #inputs=torch.Tensor(inputs)
        #inputs = inputs.view(inputs.size(0), -1).float()
        inputs= torch.from_numpy(inputs)
        print(type(inputs))
        if use_cuda:
            inputs = inputs.cuda()
        inputs = Variable(inputs)
        hidden = self.encode(inputs)
        if loss_type=="cross-entropy":
            outputs = self.decode(hidden, binary=True)
        else:
            outputs = self.decode(hidden)

        valid_recon_loss = criterion(outputs, inputs)
        total_loss += valid_recon_loss.data* len(inputs)
        total_num += inputs.size()[0]

    valid_loss = total_loss / total_num
    print("#Epoch 0: Valid Reconstruct Loss: %.3f" % (valid_loss))</code></pre>

username: ptrblck
cooked: <p>Thanks for the code.<br>
It works fine with some dumme inputs:</p>
<pre><code class="lang-python">model = DenoisingAutoencoder(10, 10)
model(torch.randn(2, 10))
</code></pre>
<p>Feel free to post an executable code snippet, which could reproduce the error you are seeing.</p>
<p>PS: you can post code snippets by wrapping them into three backticks, which makes debugging 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>sorry , but i can’t understand the code change what you are doing<br>
this is my full code for denoisingAutoencoder:</p>
<pre><code class="lang-python">import torch
import torch.nn as nn
from torch.nn import Parameter
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms
from torch.autograd import Variable
import tensorflow as tf
import numpy as np
import math
from udlp.utils import Dataset, masking_noise
from udlp.ops import MSELoss, BCELoss

class DenoisingAutoencoder(nn.Module):
    def __init__(self, in_features, out_features, activation="relu", 
        dropout=0.2, tied=False):
        super(self.__class__, self).__init__()
        self.weight = Parameter(torch.Tensor(out_features, in_features))
        if tied:
            self.deweight = self.weight.t()
        else:
            self.deweight = Parameter(torch.Tensor(in_features, out_features))
        self.bias = Parameter(torch.Tensor(out_features))
        self.vbias = Parameter(torch.Tensor(in_features))
        
        if activation=="relu":
            self.enc_act_func = nn.ReLU()
        elif activation=="sigmoid":
            self.enc_act_func = nn.Sigmoid()
        self.dropout = nn.Dropout(p=dropout)

        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        self.bias.data.uniform_(-stdv, stdv)
        stdv = 1. / math.sqrt(self.deweight.size(1))
        self.deweight.data.uniform_(-stdv, stdv)
        self.vbias.data.uniform_(-stdv, stdv)

    def forward(self, x):
        return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

    def encode(self, x, train=True):
        if train:
            self.dropout.train()
        else:
            self.dropout.eval()
        return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

    def encodeBatch(self, dataloader):
        use_cuda = torch.cuda.is_available()
        encoded = []
        for batch_idx, inputs in enumerate(dataloader):
            inputs = np.reshape(inputs, [-1, 512])
            inputs = torch.Tensor(inputs)
            #inputs = inputs.view(inputs.size(0), -1).float()
            if use_cuda:
                inputs = inputs.cuda()
            inputs = Variable(inputs)
            hidden = self.encode(inputs, train=False)
            encoded.append(hidden.data.cpu())

        encoded = torch.cat(encoded, dim=0)
        return encoded

    def decode(self, x, binary=False):
        if not binary:
            return F.linear(x, self.deweight, self.vbias)
        else:
            return F.sigmoid(F.linear(x, self.deweight, self.vbias))

    def fit(self, trainloader, validloader, lr=0.001, batch_size=128, num_epochs=10, corrupt=0.3,
        loss_type="mse"):
        """
        data_x: FloatTensor
        valid_x: FloatTensor
        """
        use_cuda = torch.cuda.is_available()
        if use_cuda:
            self.cuda()
        print("=====Denoising Autoencoding layer=======")
        optimizer = optim.Adam(filter(lambda p: p.requires_grad, self.parameters()), lr=lr)
        if loss_type=="mse":
            criterion = MSELoss()
        elif loss_type=="cross-entropy":
            criterion = BCELoss()

        # validate
        total_loss = 0.0
        total_num = 0
        for batch_idx, inputs in enumerate(validloader):
            #inputs = inputs.view(inputs.size(0), -1).float()
            inputs= np.array(torch.from_numpy(inputs))
            print(type(inputs))
            if use_cuda:
                inputs = inputs.cuda()

            inputs = Variable(inputs)
            hidden = self.encode(inputs)
            if loss_type=="cross-entropy":
                outputs = self.decode(hidden, binary=True)
            else:
                outputs = self.decode(hidden)

            valid_recon_loss = criterion(outputs, inputs)
            total_loss += valid_recon_loss.data* len(inputs)
            total_num += inputs.size()[0]

        valid_loss = total_loss / total_num
        print("#Epoch 0: Valid Reconstruct Loss: %.3f" % (valid_loss))

        for epoch in range(num_epochs):
            # train 1 epoch
            train_loss = 0.0
            for batch_idx, inputs in enumerate(trainloader):
                #inputs = inputs.view(inputs.size(0), -1).float()
                inputs= np.reshape(inputs,[-1,512])
                inputs=torch.Tensor(inputs)
                inputs_corr = masking_noise(inputs, corrupt)
                if use_cuda:
                    inputs = inputs.cuda()
                    inputs_corr = inputs_corr.cuda()
                optimizer.zero_grad()
                inputs = Variable(inputs)
                inputs_corr = Variable(inputs_corr)

                hidden = self.encode(inputs_corr)
                if loss_type=="cross-entropy":
                    outputs = self.decode(hidden, binary=True)
                else:
                    outputs = self.decode(hidden)
                recon_loss = criterion(outputs, inputs)
                train_loss += recon_loss.data*len(inputs)
                recon_loss.backward()
                optimizer.step()

            # validate
            valid_loss = 0.0
            for batch_idx,inputs in enumerate(validloader):
                inputs = np.reshape(inputs, [-1, 512])
                inputs = torch.Tensor(inputs)
                #inputs = inputs.view(inputs.size(0), -1).float()
                if use_cuda:
                    inputs = inputs.cuda()
                inputs = Variable(inputs)
                hidden = self.encode(inputs, train=False)
                if loss_type=="cross-entropy":
                    outputs = self.decode(hidden, binary=True)
                else:
                    outputs = self.decode(hidden)

                valid_recon_loss = criterion(outputs, inputs)
                valid_loss += valid_recon_loss.data * len(inputs)

            print("#Epoch %3d: Reconstruct Loss: %.3f, Valid Reconstruct Loss: %.3f" % (
                epoch+1, recon_loss , valid_recon_loss))
</code></pre>

username: ptrblck
cooked: <p>The model is still working, if I use some random values to initialize it and run a simple forward pass:</p>
<pre><code class="lang-python">model = DenoisingAutoencoder(10, 10)
out = model(torch.randn(1, 10))
</code></pre>
<p>Could you post the code snippet where you are initializing the model and which input shape you are using?<br>
If you are passing numpy arrays as the input, make sure to transform them to PyTorch tensors via <code>torch.from_numpy</code>.</p>

username: Islem_Maatar
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="14" data-topic="80260">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<pre><code class="lang-auto">model = DenoisingAutoencoder(10, 10)
out = model(torch.randn(1, 10))
</code></pre>
<p>Could you post the code snippet where you are initializing the model and which input shape you are using?<br>
If you are passing numpy arrays as the input, make sure to transform them to PyTorch tensors via <code>torch.from_numpy</code> .</p>
</blockquote>
</aside>
<p>this is the code to train data:<br>
“”"<br>
X_train, y_train = load_data(root_folder_train)<br>
X_test, y_test = load_data(root_folder_test)<br>
in_features = 512<br>
out_features = 256</p>
<pre><code>X_train= torch.Tensor(X_train)
X_test=torch.Tensor(X_test)
sdae = StackedDAE(input_dim=512, z_dim=10, binary=True,
    encodeLayer=[256,100,10], decodeLayer=[10,100,256], activation="relu",
    dropout=0)
sdae.pretrain(X_train,X_test, lr=args.lr, batch_size=args.batch_size,
              num_epochs=args.pretrainepochs, corrupt=0.3, loss_type="cross-entropy")
sdae.save_model("model/sdae.pt")
sdae.fit(X_train,X_test, lr=args.lr, num_epochs=args.epochs, corrupt=0.3, loss_type="cross-entropy")
</code></pre>
<p>“”“”</p>

username: ptrblck
cooked: <p>In these lines of code you are transforming the tensor back to a numpy array, which would yield this error:</p>
<pre><code class="lang-python">            inputs= np.array(torch.from_numpy(inputs))
            print(type(inputs))
            if use_cuda:
                inputs = inputs.cuda()
</code></pre>
<p>remove the <code>np.array</code> call and just use tensors.</p>
<p>PS: <code>Variables</code> are deprecated since PyTorch <code>0.4</code>, so you can use tensors now. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>