大家好,我初学深度学习,跟随官网教学用 torch 框架逐步实现一些经典框架看看效果。
我分别测试了两个数据集,分别是 mnist 和 fasionMnist ,使用交叉熵计算损失,sdg 做优化,然后我测试了三种模型,一种是三层全连接层铺上,第二是 lenet ,第三是 alexnet ,感觉操作这些经典结构有助于我熟悉工具。
但是我测试过程中感觉训练准确率不太对,无论哪个模型的最终准确率都在 80%左右收敛,这比我想象的低太多了。我印象中 lenet 虽然是个很老的结构,但是当时应该也能做到 95%以上吧?
且我的几种结构里,无论使用 tanh 还是 relu 做激发函数,结果并没有什么区别。这让我怀疑我是不是哪里写错了。有无大佬帮忙看一下,谢谢。
以下代码是官网的 quick start ,我直接改了一下神经网络的内部结构做了个 lenet5 ,然后把激发函数换成 relu ,最终 fasionmnist 的准确度收敛 84%,是不是有点太低了?
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from torchvision.transforms import ToTensor, Lambda, Resize
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
training_data = datasets.FasionMNIST(
root=".\\fasionmnist",
train=True,
download=True,
transform=transforms.Compose([
Resize((32, 32)),
ToTensor()
])
)
test_data = datasets.FasionMNIST(
root=".\\fasionmnist",
train=False,
download=True,
transform=transforms.Compose([
Resize((32, 32)),
ToTensor()
])
)
train_dataloader = DataLoader(training_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Conv2d(1, 6, kernel_size = 5),
nn.ReLU(),
nn.AvgPool2d(kernel_size = 2),
nn.Conv2d(6, 16, kernel_size = 5),
nn.ReLU(),
nn.AvgPool2d(kernel_size = 2),
nn.Conv2d(16, 120, kernel_size = 5),
nn.ReLU(),
nn.Flatten(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 10)
)
def forward(self, x):
return self.linear_relu_stack(x)
model = NeuralNetwork().to(device)
learning_rate = 1e-3
batch_size = 64
epochs = 5
# Initialize the loss function
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
def train_loop(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
# Compute prediction and loss
pred = model(X)
loss = loss_fn(pred, y)
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
def test_loop(dataloader, model, loss_fn):
size = len(dataloader.dataset)
num_batches = len(dataloader)
test_loss, correct = 0, 0
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fn(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
epochs = 100
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train_loop(train_dataloader, model, loss_fn, optimizer)
test_loop(test_dataloader, model, loss_fn)
print("Done!")
=============================
有没有大佬讲一下正常的这几个测试集跑这几个网络准确度应该在多少
1
3dwelcome 2021-11-14 13:55:59 +08:00
你记错了,时装样本准确率本来就不高,只有手写的才高。
我自己的测试数据: 训练用时 18 分钟,时装样本 8222/10000 82.22%, 手写识别 9650/10000 96.50% |
2
Richard14 OP @3dwelcome 大佬我迁移到新帖了,感觉这个贴没有说到重点。https://v2ex.com/t/815301
|