The broadcasting mechanism of python/numpy/pytorch
1. Python broadcast mechanism
When using numpy for bitwise operations in python, there is a little trick that can help reduce the amount of code - that is broadcasting, the broadcasting mechanism.
example:
A = numpy.array([1, 2, 3])
result = A + 100
print(result)
Results after running
[101 102 103]
The broadcasting mechanism is widely used in the construction of deep learning functions. For example, in the logistic regression function, the activation function z = wT * X + b, b is a number and the previous wT * X is a 1*n matrix. Due to the mechanism of python, we don't need to construct a vector for b, which reduces the amount of code.
Two, pytorch broadcast mechanism
Tensor (tensor) broadcasting principle: add a constant, then each element is added.
a = torch.rand(2, 3)
print(a)
b = a + 1
print(b)
operation result