Tensorflow 2.1 common function examples and detailed explanations (1)

1. Preparation

        Import library functions:

import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn import datasets
from pandas import DataFrame
from matplotlib import pyplot as plt

Two, function

        1. Commonly used cast function to modify data type

        cast (tensor, modified data type)

        Type can be:

tf.int8、tf.int16、tf.int32...
tf.float8、tf.float16、tf.float32...

        Example:

x1 = tf.constant([1., 2., 3.], dtype=tf.float64)
print("x1:", x1)
# cast函数可以修改tensor的数据类型
x2 = tf.cast(x1, tf.int32)
print("x2", x2)
# reduce_min和reduce_max函数分别可以返回张量中的最大和最小值
print("minimum of x2:", tf.reduce_min(x2))
print("maxmum of x2:", tf.reduce_max(x2))

        result:

        2. Functions reduce_mean and reduce_sum for calculating tensor sums and average values

        The first parameter is tensor, the second parameter is axis, axis=1 is the operation on the column, and equal to 0 is the operation on the row

        example:

x = tf.constant([[1, 2, 3], [2, 2, 3]])
print("x:", x)
# reduce_mean函数有两个参数,一个是张量,一个是axis,axis为零表示对行求平均值,为一表示对列求平均值
# reduce_sum也类似于以上函数
print("mean of x:", tf.reduce_mean(x))  # 求x中所有数的均值
print("sum of x:", tf.reduce_sum(x, axis=1))  # 求每一行的和

         result:

        

         3. Four arithmetic operations on tensors        

a = tf.ones([1, 3])
b = tf.fill([1, 3], 3.)
print("a:", a)
print("b:", b)
# 对应元素相加
print("a+b:", tf.add(a, b))
# 对应元素相减
print("a-b:", tf.subtract(a, b))
# 对应元素相乘
print("a*b:", tf.multiply(a, b))
# 对应元素相除
print("b/a:", tf.divide(b, a))

        result:

   

    4. Calculation of tensor

a = tf.fill([1, 2], 3.)
print("a:", a)
# pow为次方计算
print("a的次方:", tf.pow(a, 3))
# 平方计算
print("a的平方:", tf.square(a))
# 开根号计算
print("a的开方:", tf.sqrt(a))

        result:

        5. Matrix multiplication

a = tf.ones([3, 2])
b = tf.fill([2, 3], 3.)
print("a:", a)
print("b:", b)
# 矩阵乘法
print("a*b:", tf.matmul(a, b))

         result:

        6. Feature pairing function:

        The from_tensor_slices function is a function that pairs labels and features;
        the first parameter is the feature, and the second parameter is the label
        numpy and tensor can be used to read data

        example: 

features = tf.constant([12, 23, 10, 17])
labels = tf.constant([0, 1, 1, 0])
# from_tensor_slices函数是将标签和特征相配对的函数;
# 第一个参数是特征,第二个参数是标签
# numpy和tensor都可以用它读入数据
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
for element in dataset:
    print(element)

         result:

        7. Gradient derivation function

        See another blog for detailed usage of tf.GradientTape() and tf.gradient() functions:

        https://blog.csdn.net/qq_46006468/article/details/119357429?spm=1001.2014.3001.5501

        example:

# with结构用于记录计算过程,gradient求出梯度
with tf.GradientTape() as tape:
    x = tf.Variable(tf.constant(3.0))
    y = tf.pow(x, 2)
grad = tape.gradient(y, x)
print(grad)

         result:

        8. The enumerate function traverses all elements and returns the index

        example:

# enumerate函数可以用于遍历元组、列表、字符串的元素
# 组合为:索引号 元素
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
    print(i, element)

         result:

        9. One hot encoding function one_hot

         The one_hot function can be used as a label in the classification problem.
        Each one-hot vector is composed of 0 and 1.
        For example, [0,0,0,0,1,0,0,0] is represented as the fifth class
        parameter. The tensor to convert, and the dimension to convert to a 0-1 vector

# one_hot函数可以在分类问题中用独热码做标签
# 每个独热向量由0,1组成
# 如 [0,0,0,0,1,0,0,0]表示为第五个类
# 参数分别为要转换的张量,以及转换为0-1向量的维度
classes = 3
labels = tf.constant([1, 0, 2])  # 输入的元素值最小为0,最大为2
output = tf.one_hot(labels, depth=classes)
print("result of labels1:", output)
print("\n")

         result:

 

        10. Parameter self-updating function assign_sub()

# assign一类的函数可以对参数做自更新
# 如assign_sub函数可以让参数自减后返回
# 更新前参数必须声明为Variable才可更新
x = tf.Variable(4)
x.assign_sub(1)
print("x:", x)  # 4-1=3

        result: 

 

        11. The argmax function returns the maximum value according to the axis

        example:

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))  # 返回每一行最大值的索引

         result:

3. Simple example: matrix multiplication

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的维度,由于y此时为[[x,x,x]],需要降低纬度
y_pro = tf.nn.softmax(y_dim)  # 使y_dim符合概率分布,输出为概率值了
print("y_dim:", y_dim)
print("y_pro:", y_pro)

 result:

 4. Simple example: iris classification

# 导入数据,分别为输入特征和标签
x_data = datasets.load_iris().data
y_data = datasets.load_iris().target
# 随机打乱数据(因为原始数据是顺序的,顺序不打乱会影响准确率)
# seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
np.random.seed(116)  # 使用相同的seed,保证输入特征和标签一一对应
np.random.shuffle(x_data)
np.random.seed(116)
np.random.shuffle(y_data)
tf.random.set_seed(116)
# 将打乱后的数据集分割为训练集和测试集,训练集为前120行,测试集为后30行
x_train = x_data[:-30]
y_train = y_data[:-30]
x_test = x_data[-30:]
y_test = y_data[-30:]
# 转换x的数据类型,否则后面矩阵相乘时会因数据类型不一致报错
x_train = tf.cast(x_train, tf.float32)
x_test = tf.cast(x_test, tf.float32)
# from_tensor_slices函数使输入特征和标签值一一对应。(把数据集分批次,每个批次batch组数据)
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
# 生成神经网络的参数,4个输入特征故,输入层为4个输入节点;因为3分类,故输出层为3个神经元
# 用tf.Variable()标记参数可训练
# 使用seed使每次生成的随机数相同(方便教学,使大家结果都一致,在现实使用时不写seed)
w1 = tf.Variable(tf.random.truncated_normal([4, 3], stddev=0.1, seed=1))
b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))
lr = 0.1  # 学习率为0.1
train_loss_results = []  # 将每轮的loss记录在此列表中,为后续画loss曲线提供数据
test_acc = []  # 将每轮的acc记录在此列表中,为后续画acc曲线提供数据
epoch = 500  # 循环500轮
loss_all = 0  # 每轮分4个step,loss_all记录四个step生成的4个loss的和
# 训练部分
for epoch in range(epoch):  #数据集级别的循环,每个epoch循环一次数据集
    for step, (x_train, y_train) in enumerate(train_db):  #batch级别的循环 ,每个step循环一个batch
        with tf.GradientTape() as tape:  # with结构记录梯度信息
            y = tf.matmul(x_train, w1) + b1  # 神经网络乘加运算
            y = tf.nn.softmax(y)  # 使输出y符合概率分布(此操作后与独热码同量级,可相减求loss)
            y_ = tf.one_hot(y_train, depth=3)  # 将标签值转换为独热码格式,方便计算loss和accuracy
            loss = tf.reduce_mean(tf.square(y_ - y))  # 采用均方误差损失函数mse = mean(sum(y-out)^2)
            loss_all += loss.numpy()  # 将每个step计算出的loss累加,为后续求loss平均值提供数据,这样计算的loss更准确
        # 计算loss对各个参数的梯度
        grads = tape.gradient(loss, [w1, b1])
        # 实现梯度更新 w1 = w1 - lr * w1_grad    b = b - lr * b_grad
        w1.assign_sub(lr * grads[0])  # 参数w1自更新
        b1.assign_sub(lr * grads[1])  # 参数b自更新
    # 每个epoch,打印loss信息
    print("Epoch {}, loss: {}".format(epoch, loss_all/4))
    train_loss_results.append(loss_all / 4)  # 将4个step的loss求平均记录在此变量中
    loss_all = 0  # loss_all归零,为记录下一个epoch的loss做准备
    # 测试部分
    # total_correct为预测对的样本个数, total_number为测试的总样本数,将这两个变量都初始化为0
    total_correct, total_number = 0, 0
    for x_test, y_test in test_db:
        # 使用更新后的参数进行预测
        y = tf.matmul(x_test, w1) + b1
        y = tf.nn.softmax(y)
        pred = tf.argmax(y, axis=1)  # 返回y中最大值的索引,即预测的分类
        # 将pred转换为y_test的数据类型
        pred = tf.cast(pred, dtype=y_test.dtype)
        # 若分类正确,则correct=1,否则为0,将bool型的结果转换为int型
        correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)
        # 将每个batch的correct数加起来
        correct = tf.reduce_sum(correct)
        # 将所有batch中的correct数加起来
        total_correct += int(correct)
        # total_number为测试的总样本数,也就是x_test的行数,shape[0]返回变量的行数
        total_number += x_test.shape[0]
    # 总的准确率等于total_correct/total_number
    acc = total_correct / total_number
    test_acc.append(acc)
    print("Test_acc:", acc)
    print("--------------------------")
# 绘制 loss 曲线
plt.title('Loss Function Curve')  # 图片标题
plt.xlabel('Epoch')  # x轴变量名称
plt.ylabel('Loss')  # y轴变量名称
plt.plot(train_loss_results, label="$Loss$")  # 逐点画出trian_loss_results值并连线,连线图标是Loss
plt.legend()  # 画出曲线图标
plt.show()  # 画出图像
# 绘制 Accuracy 曲线
plt.title('Acc Curve')  # 图片标题
plt.xlabel('Epoch')  # x轴变量名称
plt.ylabel('Acc')  # y轴变量名称
plt.plot(test_acc, label="$Accuracy$")  # 逐点画出test_acc值并连线,连线图标是Accuracy
plt.legend()
plt.show()

result:

V. Summary

Steps: first divide the data set, then train, test and verify, optimize, and finally visualize

Guess you like

Origin blog.csdn.net/qq_46006468/article/details/119614242