Skip to content
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

Fixed double softmax bug in SSLEvaluator #663

Merged
merged 5 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 6 additions & 6 deletions pl_bolts/callbacks/ssl_online.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ def on_train_batch_end(
representations = representations.detach()

# forward pass
mlp_preds = pl_module.non_linear_evaluator(representations) # type: ignore[operator]
mlp_loss = F.cross_entropy(mlp_preds, y)
mlp_logits = pl_module.non_linear_evaluator(representations) # type: ignore[operator]
mlp_loss = F.cross_entropy(mlp_logits, y)

# update finetune weights
mlp_loss.backward()
self.optimizer.step()
self.optimizer.zero_grad()

# log metrics
train_acc = accuracy(mlp_preds, y)
train_acc = accuracy(mlp_logits.softmax(-1), y)
pl_module.log('online_train_acc', train_acc, on_step=True, on_epoch=False)
pl_module.log('online_train_loss', mlp_loss, on_step=True, on_epoch=False)

Expand All @@ -132,10 +132,10 @@ def on_validation_batch_end(
representations = representations.detach()

# forward pass
mlp_preds = pl_module.non_linear_evaluator(representations) # type: ignore[operator]
mlp_loss = F.cross_entropy(mlp_preds, y)
mlp_logits = pl_module.non_linear_evaluator(representations) # type: ignore[operator]
mlp_loss = F.cross_entropy(mlp_logits, y)

# log metrics
val_acc = accuracy(mlp_preds, y)
val_acc = accuracy(mlp_logits.softmax(-1), y)
pl_module.log('online_val_acc', val_acc, on_step=False, on_epoch=True, sync_dist=True)
pl_module.log('online_val_loss', mlp_loss, on_step=False, on_epoch=True, sync_dist=True)
17 changes: 3 additions & 14 deletions pl_bolts/models/self_supervised/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,12 @@ def __init__(self, n_input, n_classes, n_hidden=512, p=0.1):
self.n_hidden = n_hidden
if n_hidden is None:
# use linear classifier
self.block_forward = nn.Sequential(
Flatten(),
nn.Dropout(p=p),
nn.Linear(n_input, n_classes, bias=True),
nn.Softmax(),
)
self.block_forward = nn.Sequential(Flatten(), nn.Dropout(p=p), nn.Linear(n_input, n_classes, bias=True))
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved
else:
# use simple MLP classifier
self.block_forward = nn.Sequential(
Flatten(),
nn.Dropout(p=p),
nn.Linear(n_input, n_hidden, bias=False),
nn.BatchNorm1d(n_hidden),
nn.ReLU(inplace=True),
nn.Dropout(p=p),
nn.Linear(n_hidden, n_classes, bias=True),
nn.Softmax(),
Flatten(), nn.Dropout(p=p), nn.Linear(n_input, n_hidden, bias=False), nn.BatchNorm1d(n_hidden),
nn.ReLU(inplace=True), nn.Dropout(p=p), nn.Linear(n_hidden, n_classes, bias=True)
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved
)

def forward(self, x):
Expand Down
6 changes: 3 additions & 3 deletions pl_bolts/models/self_supervised/ssl_finetuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def on_train_epoch_start(self) -> None:

def training_step(self, batch, batch_idx):
loss, logits, y = self.shared_step(batch)
acc = self.train_acc(logits, y)
acc = self.train_acc(logits.softmax(-1), y)

self.log('train_loss', loss, prog_bar=True)
self.log('train_acc_step', acc, prog_bar=True)
Expand All @@ -98,7 +98,7 @@ def training_step(self, batch, batch_idx):

def validation_step(self, batch, batch_idx):
loss, logits, y = self.shared_step(batch)
self.val_acc(logits, y)
self.val_acc(logits.softmax(-1), y)

self.log('val_loss', loss, prog_bar=True, sync_dist=True)
self.log('val_acc', self.val_acc)
Expand All @@ -107,7 +107,7 @@ def validation_step(self, batch, batch_idx):

def test_step(self, batch, batch_idx):
loss, logits, y = self.shared_step(batch)
self.test_acc(logits, y)
self.test_acc(logits.softmax(-1), y)

self.log('test_loss', loss, sync_dist=True)
self.log('test_acc', self.test_acc)
Expand Down