-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Open
Description
About Chapter 2.1.4 and 2.3.7, there is too much talk about the broadcast mechanism. It's like the implicit type conversion rule, which is not a good programming style.
About 2.3.7, a good programming style is to call the functions supplied by torch.nn.functional
:
import torch
import torch.nn.functional as F
# 创建示例张量
data = torch.arange(20, dtype=torch.float32).reshape(5, 4)
# 使用 torch.nn.functional.normalize 进行 L2 归一化
normalized_data_l2 = F.normalize(data, p=2, dim=1)
# 使用 torch.nn.functional.normalize 进行 L1 归一化
normalized_data_l1 = F.normalize(data, p=1, dim=1)
print("Original data:")
print(data)
print("Normalized data (L2 norm):")
print(normalized_data_l2)
print("Normalized data (L1 norm):")
print(normalized_data_l1)
If there is really a need to use the broadcast mechanism, recommend first checkout the broadcast output:
np.broadcast_arrays(x, y)
torch.broadcast_tensors(x, y)
This helps to check what the internal broadcast mechanism will output. The broadcast mechanism is an internal mechanism in NumPy designed for speeding up computations. However, encouraging the use of this style of code is not a good idea.
Metadata
Metadata
Assignees
Labels
No labels