Unable to Use Loss Function

username: drbeethoven
cooked: <p>Hello, I created the following somewhat succinct neural net (code cited later on):</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20210624-060149-RC00_381218252" class="site-icon" width="16" height="16">

      <a href="https://colab.research.google.com/drive/1xTE_Ip05QzUOPV-C7LH1ga2w-tRmHhin?usp=sharing" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://colab.research.google.com/drive/1xTE_Ip05QzUOPV-C7LH1ga2w-tRmHhin?usp=sharing" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Now the original version uses a simple two layer network, but I was trying to make a larger version using an input layer, a module list, then an output layer. However, it seems the “loss” function is not implemented for a sequential NN if it has a module list (I think), as I am getting an error when using the larger neural net with a label of “Not Implemented Error” on the “loss_fun” call.</p>
<p>The initialization of the nn is the only step I changed in the code. You can see the before version in the code (uncommented), and the after in the code as well (commented) Before:<br>
class FirstNetwork_v2(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
torch.manual_seed(0)<br>
<a href="http://self.net" rel="noopener nofollow ugc">self.net</a> = nn.Sequential( <span class="hashtag">#sequential</span> operation<br>
nn.Linear(2, 2),<br>
nn.Sigmoid(),<br>
nn.Linear(2, 4),<br>
nn.Softmax())</p>
<p>After:<br>
class FirstNetwork_v2(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
torch.manual_seed(0)</p>
<pre><code>    self.net = nn.Sequential(
      nn.Linear(2, 10, bias=False),
      nn.ModuleList(),
      nn.Linear(10, 4, bias=False),
      nn.Softmax())

    for _ in range(3):
      self.net[1].append(nn.Linear(10, 10, bias=False))
      self.net[1].append(nn.ReLU())
</code></pre>
<p>In conclusion, is there any loss function that allows me to use more complicated neural nets? Thank you very much!</p>
<p>In the spirit of citing original work, this neural net initially used the following guide:</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://www.marktechpost.com/wp-content/uploads/2021/04/AI-PAPERS-SUMMARIES-1.png" class="site-icon" width="16" height="16">

      <a href="https://www.marktechpost.com/2019/06/30/building-a-feedforward-neural-network-using-pytorch-nn-module/" target="_blank" rel="noopener nofollow ugc" title="04:47PM - 30 June 2019">MarkTechPost – 30 Jun 19</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:79/44;"><img src="https://i1.wp.com/www.marktechpost.com/wp-content/uploads/2019/06/cropped-randall-bruder-3_I4NVI9d1k-unsplash.jpg?fit=2048%2C1151&amp;ssl=1" class="thumbnail" width="79" height="44"></div>

<h3><a href="https://www.marktechpost.com/2019/06/30/building-a-feedforward-neural-network-using-pytorch-nn-module/" target="_blank" rel="noopener nofollow ugc">Building a Feedforward Neural Network using Pytorch NN Module</a></h3>

  <p>we will discuss how to build a feed-forward neural network using TORCH.NN module classes such as Functional, Sequential, Parameter, Linear and Optim</p>

  <p>
    <span class="label1">Est. reading time: 17 minutes</span>
  </p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: ptrblck
cooked: <p>The error is raised by <code>nn.ModuleList</code>, which doesn’t implement a <code>forward</code> method, but acts as a <code>list</code> with the additional property to register all parameters and buffers into the parent <code>nn.Module</code>.<br>
You could use another <code>nn.Sequential</code> container and <code>.add_module</code> instead:</p>
<pre><code class="lang-python">net = nn.Sequential(
    nn.Linear(2, 10, bias=False),
    nn.Sequential(),
    nn.Linear(10, 4, bias=False),
    nn.Softmax(dim=1))

for i in range(3):
    net[1].add_module(str(i), nn.Linear(10, 10, bias=False))
    net[1].add_module(str(i)+'relu', nn.ReLU())
    
x = torch.randn(1, 2)
out = net(x)
</code></pre>