简体   繁体   中英

PyTorch error - 'numpy.ndarray' object has no attribute 'relu'

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM