Practice
Solution
Step 1: Analyze the fitting process
Fitting a Multinomial Naive Bayes involves calculating probabilities for each feature across all samples.Step 2: Determine complexity
This requires iterating over each feature for each sample, leading to O(n * m) complexity.Final Answer:
Option D -> Option DQuick Check:
Multinomial NB fitting is O(n * m). [OK]
import numpy as np
def ndcg_at_k(r, k):
dcg_max = dcg_at_k(sorted(r, reverse=True), k)
if not dcg_max:
return 0.
return dcg_at_k(r, k) / dcg_max
relevance_scores = [3, 2, 3, 0, 1, 2]
k = 3
print(ndcg_at_k(relevance_scores, k))The output is unexpectedly low. What is the most likely cause?Solution
Step 1: Check Ideal DCG Calculation
The ideal DCG should be calculated using the sorted relevance scores.Step 2: Identify the Error
If the ideal DCG is not calculated correctly, the NDCG will be incorrect.Final Answer:
Option D -> Option DQuick Check:
Check ideal DCG calculation. [OK]
import torch
import torch.nn as nn
class LearnedPositionalEncoding(nn.Module):
def __init__(self, max_len, d_model):
super().__init__()
self.positional_encoding = nn.Parameter(torch.zeros(max_len, d_model))
def forward(self, x):
return x + self.positional_encoding[:x.size(1), :]
model = LearnedPositionalEncoding(100, 512)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)Solution
Step 1: Identify the initialization issue
The positional encoding is initialized to zeros, which may not provide a useful starting point for learning.Step 2: Eliminate wrong options
The learning rate is too low. is incorrect because the learning rate is standard. The optimizer is not suitable for this task. is incorrect because Adam is generally suitable. The positional encoding is not being updated. is incorrect because the parameter is part of the model and should be updated.Final Answer:
Option A -> Option AQuick Check:
Check initialization of learned parameters. [OK]
Solution
Step 1: Identify the challenge
Variable-length sequences require handling of different sequence lengths.Step 2: Choose the correct approach
Padding with masking is efficient and maintains sequence integrity.Final Answer:
Option A -> Option AQuick Check:
Pad and mask for variable lengths [OK]
Solution
Step 1: Identify computational overhead sources
Frequent updates can increase overhead, especially in large-scale setups.Step 2: Optimize scheduler updates
Creating a custom scheduler that reduces the frequency of updates can minimize computational costs while maintaining performance.Final Answer:
Option D -> Option DQuick Check:
Custom schedulers can reduce update frequency. [OK]
