Tensorflow2 的常用函数11-20
11. tf.data.Dataset.from_tensor_slices()数据集切片
该函数是dataset核心函数之一,它的作用是把给定的元组、列表和张量等数据进行特征切片。切片的范围是从最外层维度开始的。如果有多个特征进行组合,那么一次切片是把每个组合的最外维度的数据切开,分成一组一组的。
import tensorflow as tf
features = tf.constant([12, 23, 10, 17])
labels = tf.constant([0, 1, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
for element in dataset:
print(element)
结果:
(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=23>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=17>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
也就是说,12-0 23-1 10-1 17-0 分别对应。在制作和使用数据集的时候,常常用此函数将特征标签进行组合。
12. tf.GradientTape() 求导
梯度下降求导的核心函数。 这里
import tensorflow as tf
with tf.GradientTape() as tape:
x = tf.Variable(tf.constant(3.0))
y = tf.pow(x, 2)
grad = tape.gradient(y, x)
print(grad)
y的值是x的平方,对x进行求导,结果是2x=6。如下:
tf.Tensor(6.0, shape=(), dtype=float32)
13. enumerate() 枚举
python 自身的枚举类型函数。遍历的时候,第一个元素是起始位置,第二个元素为值。
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
输出结果:
0 one
1 two
2 three
14. tf.one_hot() 独热编码
首先肯定需要解释下什么叫做独热编码(one-hot encoding),独热编码一般是在有监督学习中对数据集进行标注时候使用的,指的是在分类问题中,将存在数据类别的那一类用X表示,不存在的用Y表示,这里的X常常是1, Y常常是0。例如:有一个5类分类问题,我们有数据(Xi,Yi),其中类别Yi有五种取值(因为是五类分类问题),所以如果Yj为第一类那么其独热编码为: [1,0,0,0,0],如果是第二类那么独热编码为:[0,1,0,0,0],也就是说只对存在有该类别的数的位置上进行标记为1,其他皆为0。
那么tf.one_hot()又是如何使用呢? 深度classes可以理解为列数,另外还需要有需要编码的标签。在下面这个例子中,4对应的是位置5,也就是[0][4]。所以最后一个元素为1。第二行数据0明显对于第一个元素[1][0]。
import tensorflow as tf
classes = 5
labels = tf.constant([4, 0, 3]) # 输入的元素值最小为0,最大为2
output = tf.one_hot(labels, depth=classes)
print("result of labels1:", output)
print("\n")
输出结果:
result of labels1: tf.Tensor(
[[0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0.]], shape=(3, 5), dtype=float32)
15. tf.nn.softmax() 概率输出
经过softmax层后输出的概率。一般来说,数值越大,输出概率越高。负数的概率最小,正数最大,0最小。 至于softmax()的推导,这里不解释。
import tensorflow as tf
y = tf.constant([1.01, 2.01, -0.66])
y_pro = tf.nn.softmax(y)
print("After softmax, y_pro is:", y_pro) # y_pro 符合概率分布
print("The sum of y_pro:", tf.reduce_sum(y_pro)) # 通过softmax后,所有概率加起来和为1
输出的结果:
After softmax, y_pro is: tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32)
The sum of y_pro: tf.Tensor(1.0, shape=(), dtype=float32)
16.tf.squeeze() 删除
这个张量是将原始input中所有维度为1的那些维都删掉的结果。为什么要删掉呢?主要是为了观测对比效果。
例如:
import tensorflow as tf
x1 = tf.constant([[5.8, 4.0, 1.2, 0.2]]) # 5.8,4.0,1.2,0.2(0)
w1 = tf.constant([[-0.8, -0.34, -1.4],
[0.6, 1.3, 0.25],
[0.5, 1.45, 0.9],
[0.65, 0.7, -1.2]])
b1 = tf.constant([2.52, -3.1, 5.62])
y = tf.matmul(x1, w1) + b1
print("x1.shape:", x1.shape)
print("w1.shape:", w1.shape)
print("b1.shape:", b1.shape)
print("y.shape:", y.shape)
print("y:", y)
#####以下代码可将输出结果y转化为概率值#####
y_dim = tf.squeeze(y) # 去掉y中纬度1(观察y_dim与 y 效果对比)
y_pro = tf.nn.softmax(y_dim) # 使y_dim符合概率分布,输出为概率值了
print("y_dim:", y_dim)
print("y_pro:", y_pro)
结果:观察y_dim与 y 几乎完全相同
w1.shape: (4, 3)
b1.shape: (3,)
y.shape: (1, 3)
y: tf.Tensor([[ 1.0099998 2.008 -0.65999985]], shape=(1, 3), dtype=float32)
y_dim: tf.Tensor([ 1.0099998 2.008 -0.65999985], shape=(3,), dtype=float32)
y_pro: tf.Tensor([0.2563381 0.69540703 0.04825491], shape=(3,), dtype=float32)
17.assign_sub() 自减函数
设定值为自减一。x的最终值为3
import tensorflow as tf
x = tf.Variable(4)
x.assign_sub(1)
print("x:", x) # 4-1=3
18.tf.argmax() 最大值索引
可选参数axis=0,axis=1。分别代表列和行。注意返回值是索引。
import numpy as np
import tensorflow as tf
test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print("test:\n", test)
print("每一列的最大值的索引:", tf.argmax(test, axis=0)) # 返回每一列最大值的索引
print("每一行的最大值的索引", tf.argmax(test, axis=1)) # 返回每一行最大值的索引
结果
每一列的最大值的索引: tf.Tensor([3 3 1], shape=(3,), dtype=int64)
每一行的最大值的索引 tf.Tensor([2 2 0 0], shape=(4,), dtype=int64)
19.tf.greater(a, b) 判断大小
a的值大返回a,b的值大返回b。可以扩展到多维。例子看20。
20.tf.where() 选择条件语句
类似于c语言 a>b?a:b 但是不完全一样,可以扩展到多维。
import tensorflow as tf
a = tf.constant([1, 2, 3, 1, 1])
b = tf.constant([0, 1, 3, 4, 5])
c = tf.where(tf.greater(a, b), a, b) # 若a>b,返回a对应位置的元素,否则返回b对应位置的元素
print("c:", c)
a的前两位较大,返回a。b的后两位较大,返回b。所以结果如下:
c: tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)