TensorFlow基础
2.1 张量
import tensorflow as tf x = tf.constant([3.0, 1.0], name = "x") y = tf.constant([4.0, 5.0], name = "y") result = tf.add(x, y, name = "add") print(result) ''' Tensor("add_1:0", shape=(2,), dtype=float32) '''
import tensorflow as tf sess = tf.Session() # 通过 tf.Session() 的方式来获取计算图的结果 # 创建张量 x = tf.constant([3.0, 1.0], name = "x") y = tf.constant([4.0, 5.0], name = "y") result = tf.add(x, y, name = "add") print(sess.run(result)) ''' [7. 6.] '''
1. 固定张量
import tensorflow as tf ones_tensor = tf.ones([2, 3]) filled_tensor = tf.fill([2, 2], 3) constant_tensor = tf.constant([1, 2, 3])
2. 相似的形状的张量
import tensorflow as tf constant_tensor = tf.constant([1, 2, 3]) zeros_similar = tf.zeros_like(constant_tensor) one_similar = tf.ones_like(constant_tensor)
3. 序列张量
import tensorflow as tf linear_tensor = tf.linspace(start=0.0, stop=1.0, num = 3) # 所创建的张量的值为:[0.0,0.5,1.0] sequence_tensor = tf.range(start=0, limit=10, delta=3) # 所创建张量的值为[0, 3, 6, 9]
4. 随机张量
import tensorflow as tf randuniform_tensor = tf.random_uniform([2, 2], minval=0.0, maxval=1.0) # 本次创建的均匀分布随机张量为[[0.42212788, 0.96672773],[0.6716949, 0.25362217]] randormal_tensor = tf.random_normal([2, 2], mean=0.0, stddev=1.0) # 本次创建的正态分布随机张量为[[0.06353379 0.13682544][0.73617303, 0.13042414]] truncnormal_tensor = tf.truncated_normal([2,2], mean=0.0, stddev=1.0) # 本次创建正态分布随机张量为[[0.65179425 -0.7365027][0.73617303 0.13042414]] shuffle_output = tf.random_shuffle([[1,2],[3,4],[5,6]]) #随机打乱后的张量为[[1 2][5 6][3 4]] corpped_output = tf.random_crop([[1,2],[3,4],[5,6],[7,8]], [2,2]) # 随机裁剪得到得张量为[[3,4][5,6]]
2.2 会话
import tensorflow as tf sess = tf.Session() a = tf.constant([3.0, 4.0]) b = tf.constant([5.0, 6.0]) result = a + b with sess.as_default(): print(result.eval()) ''' [0.40242875 0.64216566]] '''
2.3 变量于占位符
import tensorflow as tf # 定义变量并初始化为 0 variable_name = tf.Variable(tf.zeros([2, 2])) # 创建会话并运行初始化操作 sess = tf.Session() initalize_op = tf.global_variables_initializer() sess.run(initalize_op)
import numpy as np import tensorflow as tf from tensorflow.python.framework import ops ops.reset_default_graph() sess = tf.Session() x = tf.placeholder(tf.float32, shape=(2,2)) y = tf.identity(x) rand_array = np.random.rand(2,2) merged = tf.summary.FileWriter("/tmp/variable_logs", sess.graph) print(sess.run(y, feed_dict={x: rand_array})) ''' [[0.2956999 0.10164797] [0.40242875 0.64216566]] '''
import tensorflow as tf sess = tf.Session() x = tf.Variable(tf.zeros([1,2])) print(sess.run(x.initializer)) y = tf.Variable(tf.zeros_like(x)) print(sess.run(y.initializer))
2.4 矩阵
2.4.1 创建矩阵
import numpy as np import tensorflow as tf sess = tf.Session() # 创建一个 3x3 的全 0 矩阵 m1 = tf.zeros([3,3]) print(sess.run(m1)) # 创建一个 3x3 的全 1 矩阵 m2 = tf.ones([3,3]) print(sess.run(m2)) # 创建一个 3x3 的填充为 6 的矩阵 m3 = tf.fill([3,3],6) print(sess.run(m3)) # 创建一个常量矩阵 m4 = tf.constant([1,2,3,4,5,6]) print(sess.run(m4)) # 创建一个 3x3 的随机矩阵 m5 = tf.truncated_normal([3,3]) # 创一个 3x3 的正态分布矩阵 m6 = tf.random_uniform([3,3]) print(sess.run(m6)) # 通过 numpy 数组创建矩阵 m7 = tf.convert_to_tensor(np.array([[1,2,3],[2,3,4],[3,4,5]])) print(sess.run(m7)) ''' [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] [[6 6 6] [6 6 6] [6 6 6]] [1 2 3 4 5 6] [[0.13449848 0.64351976 0.99465334] [0.77233136 0.8220886 0.8941928 ] [0.65133333 0.7570833 0.07037628]] [[1 2 3] [2 3 4] [3 4 5]] '''
import tensorflow as tf sess = tf.Session() diagonal = [1,1,1,1] print(sess.run(tf.diag(diagonal))) ''' [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]] '''
import tensorflow as tf sess = tf.Session() diagonal = tf.constant([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]) print(sess.run(tf.diag_part(diagonal))) ''' [1 1 1 1] '''
2.4.2 矩阵的基本运算
矩阵加法和矩阵减法
import tensorflow as tf m1 = tf.zeros([3,3]) m2 = tf.ones([3,3]) print(sess.run(m1+m2)) print(sess.run(m1-m2)) ''' [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] [[-1. -1. -1.] [-1. -1. -1.] [-1. -1. -1.]] '''
矩阵乘法
import tensorflow as tf m1 = tf.zeros([3,3]) m2 = tf.ones([3,3]) print(sess.run(tf.matmul(m1, m2))) ''' [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] '''
矩阵转置
import numpy as np import tensorflow as tf sess = tf.Session() m6 = tf.constant(np.array([[1,2,3],[4,5,6],[7,8,9]])) print(sess.run(m6),'\n') print(sess.run(tf.transpose(m6))) ''' [[1 2 3] [4 5 6] [7 8 9]] [[1 4 7] [2 5 8] [3 6 9]] '''
矩阵的行列式
import tensorflow as tf sess = tf.Session() m3 = tf.fill([3,3],6.0) print(sess.run(m3)) print(sess.run(tf.matrix_determinant(m3))) ''' [[6. 6. 6.] [6. 6. 6.] [6. 6. 6.]] 0.0 '''
逆矩阵
import tensorflow as tf import numpy as np sess = tf.Session() # create a 3x3 matrix filled with random values # m = tf.constant(np.random.rand(3,3)) m = tf.constant([[0.06507223, 0.41723821, 0.39361987], [0.80711338, 0.79708773, 0.78492795], [0.77092674, 0.65978775, 0.46901021]] ) print(sess.run(m), '\n') # calculate the inverse of the matrix m_inv = tf.matrix_inverse(m) with tf.Session() as sess: # evaluate the inverse matrix inv = sess.run(m_inv) print(inv) ''' [[0.06507223 0.4172382 0.39361987] [0.8071134 0.7970877 0.78492796] [0.7709267 0.6597878 0.4690102 ]] [[-2.7230706 1.210201 0.25998154] [ 4.2833314 -5.159649 5.0402927 ] [-1.5496508 5.269176 -5.3857045 ]] '''
矩阵分解
如果输入矩阵不满足正定性条件,那么 tf.cholesky() 函数将会抛出一个异常。
在这种情况下,你可以使用其他方法来分解或近似矩阵。以下代码演示了如何使用 TensorFlow 中的奇异值分解 (SVD) 来近似输入矩阵:
import tensorflow as tf sess = tf.Session() # 定义输入矩阵 #A = tf.constant([[0.06507223, 0.41723821, 0.39361987], # [0.80711338, 0.79708773, 0.78492795], # [0.77092674, 0.65978775, 0.46901021]]) A = tf.fill([3,3],9.0) # 奇异值分解 s, u, v = tf.svd(A) # 构造对角矩阵 s_mat = tf.diag(s) # 近似重构原始矩阵 A_approx = tf.matmul(tf.matmul(u, s_mat), v, transpose_b=True) # 打印结果 print(sess.run(A_approx)) ''' [[8.999999 8.999999 8.999999] [8.999999 8.999999 8.999999] [8.999999 8.999999 8.999999]] '''
特征值于特征向量
import tensorflow as tf m6 = tf.fill([3,3],5.0) print(sess.run(tf.self_adjoint_eig(m6))) ''' (array([-5.9604645e-07, -0.0000000e+00, 1.4999999e+01], dtype=float32), array([[ 0. , -0.81649655, 0.57735026], [-0.70710677, 0.4082483 , 0.57735026], [ 0.7071068 , 0.40824828, 0.57735026]], dtype=float32)) '''