Hi, i,ve a problem.plz help me:NotImplementedError

username: milad66
cooked: <p>%reload_ext autoreload</p>
<p>%autoreload 2</p>
<p>%matplotlib inline</p>
<p>import math<br>
import time<br>
import os<br>
import glob<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
from tqdm import tqdm<br>
from PIL import Image<br>
from sklearn.metrics import confusion_matrix</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
from torch.autograd import Variable</p>
<p>import torchvision<br>
import torchvision.datasets as datasets<br>
import torchvision.models as models<br>
import torchvision.transforms as transforms</p>
<h1>Our libraries</h1>
<p>from train import train_model<br>
from model_utils import *<br>
from predict_utils import *<br>
from vis_utils import *</p>
<h1>some initial setup</h1>
<p>np.set_printoptions(precision=2)<br>
use_gpu = torch.cuda.is_available()<br>
np.random.seed(1234)</p>
<p>use_gpu</p>
<p>DATA_DIR = “G:/0-HusheMasnuei-DeepLearning-Machine Learning/Dataset/ACNP/Dev/”</p>
<p>sz = 224<br>
batch_size = 8</p>
<p>os.listdir(DATA_DIR)</p>
<p>trn_dir = f’{DATA_DIR}train’<br>
val_dir = f’{DATA_DIR}valid’</p>
<p>os.listdir(trn_dir)</p>
<p>os.listdir(val_dir)</p>
<p>trn_fnames =glob.glob(f’{trn_dir}/<em>/</em>.jpg’)<br>
trn_fnames [:2]</p>
<p>img = plt.imread(trn_fnames[6])<br>
plt.imshow(img);</p>
<p>trn_fnames =glob.glob(f’{trn_dir}/<em>/</em>.png’)<br>
trn_fnames [:2]</p>
<p>train_ds = datasets.ImageFolder(trn_dir)</p>
<p>train_ds.classes</p>
<p>train_ds.class_to_idx</p>
<p>train_ds.root</p>
<p>train_ds.imgs</p>
<p>tfms = transforms.Compose([<br>
transforms.Resize((sz, sz)),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.255])</p>
<pre><code>])
</code></pre>
<p>train_ds = datasets.ImageFolder(trn_dir, transform=tfms)<br>
valid_ds = datasets.ImageFolder(val_dir, transform=tfms)</p>
<p>len(train_ds),len(valid_ds)</p>
<p>type(train_ds.transforms)</p>
<p>train_dl = torch.utils.data.DataLoader(train_ds, batch_size = batch_size, shuffle = True, num_workers = 5)<br>
valid_dl = torch.utils.data.DataLoader(valid_ds, batch_size = batch_size, shuffle = True, num_workers= 5 )</p>
<p>inputs, targets = next(iter(train_dl))<br>
out = torchvision.utils.make_grid(inputs, padding=3)<br>
plt.figure(figsize=(16,12))<br>
imshow(out, title=‘Random images from training data’)</p>
<p>class SimpleCNN(nn.Module) :</p>
<pre><code>def __init__(self) :
    
    super (SimpleCNN, self).__init__()
    
    self.conv1 = nn.Sequential(
    
        nn.Conv2d(3 , 16 , kernel_size = 5, padding = 2),
        
        nn.BatchNorm2d(16),
        
        nn.ReLU(),
        
        nn.MaxPool2d(2) 
    
    
    )

    
    self.conv2 = nn.Sequential(
    
        nn.Conv2d(16 , 32 , kernel_size = 5, padding = 2),
        
        nn.BatchNorm2d(32),
        
        nn.ReLU(),
        
        nn.MaxPool2d(2) 
    
    
    )
    
    self.fc = nn.Linear(56 * 56 * 32, 2)
    
    def forward(self , x):
        
        out = self.cov1(x)
        out = self.cov2(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out
</code></pre>
<p>model = SimpleCNN()<br>
if use_gpu:<br>
model = model.cuda()</p>
<p>model</p>
<p><span class="hashtag">#model</span> = SimpleCNN()<br>
<span class="hashtag">#if</span> torch.cuda.is_available():</p>
<h1>device = ‘cuda’</h1>
<p><span class="hashtag">#else:</span></p>
<h1>device = ‘cpu’</h1>
<p><span class="hashtag">#model</span>.to(device)</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(model.parameters(), lr=0.002, momentum=0.9)</p>
<p>num_epochs = 10<br>
losses = []<br>
for epoch in range(num_epochs):<br>
for i, (inputs, targets) in enumerate(train_dl):<br>
inputs = to_var(inputs)<br>
targets = to_var(targets)</p>
<pre><code>    # forwad pass
    optimizer.zero_grad()
    outputs = model(inputs)
    
    # loss
    loss = criterion(outputs, targets)
    losses += [loss.data[0]]
    # backward pass
    loss.backward()
    
    # update parameters
    optimizer.step()
    
    # report
    if (i + 1) % 50 == 0:
        print('Epoch [%2d/%2d], Step [%3d/%3d], Loss: %.4f'
              % (epoch + 1, num_epochs, i + 1, len(train_ds) // batch_size, loss.data[0]))
</code></pre>
<hr>
<hr>
<p>NotImplementedError                       Traceback (most recent call last)<br>
 in <br>
8         # forwad pass<br>
9         optimizer.zero_grad()<br>
—&gt; 10         outputs = model(inputs)<br>
11<br>
12         # loss</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in forward(self, *input)<br>
94             registered hooks while the latter silently ignores them.<br>
95         “”"<br>
—&gt; 96         raise NotImplementedError<br>
97<br>
98     def register_buffer(self, name, tensor):</p>
<p>NotImplementedError:</p>

username: ptrblck
cooked: <p>Your code isn’t formatted correctly (you can post code snippets by wrapping them into three backticks ```), but based on the error message and the current posted code, I guess that the <code>forward</code> method might be defined inside the <code>__init__</code> method.<br>
Could you check it and if that’s the case correct the indentation?</p>

username: milad66
cooked: <p>Thanks.<br>
that was the case correct the indentation<br>
Solved.</p>