# Matrices and Matrix Operations

This function introduces various ways to create
matrices and how to use them in TensorFlow


```python
import numpy as np
import tensorflow as tf
# from tensorflow.python.framework import ops
# ops.reset_default_graph()  # 既存のグラフをリセット
tf.reset_default_graph()  # 上と同じこと
```

Start a graph session


```python
sess = tf.Session()
```

### Declaring matrices

Identity Matrix:


```python
identity_matrix = tf.diag([1.0,1.0,1.0])
print(sess.run(identity_matrix))
```

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


2x3 random norm matrix:


```python
A = tf.truncated_normal([2,3])
print(sess.run(A))
```

    [[ 1.5801364  1.1259041 -1.3550861]
     [ 0.86778   -1.3771477  1.9069912]]


2x3 constant matrix:


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

    [[5. 5. 5.]
     [5. 5. 5.]]


3x2 random uniform matrix:


```python
C = tf.random_uniform([3,2])
print(sess.run(C))
```

    [[0.53089714 0.627354  ]
     [0.07326257 0.23314726]
     [0.34148204 0.77786624]]


Create matrix from np array:


```python
D = tf.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.]]


### Matrix Operations

Matrix addition/subtraction:


```python
print(sess.run(A+B))
print(sess.run(B-B))
```

    [[5.1397653 5.7964816 3.9101331]
     [4.082012  6.481913  5.5925517]]
    [[0. 0. 0.]
     [0. 0. 0.]]


Matrix Multiplication:


```python
print(sess.run(tf.matmul(B, identity_matrix)))
```

    [[5. 5. 5.]
     [5. 5. 5.]]


tf.matmul()の引数には各行列を転置するかどうかも指定できる

https://www.tensorflow.org/api_docs/python/tf/matmul

Matrix Transpose:


```python
print(sess.run(tf.transpose(C)))
```

    [[0.49928522 0.22033381 0.87936306]
     [0.32459974 0.53317904 0.41480982]]


Matrix Determinant:


```python
print(sess.run(tf.matrix_determinant(D)))
```

    -37.99999999999999


Matrix Inverse:


```python
print(sess.run(tf.matrix_inverse(D)))
```

    [[-0.5        -0.5        -0.5       ]
     [ 0.15789474  0.05263158  0.21052632]
     [ 0.39473684  0.13157895  0.02631579]]


Cholesky Decomposition:


```python
print(sess.run(tf.cholesky(identity_matrix)))
```

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


Eigenvalues and Eigenvectors:  We use `tf.self_adjoint_eig()` function, which returns two objects, first one is an array of eigenvalues, the second is a matrix of the eigenvectors.


```python
eigenvalues, eigenvectors = sess.run(tf.self_adjoint_eig(D))
print(eigenvalues)
print(eigenvectors)
```

    [-10.65907521  -0.22750691   2.88658212]
    [[ 0.21749542  0.63250104 -0.74339638]
     [ 0.84526515  0.2587998   0.46749277]
     [-0.4880805   0.73004459  0.47834331]]


self-adjoint matrices とは自己随伴行列の英単語

つまりエルミート行列のことをさす


```python
eigenvectors[:, 0]  # eigenvalues[0]に対する固有ベクトル
```




    array([ 0.21749542,  0.84526515, -0.4880805 ])



つまり，`tf.self_adjoint_eig()`関数は

\begin{equation}\label{eq:}
    D \cdot \text{eigenvectors[:, i]} =  \text{eigenvalues[i]} \cdot \text{eigenvectors[:, i]}
\end{equation}

のような固有値分解をしている(ただし, i = 0 ~ D.shape[1])

655a7fb21b008b2d46c29de05af7a3dada07708c
ipynb
Jupyter Notebook
Chap01/04_matrices.ipynb
haru-256/tensorflow_cookbook
18923111eaccb57b47d07160ae5c202c945da750
MIT
2021-02-27T16:16:02.000Z
2021-02-27T16:16:02.000Z
01_Introduction/04_Working_with_Matrices/04_matrices.ipynb
haru-256/tensorflow_cookbook
18923111eaccb57b47d07160ae5c202c945da750
MIT
2018-03-07T14:31:22.000Z
2018-03-07T15:04:17.000Z
Chap01/04_matrices.ipynb
haru-256/tensorflow_cookbook
18923111eaccb57b47d07160ae5c202c945da750
MIT
Qwen/Qwen-72B
1. YES
2. YES