1 >>> import tensorflow as tf
2 >>> sess = tf.compat.v1.Session()
3 >>> from tensorflow.python.framework import ops
4 >>> ops.reset_default_graph()
5 >>> tf.compat.v1.disable_eager_execution()


# 创建一个矩阵¶

## 对角矩阵¶

>>> identiy_matrix = tf.compat.v1.diag([1.0, 1.0, 1.0])
>>> print(sess.run(identiy_matrix))
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]


## 随机矩阵¶

>>> A = tf.compat.v1.truncated_normal([2,3])
>>> print(sess.run(A))
[[ 0.19759183 -1.436814   -1.107715  ]
[-0.6905967  -0.19711868  0.6596967 ]]


## 常数矩阵¶

>>> B = tf.fill([2,3],5.0)
>>> print(sess.run(B))
[[5. 5. 5.]
[5. 5. 5.]]


## 随机矩阵¶

>>> C = tf.compat.v1.random_uniform([3,2])
>>> print(sess.run(C))
[[0.3477279  0.39023817]
[0.38307    0.8967395 ]
[0.8217212  0.32184577]]


## convert_to_tensor¶

>>> D = tf.compat.v1.convert_to_tensor(np.array([[1.,2.,3.],[-3.,-7.,-1.],[0.,5.,-2.]]))
>>> print(sess.run(D))
[[ 1.  2.  3.]
[-3. -7. -1.]
[ 0.  5. -2.]]


## 非传统意义上的矩阵¶

>>> E = tf.zeros([2,3,3])
>>> print(sess.run(E))
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]]


# 矩阵加减法¶

## 加法¶

>>> print(sess.run(A+B))
[[4.2034802 5.6497774 6.104109 ]
[3.8710573 5.6505775 4.063135 ]]


## 减法¶

>>> print(sess.run(B-B))
[[0. 0. 0.]
[0. 0. 0.]]


## 乘法¶

>>> print(sess.run(tf.matmul(B, identiy_matrix)))
[[5. 5. 5.]
[5. 5. 5.]]

# 矩阵运算需要注意两个的维度，否则容易出错
>>> print(sess.run(tf.matmul(A, B)))
Traceback (most recent call last):
...
ValueError: Dimensions must be equal

# 如果对某个模块不明白，可以调用help函数
>>> help(tf.matmul)
Help on function matmul in module tensorflow.python.ops.math_ops:
...
...


## 矩阵的转置¶

>>> print(sess.run(tf.transpose(C)))
[[0.11786842 0.32758367 0.54398596]
[0.35542393 0.546188   0.6743456 ]]

# 对于行列式，可以用
>>> print(sess.run(tf.compat.v1.matrix_determinant(D)))
-37.99999999999999


## 矩阵的逆(inverse)¶

# 注意，如果矩阵是对称正定矩阵，则矩阵的逆是基于Cholesky分解，否则基于LU分解。
>>> print(sess.run(tf.compat.v1.matrix_inverse(D)))
[[-0.5        -0.5        -0.5       ]
[ 0.15789474  0.05263158  0.21052632]
[ 0.39473684  0.13157895  0.02631579]]
>>> print(sess.run(tf.compat.v1.cholesky(identiy_matrix)))
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]


## 矩阵的本征值与向量¶

# 对于矩阵的本征值和本征向量，用下面的代码
[-10.65907521  -0.22750691   2.88658212]
[-10.65907521  -0.22750691   2.88658212]
[[ 0.21749542  0.63250104 -0.74339638]
[ 0.84526515  0.2587998   0.46749277]
[-0.4880805   0.73004459  0.47834331]]
>>> eigenvalues
array([-10.65907521,  -0.22750691,   2.88658212])
>>> eigenvectors
array([[ 0.21749542,  0.63250104, -0.74339638],
[ 0.84526515,  0.2587998 ,  0.46749277],
[-0.4880805 ,  0.73004459,  0.47834331]])


# 本章学习模块¶

tf.compat.v1.diag模块介绍

Returns a diagonal tensor with a given diagonal values.

Given a diagonal, this operation returns a tensor with the diagonal and everything else padded with zeros. The diagonal is computed as follows:

Assume diagonal has dimensions [D1,…, Dk], then the output is a tensor of rank 2k with dimensions [D1,…, Dk, D1,…, Dk] where:

output[i1,…, ik, i1,…, ik] = diagonal[i1, …, ik] and 0 everywhere else.

For example:

 # ‘diagonal’ is [1, 2, 3, 4] tf.diag(diagonal) ==> [[1, 0, 0, 0]

[0, 2, 0, 0] [0, 0, 3, 0] [0, 0, 0, 4]]
param diagonal: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int32, int64, complex64, complex128. Rank k tensor where k is at most 1. A name for the operation (optional). A Tensor. Has the same type as diagonal.

tf.compat.v1.convert_to_tensor模块介绍

Converts the given value to a Tensor.

This function converts Python objects of various types to Tensor objects. It accepts Tensor objects, numpy arrays, Python lists, and Python scalars. For example:

python import numpy as np

def my_func(arg):
arg = tf.convert_to_tensor(arg, dtype=tf.float32) return tf.matmul(arg, arg) + arg

# The following calls are equivalent. value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]])) value_2 = my_func([[1.0, 2.0], [3.0, 4.0]]) value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)) 

This function can be useful when composing a new operation in Python (such as my_func in the example above). All standard Python op constructors apply this function to each of their Tensor-valued inputs, which allows those ops to accept numpy arrays, Python lists, and scalars in addition to Tensor objects.

Note: This function diverges from default Numpy behavior for float and
string types when None is present in a Python list or scalar. Rather than silently converting None values, an error will be thrown.
param value: param dtype: An object whose type has a registered Tensor conversion function. Optional element type for the returned tensor. If missing, the type is inferred from the type of value. Optional name to use if a new Tensor is created. Optional element type for the returned tensor, used when dtype is None. In some cases, a caller may not have a dtype in mind when converting to a tensor, so preferred_dtype can be used as a soft preference. If the conversion to preferred_dtype is not possible, this argument has no effect. same meaning as preferred_dtype, and overrides it. A Tensor based on value. TypeError – If no conversion function is registered for value to dtype. RuntimeError – If a registered conversion function returns an invalid value. ValueError – If the value is a tensor not of given dtype in graph mode.

tf.matmul模块介绍

Multiplies matrix a by matrix b, producing a * b.

The inputs must, following any transpositions, be tensors of rank >= 2 where the inner 2 dimensions specify valid matrix multiplication dimensions, and any further outer dimensions specify matching batch size.

Both matrices must be of the same type. The supported types are: float16, float32, float64, int32, complex64, complex128.

Either matrix can be transposed or adjointed (conjugated and transposed) on the fly by setting one of the corresponding flag to True. These are False by default.

If one or both of the matrices contain a lot of zeros, a more efficient multiplication algorithm can be used by setting the corresponding a_is_sparse or b_is_sparse flag to True. These are False by default. This optimization is only available for plain matrices (rank-2 tensors) with datatypes bfloat16 or float32.

A simple 2-D tensor matrix multiplication:

>>> a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
>>> a  # 2-D tensor
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
[4, 5, 6]], dtype=int32)>
>>> b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
>>> b  # 2-D tensor
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 7,  8],
[ 9, 10],
[11, 12]], dtype=int32)>
>>> c = tf.matmul(a, b)
>>> c  # a * b
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[ 58,  64],
[139, 154]], dtype=int32)>


A batch matrix multiplication with batch shape :

>>> a = tf.constant(np.arange(1, 13, dtype=np.int32), shape=[2, 2, 3])
>>> a  # 3-D tensor
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 1,  2,  3],
[ 4,  5,  6]],
[[ 7,  8,  9],
[10, 11, 12]]], dtype=int32)>
>>> b = tf.constant(np.arange(13, 25, dtype=np.int32), shape=[2, 3, 2])
>>> b  # 3-D tensor
<tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=
array([[[13, 14],
[15, 16],
[17, 18]],
[[19, 20],
[21, 22],
[23, 24]]], dtype=int32)>
>>> c = tf.matmul(a, b)
>>> c  # a * b
<tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy=
array([[[ 94, 100],
[229, 244]],
[[508, 532],
[697, 730]]], dtype=int32)>


Since python >= 3.5 the @ operator is supported (see [PEP 465](https://www.python.org/dev/peps/pep-0465/)). In TensorFlow, it simply calls the tf.matmul() function, so the following lines are equivalent:

>>> d = a @ b @ [, ]
>>> d = tf.matmul(tf.matmul(a, b), [, ])

param a: param b: tf.Tensor of type float16, float32, float64, int32, complex64, complex128 and rank > 1. tf.Tensor with same type and rank as a. If True, a is transposed before multiplication. If True, b is transposed before multiplication. If True, a is conjugated and transposed before multiplication. If True, b is conjugated and transposed before multiplication. If True, a is treated as a sparse matrix. Notice, this does not support tf.sparse.SparseTensor, it just makes optimizations that assume most values in a are zero. See tf.sparse.sparse_dense_matmul for some support for tf.SparseTensor multiplication. If True, b is treated as a sparse matrix. Notice, this does not support tf.sparse.SparseTensor, it just makes optimizations that assume most values in a are zero. See tf.sparse.sparse_dense_matmul for some support for tf.SparseTensor multiplication. Name for the operation (optional). A tf.Tensor of the same type as a and b where each inner-most matrix is the product of the corresponding matrices in a and b, e.g. if all transpose or adjoint attributes are False: output[…, i, j] = sum_k (a[…, i, k] * b[…, k, j]), for all indices i, j. Note: This is matrix product, not element-wise product. ValueError – If transpose_a and adjoint_a, or transpose_b and adjoint_b are both set to True.

tf.transpose模块介绍

Transposes a, where a is a Tensor.

Permutes the dimensions according to the value of perm.

The returned tensor’s dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to (n-1…0), where n is the rank of the input tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input Tensors.

If conjugate is True and a.dtype is either complex64 or complex128 then the values of a are conjugated and transposed.

@compatibility(numpy) In numpy transposes are memory-efficient constant time operations as they simply return a new view of the same data with adjusted strides.

TensorFlow does not support strides, so transpose returns a new tensor with the items permuted. @end_compatibility

For example:

>>> x = tf.constant([[1, 2, 3], [4, 5, 6]])
>>> tf.transpose(x)
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 4],
[2, 5],
[3, 6]], dtype=int32)>


Equivalently, you could call tf.transpose(x, perm=[1, 0]).

If x is complex, setting conjugate=True gives the conjugate transpose:

>>> x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
...                  [4 + 4j, 5 + 5j, 6 + 6j]])
>>> tf.transpose(x, conjugate=True)
<tf.Tensor: shape=(3, 2), dtype=complex128, numpy=
array([[1.-1.j, 4.-4.j],
[2.-2.j, 5.-5.j],
[3.-3.j, 6.-6.j]])>


‘perm’ is more useful for n-dimensional tensors where n > 2:

>>> x = tf.constant([[[ 1,  2,  3],
...                   [ 4,  5,  6]],
...                  [[ 7,  8,  9],
...                   [10, 11, 12]]])


As above, simply calling tf.transpose will default to perm=[2,1,0].

To take the transpose of the matrices in dimension-0 (such as when you are transposing matrices where 0 is the batch dimesnion), you would set perm=[0,2,1].

>>> tf.transpose(x, perm=[0, 2, 1])
<tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=
array([[[ 1,  4],
[ 2,  5],
[ 3,  6]],
[[ 7, 10],
[ 8, 11],
[ 9, 12]]], dtype=int32)>
`

Note: This has a shorthand linalg.matrix_transpose):

param a: param perm: A Tensor. A permutation of the dimensions of a. This should be a vector. Optional bool. Setting it to True is mathematically equivalent to tf.math.conj(tf.transpose(input)). A name for the operation (optional). A transposed Tensor.

tf.compat.v1.matrix_determinant模块介绍

Computes the determinant of one or more square matrices.

The input is a tensor of shape […, M, M] whose inner-most 2 dimensions form square matrices. The output is a tensor containing the determinants for all input submatrices […, :, :].

param input: A Tensor. Must be one of the following types: half, float32, float64, complex64, complex128. Shape is […, M, M]. A name for the operation (optional). A Tensor. Has the same type as input.

tf.compat.v1.matrix_inverse模块介绍

Computes the inverse of one or more square invertible matrices or their

The input is a tensor of shape […, M, M] whose inner-most 2 dimensions form square matrices. The output is a tensor of the same shape as the input containing the inverse for all input submatrices […, :, :].

The op uses LU decomposition with partial pivoting to compute the inverses.

If a matrix is not invertible there is no guarantee what the op does. It may detect the condition and raise an exception or it may simply return a garbage result.

param input: A Tensor. Must be one of the following types: float64, float32, half, complex64, complex128. Shape is […, M, M]. An optional bool. Defaults to False. A name for the operation (optional). A Tensor. Has the same type as input.

tf.compat.v1.cholesky模块介绍

Computes the Cholesky decomposition of one or more square matrices.

The input is a tensor of shape […, M, M] whose inner-most 2 dimensions form square matrices.

The input has to be symmetric and positive definite. Only the lower-triangular part of the input will be used for this operation. The upper-triangular part will not be read.

The output is a tensor of the same shape as the input containing the Cholesky decompositions for all input submatrices […, :, :].

Note: The gradient computation on GPU is faster for large matrices but not for large batch dimensions when the submatrices are small. In this case it might be faster to use the CPU.

param input: A Tensor. Must be one of the following types: float64, float32, half, complex64, complex128. Shape is […, M, M]. A name for the operation (optional). A Tensor. Has the same type as input.