Replace the two calls to np.expand_dims with one call with axis=<tuple>, i.e.
matrix_weights = np.expand_dims(
[[0, 2, 0], [1, 0, 3]], axis=(2, 3)
)
values_4d * matrix_weights
instead of the currently present
matrix_weights = np.expand_dims(np.expand_dims(
[[0, 2, 0], [1, 0, 3]], axis=2), axis=3
)
values_4d * matrix_weights
in Broadcasting.
Replace the two calls to
np.expand_dimswith one call withaxis=<tuple>, i.e.instead of the currently present
in Broadcasting.