Skip to content

Commit ef6739a

Browse files
committed
update
2 parents 3d15a1e + a5545bb commit ef6739a

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

03-Neural Network/neural_network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def forward(self, x):
9595
img = Variable(img, volatile=True).cuda()
9696
label = Variable(label, volatile=True).cuda()
9797
else:
98-
img = Variabel(img, volatile=True)
98+
img = Variable(img, volatile=True)
9999
label = Variable(label, volatile=True)
100100
out = model(img)
101101
loss = criterion(out, label)
-2.22 KB
Binary file not shown.

04-Convolutional Neural Network/convolution_network.py

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ def to_np(x):
2020

2121

2222
# 下载训练集 MNIST 手写数字训练集
23-
train_dataset = datasets.MNIST(root='./data', train=True,
24-
transform=transforms.ToTensor(),
25-
download=True)
23+
train_dataset = datasets.MNIST(
24+
root='./data', train=True, transform=transforms.ToTensor(), download=True)
2625

27-
test_dataset = datasets.MNIST(root='./data', train=False,
28-
transform=transforms.ToTensor())
26+
test_dataset = datasets.MNIST(
27+
root='./data', train=False, transform=transforms.ToTensor())
2928

3029
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
3130
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
@@ -41,14 +40,10 @@ def __init__(self, in_dim, n_class):
4140
nn.MaxPool2d(2, 2),
4241
nn.Conv2d(6, 16, 5, stride=1, padding=0),
4342
nn.ReLU(True),
44-
nn.MaxPool2d(2, 2),
45-
)
43+
nn.MaxPool2d(2, 2), )
4644

4745
self.fc = nn.Sequential(
48-
nn.Linear(400, 120),
49-
nn.Linear(120, 84),
50-
nn.Linear(84, n_class)
51-
)
46+
nn.Linear(400, 120), nn.Linear(120, 84), nn.Linear(84, n_class))
5247

5348
def forward(self, x):
5449
out = self.conv(x)
@@ -67,18 +62,17 @@ def forward(self, x):
6762
logger = Logger('./logs')
6863
# 开始训练
6964
for epoch in range(num_epoches):
70-
print('epoch {}'.format(epoch+1))
71-
print('*'*10)
65+
print('epoch {}'.format(epoch + 1))
66+
print('*' * 10)
7267
running_loss = 0.0
7368
running_acc = 0.0
7469
for i, data in enumerate(train_loader, 1):
7570
img, label = data
7671
if use_gpu:
77-
img = Variable(img).cuda()
78-
label = Variable(label).cuda()
79-
else:
80-
img = Variable(img)
81-
label = Variable(label)
72+
img = img.cuda()
73+
label = label.cuda()
74+
img = Variable(img)
75+
label = Variable(label)
8276
# 向前传播
8377
out = model(img)
8478
loss = criterion(out, label)
@@ -94,10 +88,7 @@ def forward(self, x):
9488
# ========================= Log ======================
9589
step = epoch * len(train_loader) + i
9690
# (1) Log the scalar values
97-
info = {
98-
'loss': loss.data[0],
99-
'accuracy': accuracy.data[0]
100-
}
91+
info = {'loss': loss.data[0], 'accuracy': accuracy.data[0]}
10192

10293
for tag, value in info.items():
10394
logger.scalar_summary(tag, value, step)
@@ -106,26 +97,20 @@ def forward(self, x):
10697
for tag, value in model.named_parameters():
10798
tag = tag.replace('.', '/')
10899
logger.histo_summary(tag, to_np(value), step)
109-
logger.histo_summary(tag+'/grad', to_np(value.grad), step)
100+
logger.histo_summary(tag + '/grad', to_np(value.grad), step)
110101

111102
# (3) Log the images
112-
info = {
113-
'images': to_np(img.view(-1, 28, 28)[:10])
114-
}
103+
info = {'images': to_np(img.view(-1, 28, 28)[:10])}
115104

116105
for tag, images in info.items():
117106
logger.image_summary(tag, images, step)
118107
if i % 300 == 0:
119108
print('[{}/{}] Loss: {:.6f}, Acc: {:.6f}'.format(
120-
epoch+1, num_epoches,
121-
running_loss/(batch_size*i),
122-
running_acc/(batch_size*i)
123-
))
109+
epoch + 1, num_epoches, running_loss / (batch_size * i),
110+
running_acc / (batch_size * i)))
124111
print('Finish {} epoch, Loss: {:.6f}, Acc: {:.6f}'.format(
125-
epoch+1,
126-
running_loss/(len(train_dataset)),
127-
running_acc/(len(train_dataset))
128-
))
112+
epoch + 1, running_loss / (len(train_dataset)), running_acc / (len(
113+
train_dataset))))
129114
model.eval()
130115
eval_loss = 0
131116
eval_acc = 0
@@ -135,18 +120,16 @@ def forward(self, x):
135120
img = Variable(img, volatile=True).cuda()
136121
label = Variable(label, volatile=True).cuda()
137122
else:
138-
img = Variabel(img, volatile=True)
123+
img = Variable(img, volatile=True)
139124
label = Variable(label, volatile=True)
140125
out = model(img)
141126
loss = criterion(out, label)
142-
eval_loss += loss.data[0]*label.size(0)
127+
eval_loss += loss.data[0] * label.size(0)
143128
_, pred = torch.max(out, 1)
144129
num_correct = (pred == label).sum()
145130
eval_acc += num_correct.data[0]
146-
print('Test Loss: {:.6f}, Acc: {:.6f}'.format(
147-
eval_loss/(len(test_dataset)),
148-
eval_acc/(len(test_dataset))
149-
))
131+
print('Test Loss: {:.6f}, Acc: {:.6f}'.format(eval_loss / (len(
132+
test_dataset)), eval_acc / (len(test_dataset))))
150133
print()
151134

152135
# 保存模型

0 commit comments

Comments
 (0)