Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the broadcasting mechanism of python?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how the broadcasting mechanism of python is, which is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Why is there a broadcasting mechanism?

When designing the python language, it takes into account the mismatch of the matrix or vector dimensions used in the two operations. For example, we have matrix A, which adds 1 to each element of the matrix, using the

The goal can be achieved by using Aspiri 1. This uses the broadcast mechanism of python, so in many third-party libraries of python, broadcast mechanisms are supported, such as Numpy and pytorch.

Broadcast mechanism in matrix or vector correlation operations 1. General operations

Suppose we have a matrix A, a matrix B, and their +-* operation is the operation of one-to-one corresponding elements.

2. The case of one matrix and one vector

Matrix A ∈ R3 × 3 A\ in R ^ {3\ times 3} A ∈ R3 × 3 and vector b ∈ R1 × 3 b\ in R ^ {1\ times 3} b ∈ R1 × 3, vector b ∈ R1 × 3 b\ in R ^ {1\ times 3} b ∈ R1 × 3 expands its own dimension according to the dimension of matrix A, [1 × 3] → [3 × 3] [1\ times 3]\ to [3\ times 3] [1 × 3] → [3 × 3] The first dimension has a size of 1, so it expands in the direction of the column, that is, copying multiple rows. As shown in the figure.

Similarly, if the vector b ∈ R3 × 1 b\ in R ^ {3\ times 1} b ∈ R3 × 1, it expands to the row, that is, multiple columns are copied.

3. Two vectors

If the size of the two vectors is the same, then there is nothing to say. After the operation, it is still a vector, and there is a broadcast only when the size of the two vectors is different.

Vector a ∈ R3 × 1 a\ in R ^ {3\ times 1} a ∈ R3 × 1 vector b ∈ R1 × 3 b\ in R ^ {1\ times 3} b ∈ R1 × 3 or vector b ∈ R3 b\ in R ^ {3} b ∈ R3, the size of the two is different. Vector an expands in the direction of dimension size 1, and so does b.

It is worth noting that when we use numpy

# construct a vector import numpy as npa = np.arange (10) # (10menone) a has only one dimension. Whether you rotate or transpose, it will not change. To achieve broadcasting, add a dimension to a: import numpy as npa = np.arange (10) a = np.expand_dims (ap1) # (10p1)

4. Broadcast mechanism of matrix multiplication.

The above is what the broadcasting mechanism of python is like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report