# Loss and accuracy

**URL:** <https://discuss.pytorch.org/t/loss-and-accuracy/70469>\
**Category:** audio\
**Created:** [February 20, 2020, 12:50pm UTC](https://discuss.pytorch.org/t/loss-and-accuracy/70469 "2020-02-20T12:50:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohamed\_Nabih](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mohamed_nabih/32/19887_2.png) [@Mohamed\_Nabih](https://discuss.pytorch.org/u/Mohamed_Nabih)\
**Post date:** [February 20, 2020, 12:50pm UTC](https://discuss.pytorch.org/t/loss-and-accuracy/70469/1 "2020-02-20T12:50:56Z")

</div>

input\_size = 13  
hidden1\_size = 13  
hidden2\_size = 128  
hidden3\_size = 64  
output\_size = 30

class DNN(nn.Module):  
def **init** (self, input\_size, hidden1\_size, hidden2\_size, hidden3\_size, output\_size):  
super(DNN, self). **init** ()  
self.fc1 = nn.Linear(input\_size, hidden1\_size)  
self.relu1 = nn.ReLU()  
self.fc2 = nn.Linear(hidden1\_size, hidden2\_size)  
self.relu2 = nn.ReLU()  
self.fc3 = nn.Linear(hidden2\_size, hidden3\_size)  
self.relu3 = nn.ReLU()  
self.fc4 = nn.Linear(hidden3\_size, output\_size)  
self.relu4 = nn.ReLU()

```
def forward(self, x):
    out = self.fc1(x)
    out = self.relu1(out)
    out = self.fc2(out)
    out = self.relu2(out)
    out = self.fc3(out)
    out = self.relu3(out)
    out = self.fc4(out)
    out = self.relu4(out)
    return out

```

batch\_size = 10  
n\_iterations = 50  
no\_eps = n\_iterations / (13 / batch\_size)  
no\_epochs = int(no\_eps)  
model = DNN(input\_size, hidden1\_size, hidden2\_size, hidden3\_size, output\_size)

criterion = nn.CrossEntropyLoss()  
learning\_rate = 0.01  
optimizer = torch.optim.SGD(model.parameters(), lr=learning\_rate)

iter = 0  
for epoch in range(no\_epochs):  
for i, (X\_train, y\_train) in enumerate(train\_loader):  
y\_train = torch.empty(batch\_size, dtype=torch.long).random\_(output\_size)  
optimizer.zero\_grad()  
outputs = model(X\_train)  
loss = criterion(outputs, y\_train)  
loss.backward()  
optimizer.step()  
iter += 1  
if iter % 500 == 0:  
correct = 0  
total = 0  
for X\_test, y\_test in test\_loader:  
outputs = model(X\_test)  
pred = outputs.argmax(dim=1, keepdim=True)  
total += y\_test.size(0)  
correct += pred.eq(y\_test.view\_as(pred)).sum().item()  
accuracy = 100 \* correct / total  
print(‘Iteration: {}. Loss: {}. Accuracy: {}’.format(iter, loss.item(), accuracy))

* * *

The output start with Iteration 500 not 1 why !!!  
Iteration: 500. Loss: 3.403779983520508. Accuracy: 0.2761323337093925  
Iteration: 1000. Loss: 3.4060192108154297. Accuracy: 0.3276070990724763  
Iteration: 1500. Loss: 3.416713237762451. Accuracy: 0.4173101012337052  
Iteration: 2000. Loss: 3.402294635772705. Accuracy: 0.34867708074959347  
Iteration: 2500. Loss: 3.3952858448028564. Accuracy: 0.2755100135754692  
Iteration: 3000. Loss: 3.4023067951202393. Accuracy: 0.3158719194042085

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [February 21, 2020, 6:17am UTC](https://discuss.pytorch.org/t/loss-and-accuracy/70469/2 "2020-02-21T06:17:41Z")

</div>

You are increasing `iter` directly before the if condition to 1, which result in `iter % 500 == 0` when `iter` reaches multiples of 500.  
If you want to print the very first iteration, increase `iter` after the condition.
