TypeError: ‘Adam’ object is not callable

username: Ibrahim_Banat
cooked: <p>the file of this function is imported and when running (python train.py --epochs 1) i got this error:</p>
<pre><code class="lang-auto">--------------Training is starting------------- 
Traceback (most recent call last):
  File "train.py", line 48, in &lt;module&gt;
    suppFunctions.train_network(model, criterion, optimizer, trainloader, epochs, 20, power)
  File "/home/workspace/ImageClassifier/suppFunctions.py", line 128, in train_network
    loss = criterion(outputs, labels)
TypeError: 'Adam' object is not callable
</code></pre>
<p>the function <code>train_network</code>:</p>
<pre><code class="lang-auto">def train_network(model, criterion, optimizer, loader, epochs = 3, print_every=20, power='gpu'):
    
    steps = 0
    running_loss = 0

    print("--------------Training is starting------------- ")
    for e in range(epochs):
        running_loss = 0
        for ii, (inputs, labels) in enumerate(loader):
            steps += 1
            if torch.cuda.is_available() and power =='gpu':
                inputs, labels = inputs.to('cuda'), labels.to('cuda')

            optimizer.zero_grad()

            # Forward and backward passes
            outputs = model.forward(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            if steps % print_every == 0:
                model.eval()
                vlost = 0
                accuracy=0


                for ii, (inputs2,labels2) in enumerate(vloader):
                    optimizer.zero_grad()
                    if torch.cuda.is_available():
                        inputs2, labels2 = inputs2.to('cuda:0') , labels2.to('cuda:0')
                        model.to('cuda:0')

                    with torch.no_grad():
                        outputs = model.forward(inputs2)
                        vlost = criterion(outputs,labels2)
                        ps = torch.exp(outputs).data
                        equality = (labels2.data == ps.max(1)[1])
                        accuracy += equality.type_as(torch.FloatTensor()).mean()

                vlost = vlost / len(vloader)
                accuracy = accuracy /len(vloader)



                print("Epoch: {}/{}... ".format(e+1, epochs),
                      "Loss: {:.4f}".format(running_loss/print_every),
                      "Validation Lost {:.4f}".format(vlost),
                       "Accuracy: {:.4f}".format(accuracy))


                running_loss = 0


    print("-------------- Finished training -----------------------")
    print("Dear User I the ulitmate NN machine trained your model. It required")
    print("----------Epochs: {}------------------------------------".format(epochs))
    print("----------Steps: {}-----------------------------".format(steps))
    print("That's a lot of steps")
</code></pre>

username: albanD
cooked: <p>Hi,</p>
<p>Given the names, I guess you mixup the optimizer and the loss function? Here, <code>criterion</code> seem to contain your optimizer.</p>

username: Ibrahim_Banat
cooked: <p><a class="mention" href="/u/alband">@albanD</a> thank you for your replay, i think i figured out where is the problem! but i don’t know how to solve it…</p>
<p>i have uploaded the files to make it easy to refer :<br>
<a href="https://github.com/ibrahimBanat/image-classifier-notcom" rel="nofollow noopener">link</a></p>
<p>error said that ‘Adam’ object is not callable and the error in is in line 128 in train_network  from suppFunctions.py file</p>
<p>in the train.py line 45 we create an optimizer inctance from nn_setup fucntion in suppFunctions.py<br>
i saw that in line 103 from the suppFunctions.py this line of code:</p>
<pre><code class="lang-auto">optimizer = optim.Adam(model.classifier.parameters(), lr )
</code></pre>
<p>and when i read the doc of pytorch i figured that i passed a wrong parameters<br>
could you help me writing the file in right way ?</p>

username: albanD
cooked: <p>The problem is that <a href="https://github.com/ibrahimBanat/image-classifier-notcom/blob/8fb2e2f6681493d43813afc2b61d88f97aa5b730/suppFunctions.py#L108">here</a> you <code>return model, criterion, optimizer</code><br>
But <a href="https://github.com/ibrahimBanat/image-classifier-notcom/blob/8fb2e2f6681493d43813afc2b61d88f97aa5b730/train.py#L45">here</a> you unpack <code>model, optimizer, criterion</code>.</p>
<p>Notice that the optimizer and criterion are swapped. You need to swap them back.</p>

username: Ibrahim_Banat
cooked: <p><a class="mention" href="/u/alband">@albanD</a> Thank you alot it worked even if i miss types some variable names, but now can i ask you how to run predict.py file ?<br>
i run train.py by typing <code>python train.py --epochs 1 </code> in my work-space command line</p>

username: albanD
cooked: <p>It looks like a standalone file no?<br>
I don’t know.</p>