Skip to content

Commit

Permalink
commit 1: numerically unstable may exist when computing softmax for M…
Browse files Browse the repository at this point in the history
…NIST and CIFAR10 model; the solution is referred to section 4.1 of Deep Learning Book by Goodfellow et al.
  • Loading branch information
ryderling committed May 21, 2019
1 parent 20281e9 commit d4e1181
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 2 additions & 0 deletions RawModels/MNISTConv.py
Expand Up @@ -10,6 +10,7 @@
import os
import sys

import torch
import torch.nn as nn
import torch.nn.functional as F

Expand Down Expand Up @@ -64,4 +65,5 @@ def forward(self, x):
out = self.dropout(out)
out = F.relu(self.fc2(out))
out = self.fc3(out)
out = out - torch.max(out, dim=1, keepdim=True)[0]
return out
3 changes: 2 additions & 1 deletion RawModels/ResNet.py
Expand Up @@ -8,7 +8,7 @@
import math
import os
import sys

import torch
import torch.nn as nn

sys.path.append('%s/../' % os.path.dirname(os.path.realpath(__file__)))
Expand Down Expand Up @@ -136,6 +136,7 @@ def forward(self, x):
x = x.view(x.size(0), -1)
x = self.fc(x)

x = x - torch.max(x, dim=1, keepdim=True)[0]
return x


Expand Down

0 comments on commit d4e1181

Please sign in to comment.