Tensorflow 机器学习项目实战 记录

时间:2021-08-01 13:51:27

**

Tensorflow 机器学习项目实战 记录


**

基本操作


简单矩阵运算

import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.constant([[2, 5, 3, -5],
[0, 3, -2, 5],
[4, 3, 5, 3],
[6, 1, 4, 0]])
y = tf.constant([[4, -7, 4, -3, 4],
[6, 4, -7, 4, 7],
[2, 3, 2, 1, 4],
[1, 5, 5, 5, 2]])
floatx = tf.constant([[2., 5., 3., -5.],
[0., 3., -2., 5.],
[4., 3., 5., 3.],
[6., 1., 4., 0.]])
  # 矩阵转置
tf.transpose(x).eval()
array([[ 2, 0, 4, 6],
[ 5, 3, 3, 1],
[ 3, -2, 5, 4],
[-5, 5, 3, 0]]
)
#矩阵相乘  Matrix Multiplication
tf.matmul(x, y).eval()
array([[ 2, 0, 4, 6],
[ 5, 3, 3, 1],
[ 3, -2, 5, 4],
[-5, 5, 3, 0]]
)
#矩阵行列式(determinant)
tf.matrix_determinant(floatx).eval()
817.99969
#逆矩阵(inverse matrix)
tf.matrix_inverse(floatx).eval()
array([[-0.00855745, 0.10513447, -0.18948655, 0.29584354],
[ 0.12958434, 0.12224938, 0.01222495, -0.05134475],
[-0.01955992, -0.18826404, 0.28117359, -0.18092909],
[-0.08557458, 0.05134474, 0.10513448, -0.0415648 ]]
, dtype=float32)
#行列式求解
tf.matrix_solve(floatx, [[1],[1],[1],[1]]).eval()
array([[ 0.202934 ],
[ 0.21271393],
[-0.10757945],
[ 0.02933985]]
, dtype=float32)

**

约简(reduction)

**

x = tf.constant([[1, 2, 3],
[3, 2, 1],
[-1, -2, -3]]
)
boolean_tensor = tf.constant([[True, False, True],
[False, False, True],
[True, False, False]]
)
# 乘积方式降维,reduction_indices=1在行内计算,reduction_indices=0在列内计算
tf.reduce_prod(x, reduction_indices= 1).eval()
array([ 6, 6, -6])
tf.reduce_prod(x, reduction_indices= 0).eval(0)
Out[15]:
array([-3, -8, -9])
# 最小值降维
tf.reduce_min(x, reduction_indices=1).eval()
array([ 1, 1, -3])
#最大值降维
tf.reduce_max(x, reduction_indices=1).eval()
array([ 3, 3, -1])
#平均值
tf.reduce_mean(x, reduction_indices=1).eval()
array([ 2, 2, -2])
# 全部为真,则为真
tf.reduce_all(boolean_tensor,reduction_indices=1).eval()
array([False, False, False], dtype=bool)
# 存在真,则为真
tf.reduce_any(boolean_tensor, reduction_indices=1).eval()

array([ True, True, True], dtype=bool)

张量分解

seg_ids = tf.constant([0, 1, 1, 2, 2]) # 里面的数字代表分解后所在的index位置。分组的索引: 0 | 12 | 34
tens1 = tf.constant([[2, 5, 3, -5],
[0, 3, -2, 5],
[4, 3, 5, 3],
[6, 1, 4, 0],
[6, 1, 4, 0]]
)
#0组求和作为新的012组求和作为新的第134组求和作为新的第2
tf.segment_sum(tens1,segment_ids=seg_ids).eval() # sum segmentation
array([[ 2, 5, 3, -5],
[ 4, 6, 3, 8],
[12, 2, 8, 0]]
)
tf.segment_prod(tens1, segment_ids=seg_ids).eval()
array([[ 2, 5, 3, -5],
[ 0, 9, -10, 15],
[ 36, 1, 16, 0]]
)

还有tf.segment_max,tf.segment_min,tf.segment_mean等

序列

x = tf.constant([[2, 5, 3, -5],
[0, 3, -2, 5],
[4, 3, 5, 3],
[6, 1, 4, 0]]
)
listx = tf.constant([1,2,3,4,5,6,7,8])
listy = tf.constant([4,5,8,9])
boolx = tf.constant([[True, False],
[False, True]]
)
# 返回x中最大元素的坐标
tf.argmax(x,axis=1).eval()
array([1, 3, 2, 0], dtype=int64)
#同理
tf.argmin(x,axis=1).eval()
array([3, 2, 1, 3], dtype=int64)
#tf.listdiff 被移除
#返回真值坐标
tf.where(boolx).eval()
array([[0, 0],
[1, 1]]
, dtype=int64)
#返回listx中不重复包含的数,并且返回这些数的下标index
item, index = tf.unique(listx)
print(item.eval())
print(index.eval())
[1 2 3 4 5 6 7 8]
[0 1 2 3 4 5 6 7]

张量形状变换

x = tf.constant([[2, 5, 3, -5],
[0, 3, -2, 5],
[4, 3, 5, 3],
[6, 1, 4, 0]]
)
tf.shape(x).eval()
array([4, 4])
# tensor大小
tf.size(x).eval()
16
#tensor的rank(秩) 等于维度
tf.rank(x).eval()
2
# -1代表视其他维度而定
tf.reshape(x , [-1, 2]).eval()
array([[ 2, 5],
[ 3, -5],
[ 0, 3],
[-2, 5],
[ 4, 3],
[ 5, 3],
[ 6, 1],
[ 4, 0]]
)
y = tf.reshape(x, [-1, 1])
print(tf.shape(y).eval())
y.eval()
[16 1]
Out[66]:
array([[ 2],
[ 5],
[ 3],
[-5],
[ 0],
[ 3],
[-2],
[ 5],
[ 4],
[ 3],
[ 5],
[ 3],
[ 6],
[ 1],
[ 4],
[ 0]])
"""Given a tensor `input`, this operation returns a tensor of the same type with
all dimensions of size 1 removed. If you don't want to remove all size 1
dimensions, you can remove specific size 1 dimensions by specifying
"""

# 维度大小为1的都被移除
z = tf.squeeze(y)
z.shape
(16,)
z
array([ 2, 5, 3, -5, 0, 3, -2, 5, 4, 3, 5, 3, 6, 1, 4, 0])
  ```python
# 't' is a tensor of shape [2]
tf.shape(tf.expand_dims(t, 0)) # [1, 2]
tf.shape(tf.expand_dims(t, 1)) # [2, 1]
tf.shape(tf.expand_dims(t, -1)) # [2, 1]

# 't2' is a tensor of shape [2, 3, 5]
tf.shape(tf.expand_dims(t2, 0)) # [1, 2, 3, 5]
tf.shape(tf.expand_dims(t2, 2)) # [2, 3, 1, 5]
tf.shape(tf.expand_dims(t2, 3)) # [2, 3, 5, 1]
```
# 在指定的axis上扩展维度
tf.expand_dims(x, 1).eval()
array([[[ 2, 5, 3, -5]],

[[ 0, 3, -2, 5]],

[[ 4, 3, 5, 3]],

[[ 6, 1, 4, 0]]])