Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ local*

# Johanthings
formatting.x
testrun.x
storage/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
9 changes: 7 additions & 2 deletions CollaborativeCoding/dataloaders/mnist_4_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ def __init__(
self.labels_path = self.mnist_path / (
MNIST_SOURCE["train_labels"][1] if train else MNIST_SOURCE["test_labels"][1]
)


# Functions to map the labels from (4,9) -> (0,5) for CrossEntropyLoss to work properly.
self.label_shift = lambda x: x-4
self.label_restore = lambda x: x+4


def __len__(self):
return len(self.samples)

Expand All @@ -66,4 +71,4 @@ def __getitem__(self, idx):
if self.transform:
image = self.transform(image)

return image, label
return image, self.label_shift(label)
11 changes: 5 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ def main():

for epoch in range(args.epoch):
# Training loop start
print(f"Epoch: {epoch+1}/{args.epoch}")
trainingloss = []
model.train()
for x, y in tqdm(trainloader, desc="Training"):
x, y = x.to(device), y.to(device)
logits = model.forward(x)

loss = criterion(logits, y)
loss.backward()

Expand Down Expand Up @@ -172,8 +172,8 @@ def main():
"Train loss": np.mean(trainingloss),
"Validation loss": np.mean(valloss),
}
| train_metrics.getmetric(str_prefix="Train ")
| val_metrics.getmetric(str_prefix="Validation ")
| train_metrics.getmetrics(str_prefix="Train ")
| val_metrics.getmetrics(str_prefix="Validation ")
)
train_metrics.resetmetric()
val_metrics.resetmetric()
Expand All @@ -187,12 +187,11 @@ def main():
loss = criterion(logits, y)
testloss.append(loss.item())

preds = th.argmax(logits, dim=1)
test_metrics(y, preds)
test_metrics(y, logits)

wandb.log(
{"Epoch": 1, "Test loss": np.mean(testloss)}
| test_metrics.getmetric(str_prefix="Test ")
| test_metrics.getmetrics(str_prefix="Test ")
)
test_metrics.resetmetric()

Expand Down
Loading