初步接触深度学习,使用 pytorch 框架,关键代码如下:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 50, 5, stride=1, padding=1, bias=False)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(50, 100, 7, stride=1, padding=1, bias=False)
self.pool2 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(1600 , 400)
self.fc2 = nn.Linear(400, 100)
self.fc3 = nn.Linear(100, 2)
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(5): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(data_loader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs = data['image']
labels = data['label']
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
inputs(输入值):
tensor([[[[-0.3725, -0.3412, -0.3098, ..., 0.5451, 0.5686, 0.6314],
[-0.3020, -0.3098, -0.3333, ..., 0.4039, 0.5451, 0.5294],
[-0.2078, -0.2784, -0.3255, ..., -0.0118, 0.2471, 0.5216],
...,
[-0.4431, -0.5294, -0.4902, ..., -1.0000, -0.1216, 0.4588],
[-0.4196, -0.4431, -0.5451, ..., -0.1843, 0.5451, 0.5294],
[-0.2549, -0.4039, -0.5686, ..., 0.4824, 0.5294, 0.5137]]],
[[[ 0.4588, 0.3882, 0.3490, ..., 0.0745, 0.3725, 0.5451],
[ 0.5451, 0.4902, 0.4745, ..., 0.0902, 0.4588, 0.5922],
[ 0.5608, 0.5373, 0.5373, ..., 0.2784, 0.5216, 0.6314],
...,
label(标签):
tensor([3, 4, 1, 5, 6, 3, 4, 0, 4, 3, 6, 3, 3, 3, 3, 3, 6, 3, 0, 5, 3, 3, 6, 0,
5, 3, 0, 0, 2, 4, 3, 3, 4, 5, 4, 2, 2, 6, 3, 3, 3, 3, 3, 0, 3, 5, 3, 3,
6, 5])
output(网络输出值?)
tensor([[ 0.0508, -0.0647],
[ 0.0380, -0.0434],
[ 0.0412, -0.0596],
[ 0.0499, -0.0559],
[ 0.0579, -0.0619],
[ 0.0455, -0.0614],
[ 0.0494, -0.0628],
[ 0.0494, -0.0491],
[ 0.0439, -0.0690],
[ 0.0512, -0.0562],
[ 0.0491, -0.0516],
[ 0.0493, -0.0699],
[ 0.0468, -0.0654],
[ 0.0606, -0.0682],
[ 0.0603, -0.0597],
[ 0.0522, -0.0604],
[ 0.0422, -0.0535],
[ 0.0616, -0.0678],
[ 0.0366, -0.0472],
[ 0.0388, -0.0439],
[ 0.0575, -0.0728],
[ 0.0408, -0.0586],
[ 0.0426, -0.0641],
[ 0.0467, -0.0601],
[ 0.0389, -0.0470],
[ 0.0481, -0.0654],
[ 0.0575, -0.0677],
[ 0.0484, -0.0633],
[ 0.0398, -0.0525],
[ 0.0490, -0.0641],
[ 0.0438, -0.0625],
[ 0.0429, -0.0583],
[ 0.0426, -0.0521],
[ 0.0692, -0.0447],
[ 0.0541, -0.0779],
[ 0.0330, -0.0317],
[ 0.0473, -0.0488],
[ 0.0484, -0.0528],
[ 0.0438, -0.0542],
[ 0.0414, -0.0508],
[ 0.0443, -0.0413],
[ 0.0483, -0.0577],
[ 0.0426, -0.0607],
[ 0.0430, -0.0570],
[ 0.0472, -0.0560],
[ 0.0452, -0.0608],
[ 0.0381, -0.0407],
[ 0.0396, -0.0379],
[ 0.0444, -0.0607],
[ 0.0564, -0.0578]], grad_fn=<AddmmBackward>)
CrossEntropyLoss
就会报错:indexError: Target 2 is out of bounds.
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.