训练数据x_train是一个形状为(3000,32,32,3)的numpy ndarray。
class Netz(nn.Module):
def __init__(self):
super(Netz, self).__init__()
self.conv1 = nn.Conv2d(3,28,5)
self.conv2 = nn.Conv2d(28,100,5)
self.fc1 = nn.Linear(2500,120)
self.fc2 = nn.Linear(120,3)
def forward(self, x):
x = self.conv1(x)
x = F.max_pool2d(x,2)
x = F.relu(x)
x = self.conv2(x)
x = F.max_pool2d(x,2)
x = F.relu(x)
x = x.view(-1,2500)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x)
model = Netz()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.8)
def train(epoch):
model.train()
avg_loss = 0
correct = 0
criterion = F.nll_loss
for i in range(len(x_train)):
optimizer.zero_grad()
x = torch.tensor(x_train[i])
x = x.permute(2, 0, 1)
x = Variable(x)
x = x.unsqueeze(0)
target = Variable(torch.Tensor([y_train[i]]).type(torch.LongTensor))
out = model(x)
loss = criterion(out, target)
avg_loss += loss
pred = out.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
loss.backward()
optimizer.step()
if i%64==0:
print("epoch ", epoch, " [", i, "/", len(x_train), "] average loss: ", avg_loss.item() / 64, " correct: ", correct, "/64")
avg_loss = 0
correct = 0
我希望平均误差会随着时间的流逝而减少,但似乎会一直在同一数字附近波动...
答案 0 :(得分:0)
您的损失正在波动,这意味着您的网络功能不足以提取有意义的嵌入。我可以建议尝试以下几件事之一。