matrix

Initialization of the matrix

The matrix has more elements, so it has more initialization functions. Linear generating functions such as tf.linspace and tf.range are no longer enough.

0 Could be 0 0 initialized by 0 0 in a linear sequence and then 0 0 in a matrix

Ex. :

>>> g1 = tf.linspace(1.0,10.0,16) >>> g1 < tf.tensor 'LinSpace_6:0' shape=(16,) dtype=float32> >>> g2 = Tf. Constant (sess. Run (tf) reshape (g1, [4]))) > > > sess. Run (g2) array ([[1., 1.6, 2.2, 2.8000002], [3.4, 4. 5.2000003], [5.8, 6.4, 7., 7.6000004], [8.200001, 8.8, 9.400001, 10.], dtype=float32) >>> g2 <tf.Tensor 'Const_29:0' shape=(4, 4) dtype=float32>Copy the code

Tf.linspace generates a vector of (16,) and is 0 0 0 in a matrix of (4,4)

Generates a matrix with all zero values

Tf. zeros can generate a matrix of all zeros. If no type is specified, the default value is float32.

> > > the g7 = tf. Zeros ([4, 5)) > > > sess. Run (g7) array ([[... 0, 0, 0, 0), and 0.], [... 0, 0, 0, 0), 0.], [... 0, 0, 0, 0., 0.], [0., 0., 0., 0., 0.]], dtype=float32)Copy the code

You can specify the data type:

> > > the g8 = tf. Zeros ([10, 10], dtype = tf. Int32) > > > sess. Run (g8) array ([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)Copy the code

Generate a matrix of all 1s

Similarly, we can use tF.ones to generate matrices with all values of 1. Ex. :

> > > g9 = tf. 'ones ([8, 2], dtype = tf int64) > > > sess. Run (g9) array ([[1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1), [1, 1]])Copy the code

Set the matrix all to one value

Tf.ones and tF.Zeros are special cases, and tF.fill is more general:

> > > g10 = tf. The fill (5, 5), (10.1) > > > sess. Run (g10) array ([[10.1, 10.1, 10.1, 10.1, 10.1], [10.1, 10.1, 10.1, 10.1, 10.1]. [10.1, 10.1, 10.1, 10.1, 10.1], [10.1, 10.1, 10.1, 10.1, 10.1], [10.1, 10.1, 10.1, 10.1, 10.1]], dtype = float32)Copy the code

Generating diagonal matrix

A characteristic of matrices is that they often have only sparse values. The most common one is the diagonal matrix, where only one diagonal has a value. Ex. :

> > > g11 = tf. Diag (,1,2,2 [1]) > > > sess. Run (g11) array ([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]]. dtype=int32)Copy the code

In addition to generating a diagonal matrix, we can also get the diagonal values from a matrix into a vector:

>>> g12 = tf.diag_part(g11)
>>> sess.run(g12)
array([1, 1, 2, 2], dtype=int32)
>>> g12
<tf.Tensor 'DiagPart:0' shape=(4,) dtype=int32>Copy the code

Generate initialization values randomly

In addition to all zeros, all ones, all deterministic values and diagonal values, another very common way to generate random values is to generate random values. We can generate initial values as normal distribution:

>>> g13 = tf.random_normal([5,5]) >>> sess.run(g13) array([[0.21010283, 1.083522, -2.1688387, -1.2340024, 0.9230036], [0.43592915, -0.7187195, -1.3310403, 0.27570882, 1.3831469], [-0.42430717, 2.8005996, 1.1899991, 0.6987934, 1.6732428], [0.4975314, -1.259698, 1.2508341, -1.2581793, -0.8776101], [0.49039882, 0.8129552, 1.2836359, -0.3732389, -2.034603]], dType =float32)Copy the code

You can specify the mean and standard deviation, with the default mean being 0 and standard deviation being 1. The default type is float32, which does not support integers anyway.

Ex. :

>>> g14 = tf.random_normal([3,8], mean=1.0, stddev=2.0, dtype=tf.float32) -2.7150466, -2.107638, 1.7130036, -0.8702172, -1.0325654, 3.1230848, -0.82150674], [-1.3860679, 0.03262603, 0.63146615, -0.71946084, 1.182011, 0.34882843, 2.3536258, -1.0503623], [-3.6498313, 0.4458651, 2.9859743, 2.153699, 3.8967788, 1.895072, 3.5918627, 1.9855003]], dType =float32)Copy the code

The transpose of a matrix

The symmetrical exchange of elements in the matrix based on the diagonal is called transpose of the matrix.

Ex. :

>>> g3 = tf.transpose(g2) >>> g3 <tf.Tensor 'transpose_1:0' shape=(4, 4) dtype=float32> >>> sess.run(g3) array([[ 1. , 3.4, 5.8, 8.200001], [1.6, 4., 6.4, 8.8], [2.2, 4.6000004, 7., 9.400001], [2.8000002, 5.2000003, 7.6000004, 10.]], dtype = float32)Copy the code

1,4,7, and 10 are diagonal lines that don’t change when we transpose them.

In the case of non-square matrices, the post-transpose diagonal remains the same. Let’s look at an example of a 2 by 3 matrix:

> > > g4 = tf. Linspace (1.0, 10.0, 6) > > > the g5 = tf. Reshape (g4, [2, 3]) > > > sess. Run (g5) array ([[1., 2.8, 4.6], [6.3999996, ], dType =float32)Copy the code

The diagonal lines are 1 and 8.2. Let’s transpose:

> > > g6 = tf. Constant (sess. Run (tf) transpose (g5))) > > > sess. Run (g6) array ([[1., 6.3999996], [2.8, 8.2], [4.6, 10. ]], dtype=float32)Copy the code

Although a wide matrix becomes a high matrix, the diagonals are still 1 and 8.2.

The mathematics of matrices

Addition and subtraction

Two matrices with the same row and column can be added and subtracted. Ex. :

> > > h01-2 = tf. Random_normal ([4]) > > > h02 = tf. The fill ([4], 1.0) > > > h03 = h01-2 + h02 > > > sess. Run (h03) array ([[1.959749, 1.2833667, 0.12137735, 1.0297428], [1.3971953, -0.0582509, 1.1770982, 2.154177], [-1.1314301, 1.6063341, -1.2442939, 1.2752731], [1.3077021, 0.42679614, 2.9681108, 1.6179581]], dType =float32)Copy the code

Broadcast operations

Ex. :

> > > h04 h02 + 2.0 = > > > sess. Run (h04) array ([[3, 3), (3), (3)], [3, 3), (3), (3)], [3, 3), (3), (3)], [3), (3), (3), (3)]]. dtype=float32)Copy the code

Product of matrices

In matrix multiplication, as in the previous section, the Hadamard product is the product of the corresponding elements, for example:

> > > h05 = tf. Reshape (tf) linspace (1.0, 10.0, 16), [4]) > > > sess. Run (h05) array ([[1., 1.6, 2.2, 2.8000002], [3.4, , 4.6000004, 5.2000003], [5.8, 6.4, 7., 7.6000004], [8.200001, 8.8, 9.400001, 10.], Dtype = float32) > > > h06. = tf reshape (tf) linspace (1.0, 16.0, 16), [4]) > > > sess. Run (h06) array ([[1, 2, 3., 4], [5., 6, 7, 8], [... 9, 10, 11, 12.], [. 13, 14, 15, 16.]], dtype = float32) > > > sess. Run (h05 * h06) array ([[1., 3.2, [17., 24., 32.200005, 41.600002], [52.2., 64., 77., 91.200005], [106.600006, ], dType =float32)Copy the code

We can also use the matmul function, or the “@” operator, to compute matrix multiplication:

>>> h05@h06 <tf.Tensor 'matmul:0' shape=(4, 4) dType =float32> >>> sess.run(h05@h06) array([[65.200005, 72.8, [132.40001, 149.6, 166.80002, 184.], [199.6, 226.40002, 253.20001, 280.], [266.8, 303.2, ], dType =float32)Copy the code

“@” is a supported operation in higher versions of Python, overloading it in TensorFlow as matmul.

Inverse Matrices

Let’s say I is the identity diagonal matrix, and if BA is equal to I, then I say B is the inverse of A. We can obtain the inverse matrix by using the matrix_inverse function, for example:

> > > i01 = tf. Diag ([1.0, 2.0, 3.0, 4.0]) > > > sess. Run (i01) array ([[. 1, 0, 0), and 0.], [. 0. 2, 0, 0.], [0., 0. 3, 0.]. [0., 0., 0., 4.]], dtype=float32) >>> i01_rev = tf.matrix_inverse(i01) >>> sess.run(i01_rev) array([[1. , 0. , 0. , 0.], [0, 0.5, 0., 0.], [0., 0., 0.33333334, 0.], [.. 0, 0, 0., 0.25]], dtype = float32)Copy the code

Let’s check if i01_rev multiplied by i01 is the identity matrix:

>>> sess.run( i01_rev @ i01)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]], dtype=float32)Copy the code

So he is.

Diagonal matrices are special and also satisfy the commutative law:

>>> sess.run( i01 @ i01_rev)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]], dtype=float32)Copy the code

Evaluate the determinant to see if there is an inverse

We learned in linear algebra that if a matrix is going to have an inverse, its determinant must not be 0.

In Matlab and Mathematica, the determinant function is simply called det. Tensorflow, going by a long name because it’s a library, called TF. Matrix_determinant.

Let’s look at an example:

,1,1 > > > A1 = [[1], [1, 1, 1], [5, 2, 2]] > > > A = tf. The constant (A1, tf. Float32) > > > A < tf. The Tensor Const_3: '0' shape = (3, 3) dtype=float32> >>> sess.run(A) array([[ 1., 1., 1.], [ 1., -1., -1.], [ 5., -2., 2.]], >>> D = tf. matrix_primitive (A) >>> sess.run(D) -8.0Copy the code

Use inverse matrix to solve linear equations

Assume the following equations and solve them:

x+y+z =1,
x-y-z = 2,
5x-2y+2z = 3Copy the code

The coefficient matrix in this case is the matrix in our example, and we already figured out that the determinant is negative 8 not equal to 0, so we can solve for it by multiplying the inverse of the coefficient matrix times the constant vector.

>>> b = tf.constant([[1],[2],[3]],dtype=tf.float32) >>> b <tf.Tensor 'Const_4:0' shape=(3, 1) dtype=float32> >>> sess.run(b) array([[1.], [2.], [3.]], Dtype =float32) >>> Sess.run (tf.matmul(tf.Matrix_inverse (A),b) array([[1.5000001], [0.875], [-1.3750001]], dtype=float32)Copy the code

Finally, x=1.5, y=0.875, z = -1.375.