0

I am testing my CNN model, but keep on getting error "AttributeError: 'numpy.ndarray' object has no attribute 'relu'".

my dataset is extracted by below code:

import torch
from torch.utils.data import Dataset, DataLoader
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np

class MyDataset(Dataset):
    def __init__(self, data, target, transform=None):
        self.data = torch.from_numpy(data).float()
        self.target = torch.from_numpy(target).long()
        self.transform = transform

    def __getitem__(self, index):
        x = self.data[index]
        y = self.target[index]

        if self.transform:
            x = self.transform(x)

        return x, y

    def __len__(self):
        return len(self.data)

numpy_data = np.random.randn(100,3,224,224) # 10 samples, image size = 224 x 224 x 3
numpy_target = np.random.randint(0,5,size=(100))

dataset = MyDataset(numpy_data, numpy_target)

my model is very simple as below:

class Network(nn.Module): 
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(in_channels = 3, out_channels = 36, kernel_size = 100)

    def forward(self, t): 

        t = self.conv1(t)
        print(t.shape)
        print(type(t))
        t = F.relu(t)
        print(t.shape)

        return t

I test model using below:

sample, target = next(iter(dataset))
network=Network()
pred = network(sample.unsqueeze(0))

I got below result and error:

torch.Size([1, 6, 125, 125])
<class 'torch.Tensor'>

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-40-37e58cfe971f> in <module>
----> 1 pred = network(sample.unsqueeze(0))

C:\Miniconda\envs\py37_default\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

<ipython-input-30-0d0592ad705d> in forward(self, t)
     22         print(t.shape)
     23         print(type(t))
---> 24         t = F.relu(t)
     25         print(t.shape)
     26         #t = F.max_pool2d(t, kernel_size=2, stride=2)

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

I could not figure out why, type(t) does output as , why error is showing it is numpy.ndarray?

2
  • 1
    Where do you define F? F is the NumPy array, not t. Commented May 24, 2020 at 18:27
  • yes I defined F, forgot to include here, just added. But still the same error Commented May 25, 2020 at 0:28

1 Answer 1

1

Where is F defined? F seems to be the numpy array.

Did you maybe mean to do:

import torch.nn.functional as F? Otherwise, the relu function isn't defined anywhere.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I did have import torch from torch.autograd import Variable import torch.nn as nn import torch.optim as optim import torch.nn.functional as F, but still the same error
Right, but according to the PyTorch Documentation the ReLU function belongs to the torch.nn module, not to torch.nn.functional. Perhaps try rewriting the line as t = nn.ReLU(t) to see if that makes a difference?
Thanks, this solved it for me. PyCharm autoimported F as something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.