神经网络的准确率没有改变

问题描述 投票:0回答:1

全连接神经网络的代码(我知道什么是更好的卷积,我会进一步做),它确定 MNIST 数据集中的数字。启用后,精度根本不会改变。可能是什么错误?我用线条划分了代码单元格,并用注释标记了内容在哪里。对于大量代码和冗长的答案,我提前表示歉意。

import os
from torchvision.datasets import MNIST
from torchvision import transforms as tfs


data_tfs = tfs.Compose([
    tfs.ToTensor(),
    tfs.Normalize((0.5), (0.5))
])

# install for train and test
root = './'
train_dataset = MNIST(root, train=True,  transform=data_tfs, download=True)
val_dataset  = MNIST(root, train=False, transform=data_tfs, download=True)

train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True) 

valid_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=128, shuffle=False)
---------------------------------------------------------------
#Creating the model
activation = nn.ELU

model = nn.Sequential(
    nn.Flatten(),
    #YOUR CODE. Add layers to your sequential class
    nn.Linear(in_features=2, out_features=128),
    nn.ELU(),
    nn.Linear(in_features=128, out_features=10),
    nn.ELU()
)
---------------------------------------------------------------
criterion = nn.CrossEntropyLoss()#YOUR CODE. Select a loss function
optimizer = torch.optim.Adam(model.parameters())

loaders = {"train": train_dataloader, "valid": valid_dataloader}
---------------------------------------------------------------
device = "cuda" if torch.cuda.is_available() else "cpu"
---------------------------------------------------------------
max_epochs = 10
accuracy = {"train": [], "valid": []}

class Identical(nn.Module):
    def forward(self, x):
        return x

activation = nn.ELU

model = nn.Sequential(
    nn.Flatten(),
    #YOUR CODE. Add layers to your sequential class
    nn.Linear(in_features=2, out_features=128),
    nn.ELU(),
    nn.Linear(in_features=128, out_features=10),
    nn.ELU()
)

criterion = nn.CrossEntropyLoss()#YOUR CODE. Select a loss function
optimizer = torch.optim.Adam(model.parameters())

loaders = {"train": train_dataloader, "valid": valid_dataloader}

device = "cuda" if torch.cuda.is_available() else "cpu"

for epoch in range(max_epochs):
    for k, dataloader in loaders.items():
        epoch_correct = 0
        epoch_all = 0
        for x_batch, y_batch in dataloader:
            if k == "train":
              model.train()
              optimizer.zero_grad()
              outp = model(x_batch)
              outp = outp[:y_batch.shape[0]] #otherwise, the last batch is less than necessary
            else:
              model.eval()
              with torch.no_grad():
                outp = model(x_batch)
                outp = outp[:y_batch.shape[0]]
            preds = outp.argmax(-1)
            correct =  (preds == y_batch).sum()
            all =  tuple(preds.shape)[0]
            epoch_correct += correct.item()
            epoch_all += all
            if k == "train":
                loss = criterion(outp, y_batch)
                loss.backward()
                optimizer.step()
        if k == "train":
            print(f"Epoch: {epoch+1}")
        print(f"Loader: {k}. Accuracy: {epoch_correct/epoch_all}")
        accuracy[k].append(epoch_correct/epoch_all)
python deep-learning pytorch neural-network
1个回答
1
投票

我已将您的模型更改为下面的代码,因为您正在展平图像,并且在下一层中输入的尺寸应为 28*28=784:

model = nn.Sequential(
nn.Flatten(),
#YOUR CODE. Add layers to your sequential class
nn.Linear(in_features=784, out_features=128),
nn.ELU(),
nn.Linear(in_features=128, out_features=10),
nn.ELU()
)

在此更改之后,模型完美运行,结果如下:

  • 火车。准确度:0.98535
  • 有效。准确度:0.9759
© www.soinside.com 2019 - 2024. All rights reserved.