Skip to content

Update pytorch_simple_fullynet.py #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions ML/Pytorch/Basics/pytorch_simple_fullynet.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,26 @@ def forward(self, x):

# Train Network
for epoch in range(num_epochs):
for batch_idx, (data, targets) in enumerate(tqdm(train_loader)):
for batch_idx, (data, targets) in enumerate(tqdm(train_loader)):
# enumerate就是枚举的意思,把元素一个个列举出来,第一个是什么,第二个是什么,所以他返回的是元素以及对应的索引。
# tqdm用在dataloader上其实是对每个batch和batch总数做的进度条,我也是在训练比较慢时写出来看看训练到哪了
# Get data to cuda if possible
data = data.to(device=device)
targets = targets.to(device=device)

# Get to correct shape
# Get to correct shape 64*1*28*28 --- 64*784 !
data = data.reshape(data.shape[0], -1)

# forward
scores = model(data)
loss = criterion(scores, targets)

# backward
# backward
optimizer.zero_grad()
loss.backward()
loss.backward() # 自动计算梯度

# gradient descent or adam step
optimizer.step()
# gradient descent or adam step 更新所有的参数
optimizer.step()


# Check accuracy on training & test to see how good our model
Expand All @@ -108,7 +110,7 @@ def check_accuracy(loader, model):
x = x.reshape(x.shape[0], -1)

scores = model(x)
_, predictions = scores.max(1)
_, predictions = scores.max(1) # _ 概率值 我们不感兴趣 predictions 索引 就是 类别
num_correct += (predictions == y).sum()
num_samples += predictions.size(0)

Expand Down