forked from JaywayXu/TF_Cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_matrices.py
58 lines (43 loc) · 1.33 KB
/
04_matrices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Matrices and Matrix Operations
#----------------------------------
#
# This function introduces various ways to create
# matrices and how to use them in TensorFlow
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# Declaring matrices
sess = tf.Session()
# Declaring matrices
# Identity matrix
identity_matrix = tf.diag([1.0,1.0,1.0])
print(sess.run(identity_matrix))
# 2x3 random norm matrix
A = tf.truncated_normal([2,3])
print(sess.run(A))
# 2x3 constant matrix
B = tf.fill([2,3], 5.0)
print(sess.run(B))
# 3x2 random uniform matrix
C = tf.random_uniform([3,2])
print(sess.run(C))
print(sess.run(C)) # Note that we are reinitializing, hence the new random variabels
# Create matrix from np array
D = tf.convert_to_tensor(np.array([[1., 2., 3.], [-3., -7., -1.], [0., 5., -2.]]))
print(sess.run(D))
# Matrix addition/subtraction
print(sess.run(A+B))
print(sess.run(B-B))
# Matrix Multiplication
print(sess.run(tf.matmul(B, identity_matrix)))
# Matrix Transpose
print(sess.run(tf.transpose(C))) # Again, new random variables
# Matrix Determinant
print(sess.run(tf.matrix_determinant(D)))
# Matrix Inverse
print(sess.run(tf.matrix_inverse(D)))
# Cholesky Decomposition
print(sess.run(tf.cholesky(identity_matrix)))
# Eigenvalues and Eigenvectors
print(sess.run(tf.self_adjoint_eig(D)))