大家好,我初学深度学习,跟随官网教学用 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!")
=============================
有没有大佬讲一下正常的这几个测试集跑这几个网络准确度应该在多少
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.