The calculation conditions MXNet broadcast in NDArray

problem

"Hands-on learning deep learning" (By Aston Zhang, Mu Li, Zachary C.Lipton, Alexander J.Smola) described in broadcast operations and does not make clear the conditions of broadcast operations NDArray in. However, this operation is widely used in the subsequent code book, the binding of the search network, in particular the following Detailed.

Detailed

Case 1: the shape of two identical arithmetic elements according NDArray, no broadcast mechanism.

Case 2: When the shape is not the same, the two press elements NDArray operation also possible

Provided that: the shape of the two NDArray to be " compatible ." The so-called "compatible" means:

To determine whether two shapes are compatible, from the need to start the last one by comparing them forward dimension (Dimensions) size. The comparison process, if both the corresponding dimension of the same, or one of them (or all) is equal to 1, the comparison continues until the front-most dimension. Otherwise, you will see ValueError errors ( "operands could not be broadcast together with shapes ...").

When the dimension of the shape of one of them is out of range (e.g., Shape of a1 = (2,3,4) and (a2) shape = (3,4), when the range of a1 to a2 exceeds 2), will be used at this time Numpy 1 are compared (e.g., replaced a2.shape as (3,4)) until another dim also beyond the range.

Once Numpy determining the shape of both are compatible, the shape of the final result it would take a shape largest dimension between each dimension.

For example 1

A=nd.arange(3).reshape((3,1))
B=nd.arange(2).reshape((1,2))
A+B可以进行,结果的形状为(3,2)

Example 2

a=nd.arange(6).reshape((1,6))
b=nd.arange(8).reshape((8,1))
A+B可以进行,结果的形状为(8,6)

For example 3

a=nd.arange(6).reshape((1,6))
b=nd.arange(8).reshape((4,2,1))

A+B可以进行,结果的形状为(4,2,6)

Counterexample

a=nd.arange(6).reshape((3,2))
b=nd.arange(8).reshape((8,1))
A+B不可以进行,抛出错误!

Guess you like

Origin blog.51cto.com/zhuxianzhong/2412409