Python/numpy/tensorflow, the matrix row and column operations, subscript is how to go?

Source: Internet
Author: User

The Ndarrray in List/tuple,numpy in Python and the tensor in TensorFlow.

In Python, List/tuple understands that a sequence of data is understood only from the memory point of view, not the number of mathematical bids, vectors, and tensor.

From the Python memory point of view, is a numeric value, length 1, and is not a sequence;

From the perspective of NumPy and TensorFlow mathematics, it is a scalar, shape is (), its axis is 0;

[1,2,3,4,5,6]

From the Python memory perspective, it is a sequence of 1*6 or a length of 6;

From NumPy and TensorFlow mathematical perspective, is a one-dimensional vector, and is a column vector, Shape is (6,), its axis is 1; note: one-dimensional vector, column vector, its axis is 1, the axis subscript 0, representing the first axis;

[[A] [[4,5,6]]

From the Python memory point of view, is a two-dimensional array 2*3,2 column 3 rows; Note: Whether Python or Numpy/tensorflow are listed before, that is, a Shang is considered a column vector;

From the Numpy/tensorflow mathematical point of view, is a two-dimensional tensor, shape is (2,3), its axis is 2; note: two-dimensional vector, the first axis is a column, subscript is 0, the second axis is a row, subscript is 1;

Example:

Import OS, sys

Import NumPy

Import TensorFlow as TF

A = [[1,2,3],[4,5,6]]

b = tf. Variable (A, Dtype=tf.float32)

init = Tf.global_variables_initializer ()

Sess = tf. Session ()

Sess.run (INIT)

D1 = Tf.reduce_mean (b)

D2 = Tf.reduce_mean (b, 0)

D3 = Tf.reduce_mean (b, 1)

Sess.run ([B, B[0,:], b[:, 0]])

# [Array ([[1., 2., 3.],

# [4., 5., 6.]], Dtype=float32),

# array ([1., 2., 3.], Dtype=float32),

# array ([1., 4.], Dtype=float32)]

Sess.run ([D1, D2, D3])

# [3.5,

# array ([2.5, 3.5, 4.5], dtype=float32),

# Array ([2., 5.], Dtype=float32)]

Note:
[B, B[i,:], b[:, I]]
B[i,:]: denotes the first axis (column) of the B matrix, and the column I corresponds to all elements;
B[:,i]: Represents the second axis of the B matrix (row), the line I corresponds to all the elements;
D1=tf.reduce_mean (b): means the mean value of all elements of the matrix;
D2=tf.reduce_mean (b, 0): represents the projection on the first axis (column) direction of the Matrix, that is, the mean value on each row;
D3=tf.reduce_mean (b, 1): represents the projection on the second axis (row) direction of the Matrix, that is, the mean value of each column;
View this URL: http://www.cnblogs.com/silence-tommy/p/6554986.html
There are questions on this website:
Tf.reduce_mean (x) ==> 2.5 #如果不指定第二个参数, then the average of all the elements
Tf.reduce_mean (x, 0) ==> [2., 3.] #指定第二个参数为0, the elements of the first dimension are averaged, that is, each column averages
Tf.reduce_mean (x, 1) ==> [1.5, 3.5] #指定第二个参数为1, the elements of the second dimension are averaged, that is, each row is averaged

These two underlined callouts are wrong. Clearly no understanding of the concept of matrix axes;

(Sorry, this site blogger, just see, so only from the perspective of development analysis, not intentionally, please understand!) )

Python/numpy/tensorflow, the matrix row and column operations, subscript is how to go?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.