Practice
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.layer_norm = nn.LayerNorm(10)
def forward(self, x):
return self.layer_norm(x)
model = MyModel()
input_tensor = torch.randn(5, 10)
output = model(input_tensor)
print(output.shape)What is the output shape of the above code?Solution
Step 1: Analyze the LayerNorm application.
LayerNorm normalizes across the last dimension, maintaining the input shape.Step 2: Determine the output shape.
The input tensor has shape (5, 10), and LayerNorm does not alter the shape, so the output is also (5, 10).Final Answer:
Option C -> Option CQuick Check:
LayerNorm keeps input shape. [OK]
import torch
from torchvision.models.detection import yolo_v3
model = yolo_v3(pretrained=True)
model.eval()
input_tensor = torch.rand(1, 3, 416, 416)
output = model(input_tensor)
print(output[0]['boxes'].shape)Solution
Step 1: Understand YOLO output
YOLO outputs a list of detections, each with a bounding box of 4 coordinates.Step 2: Determine output shape
The shape is (N, 4) where N is the number of detected objects.Final Answer:
Option B -> Option BQuick Check:
YOLO boxes = (N, 4) [OK]
import numpy as np
np.random.seed(0)
weights = [0.1, 0.3, 0.6]
choices = ['A', 'B', 'C']
result = np.random.choice(choices, p=weights)
print(result)Solution
Step 1: Understand weighted sampling.
np.random.choice with 'p' parameter uses specified probabilities for sampling.Step 2: Analyze the code.
Weights [0.1, 0.3, 0.6] mean 'C' is most likely, but not guaranteed.Final Answer:
Option A -> Option AQuick Check:
np.random.choice uses 'p' for weighted sampling [[check]]
Solution
Step 1: Consider dataset size and model complexity.
Small datasets are prone to overfitting with complex models.Step 2: Evaluate the impact of each option.
Polynomial features (A, C) increase complexity significantly, leading to overfitting. Interaction terms (B, D) add complexity but less than polynomial features, making them more suitable for small datasets.Final Answer:
Option D -> Option DQuick Check:
Small data: prefer interaction terms to avoid overfitting. [OK]
Solution
Step 1: Understand RAG efficiency
For real-time applications, updating documents based on interactions ensures relevance and accuracy.Step 2: Eliminate wrong options
Use a static set of documents for retrieval to ensure consistency. lacks adaptability. Rely solely on the model's pre-trained knowledge without retrieval. ignores the retrieval component. Use a large, diverse document set without updates to avoid bias. risks outdated information.Final Answer:
Option C -> Option CQuick Check:
Update documents for real-time RAG efficiency. [OK]
