Practice
import numpy as np
# Initialize weights
weights = np.zeros((m, 1))
# Training loop
for epoch in range(epochs):
# Compute predictions
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
# Compute gradient
gradient = np.dot(X.T, (predictions - y)) / n
# Update weights
weights -= learning_rate * gradientSolution
Step 1: Identify potential issues
Convergence issues often arise from incorrect weight updates or learning rate problems.Step 2: Analyze weight update line
If learning_rate is too high, weights update too aggressively, causing divergence.Final Answer:
Option A -> Option AQuick Check:
Check learning rate in weight updates [OK]
def calculate_f1(confusion_matrix):
tp = confusion_matrix[0, 0]
fp = confusion_matrix[1, 0]
fn = confusion_matrix[0, 1]
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score = 2 * (precision * recall) / (precision + recall)
return f1_score
confusion_matrix = np.array([[50, 10], [5, 35]])
print(calculate_f1(confusion_matrix))Solution
Step 1: Calculate precision and recall
Precision = 0.9091, Recall = 0.8333.Step 2: Calculate F1-score
F1 = 2 * (0.9091 * 0.8333) / (0.9091 + 0.8333) = 0.8696.Final Answer:
Option B -> Option BQuick Check:
F1 combines precision and recall. [OK]
Solution
Step 1: Identify optimization techniques
Fusing operations can reduce computational overhead.Step 2: Evaluate each option
Fusing batch normalization with linear layers reduces redundant computations.Final Answer:
Option A -> Option AQuick Check:
Fuse layers to optimize batch norm [OK]
import numpy as np
# Initialize weights
weights = np.zeros((m, 1))
# Training loop
for epoch in range(epochs):
# Compute predictions
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
# Compute gradient
gradient = np.dot(X.T, (predictions - y)) / n
# Update weights
weights -= learning_rate * gradientSolution
Step 1: Identify bottlenecks
Matrix operations can be slow if X is dense and large.Step 2: Optimize matrix operations
Using a sparse matrix representation for X reduces memory usage and speeds up computations.Final Answer:
Option A -> Option AQuick Check:
Sparse matrices optimize large, sparse datasets [OK]
Solution
Step 1: Identify optimization needs
For large datasets, memory efficiency is crucial.Step 2: Evaluate optimization strategies
Using a sparse matrix (Use a sparse matrix representation for input data.) reduces memory usage and speeds up computation. Use a more complex model like SVM. increases complexity unnecessarily. Implement batch processing for training. is less relevant as GaussianNB is already efficient. Increase the number of features by adding polynomial terms. increases computational load.Final Answer:
Option A -> Option AQuick Check:
Use sparse matrices for memory efficiency. [OK]
