Created
May 30, 2020 18:02
-
-
Save ResidentMario/af595ccbb412de0927dde265ed609d0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.train() | |
X = torch.tensor(X, dtype=torch.float32) | |
y = torch.tensor(y, dtype=torch.float32) | |
optimizer = torch.optim.Adam(self.parameters(), lr=self.max_lr) | |
scheduler = torch.optim.lr_scheduler.OneCycleLR( | |
optimizer, self.max_lr, | |
cycle_momentum=False, | |
epochs=self.n_epochs, | |
steps_per_epoch=int(np.ceil(len(X) / self.batch_size)), | |
) | |
batches = torch.utils.data.DataLoader( | |
torch.utils.data.TensorDataset(X, y), | |
batch_size=self.batch_size, shuffle=True | |
) | |
# NEW | |
scaler = torch.cuda.amp.GradScaler() | |
for epoch in range(self.n_epochs): | |
for i, (X_batch, y_batch) in enumerate(batches): | |
X_batch = X_batch.cuda() | |
y_batch = y_batch.cuda() | |
optimizer.zero_grad() | |
# NEW | |
with torch.cuda.amp.autocast(): | |
y_pred = model(X_batch).squeeze() | |
loss = self.loss_fn(y_pred, y_batch) | |
# NEW | |
scaler.scale(loss).backward() | |
lv = loss.detach().cpu().numpy() | |
if i % 100 == 0: | |
print(f"Epoch {epoch + 1}/{self.n_epochs}; Batch {i}; Loss {lv}") | |
# NEW | |
scaler.step(optimizer) | |
scaler.update() | |
scheduler.step() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment