Skip to content

Please do not talk too much about the broadcast mechanism. #2639

@a358003542

Description

@a358003542

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions