Skip to content
Merged
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
6 changes: 2 additions & 4 deletions questions/160_mixed_precision_training/solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


class MixedPrecision:
def __init__(self, loss_scale=1024.0):
self.loss_scale = loss_scale
Expand All @@ -15,9 +14,8 @@ def forward(self, weights, inputs, targets):
predictions = np.dot(inputs_fp16, weights_fp16)
loss = np.mean((targets_fp16 - predictions) ** 2)

# Scale loss and convert back to float32
scaled_loss = loss.astype(np.float32) * self.loss_scale

# Scale loss and convert back to float32 (Python float)
scaled_loss = float(loss) * self.loss_scale
return scaled_loss

def backward(self, gradients):
Expand Down