TypeError: torch.nn.modules.activation.ReLU is not a Module subclass

username: Gaju27
cooked: <p>import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()</p>
<pre><code>    self.layer1=nn.Sequential(
        nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1),
        nn.ReLU(),
        nn.BatchNorm2d(16),
    )
</code></pre>
<p>When I run the above code I get the below error.</p>
<p>TypeError: torch.nn.modules.activation.ReLU is not a Module subclass</p>

username: ptrblck
cooked: <p>Which PyTorch version are you using? (You can see it via <code>print(torch.__version__)</code>).<br>
The code is running fine on my system.</p>

username: Gaju27
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="71600">
<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>print(torch.__version__)</code> )</p>
</blockquote>
</aside>
<p>print(torch.<strong>version</strong>)</p>
<p>1.2.0</p>

username: ptrblck
cooked: <p>How did you install PyTorch? Could you create a new virtual environment and reinstall it?</p>

username: Gaju27
cooked: <p>Do i have to uninstall first?<br>
Can you please help best practice for creating virtual Env.</p>

username: ptrblck
cooked: <p>You don’t need to change your current setup, if you create a new virtual environment.</p>
<p>I’m personally using conda, as I think their env setup is convenient to switch quickly and in the worst case, you can just delete a “broken” environment.</p>
<ul>
<li>download Anaconda</li>
<li>create a new env via: <code>conda create -n my_env_name Python=3.7 Anaconda</code> (the last <code>Anaconda</code> argument pulls in a lot of libs like numpy, matplitlib etc., so you might want to skip it)</li>
<li>activate the env via: <code>conda activate my_env_name</code>
</li>
<li>install the PyTorch binaries</li>
</ul>

username: Gaju27
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="71600">
<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>conda activate my_env_name</p>
</blockquote>
</aside>
<p>Created new env and installed pytorch libraries</p>
<p>(eva-4) C:\Users\gajanana_ganjigatti&gt;python -m ipykernel install --user --name=my_env_name<br>
Installed kernelspec eva-4 in C:\Users\gajanana_ganjigatti\AppData\Roaming\jupyter\kernels\my_env_name</p>
<p>getting the same error below.</p>
<p> in <strong>init</strong>(self)<br>
20             nn.ReLU,<br>
21             nn.BatchNorm2d(64),<br>
—&gt; 22             nn.MaxPool2d(2, 2),<br>
23         )<br>
24</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\container.py in <strong>init</strong>(self, *args)<br>
51         else:<br>
52             for idx, module in enumerate(args):<br>
—&gt; 53                 self.add_module(str(idx), module)<br>
54<br>
55     def _get_item_by_idx(self, iterator, idx):</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in add_module(self, name, module)<br>
192         if not isinstance(module, Module) and module is not None:<br>
193             raise TypeError(“{} is not a Module subclass”.format(<br>
 → 194                 torch.typename(module)))<br>
195         elif not isinstance(name, torch._six.string_classes):<br>
196             raise TypeError(“module name should be a string. Got {}”.format(</p>
<p><strong>TypeError: torch.nn.modules.activation.ReLU is not a Module subclass</strong></p>

username: ptrblck
cooked: <p>Could you make sure you are using the posted code snippet and do not forget to create an instance of <code>nn.ReLU</code>?<br>
This code will yield your error:</p>
<pre><code class="lang-python">layer1=nn.Sequential(
    nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1),
    nn.ReLU, # missing () !!!
    nn.BatchNorm2d(16),
)
</code></pre>
<p>while your posted code should work.</p>

username: Gaju27
cooked: <p>thanks a ton. Silly mistakes made me create another env.</p>
<p>issue fixed <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> Cowabunga <img src="https://discuss.pytorch.org/images/emoji/apple/muscle.png?v=9" title=":muscle:" class="emoji" alt=":muscle:"></p>

username: ooodragon
cooked: <p>One question!<br>
I’m not able to see my activation modules when I do</p>
<pre><code class="lang-auto">print(model)
</code></pre>
<p>only convolution, batchnorm, avgpool, and etc.<br>
Is there any way to see my activation layer?</p>
<p>I want to load parameters from a pretrained model and just change the activation layers (sigmoid to ReLU for example…). Would this be achievable??</p>

username: ptrblck
cooked: <p>You should be able to see the activation modules, if you’ve created them as modules.<br>
Note that functional API calls won’t be printed in this way, i.e. if you’ve used <code>F.relu()</code> in the <code>forward</code>.<br>
Could this be the case for your model?</p>

username: DHARMENDRA_PRAJAPAT
cooked: <p>Instead of using nn.ReLU in Sequential block, create an instance of nn.ReLU and use that instance inside the sequential block.<br>
Eg…</p>
<p>relu1 = nn.ReLU()<br>
self.layer1=nn.Sequential(<br>
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1),<br>
relu1,<br>
nn.BatchNorm2d(16),<br>
)</p>