- eq
It stands for "equal" - it performs element-wise equality comparison between tensors.
tensor1.eq(tensor2) # Returns True/False for each element
- Compares each element in tensor1 with corresponding element in tensor2
- Returns a boolean tensor with True where elements are equal, False otherwise
For example,
predicted = torch.tensor([1, 2, 3, 4, 5])
targets = torch.tensor([1, 0, 3, 0, 5])
result = predicted.eq(targets)
# → tensor([True, False, True, False, True])
correct = result.sum()
# → 3 (three correct predictions)
- view_as
It reshapes a tensor to have the same shape as another tensor.
tensor1.view_as(tensor2) # Reshape tensor1 to match tensor2's shape
- Automatically reshapes tensor1 to have the same dimensions as tensor2
- Equivalent to tensor1.view(tensor2.shape)
- Does NOT copy data, just changes how it's viewed
For example,
predicted = torch.tensor([1, 2, 3, 4]) # Shape: (4,)
targets = torch.tensor([[1], [2], [3], [4]]) # Shape: (4, 1)
# Reshape targets to match predicted
targets_reshaped = targets.view_as(predicted) # Shape: (4,)
# → tensor([1, 2, 3, 4])
# Now comparison works properly
result = predicted.eq(targets_reshaped)
'Basic Python' 카테고리의 다른 글
| float16 VS float32 VS float64, int8 VS int16 VS int32 VS int64 (0) | 2026.01.22 |
|---|---|
| StringIO (0) | 2024.05.03 |
| cv2.imread (0) | 2024.02.28 |
| argparse (0) | 2023.12.13 |
| Logger, Handler, Filter, Formatter (0) | 2023.12.08 |