Tensorflow学习笔记(一):神经网络计算

这篇博客介绍了如何使用TensorFlow进行张量生成,包括常量、转化、填充和随机张量的创建。此外,还讲解了TensorFlow中的常用函数,如格式转换、统计函数、运算以及数据处理。博主通过实例展示了数据配对、求梯度和更新操作。文章进一步探讨了softmax多类分类的相关函数,并导入鸢尾花数据集进行了简单的神经网络搭建和训练。
摘要由CSDN通过智能技术生成

本文是个人的学习笔记,是跟随北大曹健老师的视频课学习的
附:bilibili课程链接MOOC课程链接 以及 源码下载链接 (提取码:mocm)

一、导入Tensorflow库

import tensorflow as tf

if __name__ == '__main__':
    print(tf.version.VERSION)

目前的版本号为

2.11.0

二、张量生成

1. 直接生成:constant

def tensorGenerate():
    t0 = tf.constant(1, dtype=tf.float32)
    t1 = tf.constant([2, 4])
    t2 = tf.constant([[1, 2], [3, 4]], dtype=tf.int32)
    t3 = tf.constant([[[1, 2], [2, 3], [4, 5]]])
    print(t0) # scalar
    print(t1) # vector
    print(t2) # matrix
    print(t3) # tensor(n=3)

运行结果

# shape 可以理解为每个中括号里面嵌套了几个子元素
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor([2 4], shape=(2,), dtype=int32)
tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[[1 2]
  [2 3]
  [4 5]]], shape=(1, 3, 2), dtype=int32)

2. 转化(convert_to_tensor)、填充(zeros、ones、fill)

def tensorGenerate():
    t0 = tf.zeros(4)
    a = np.array([2, 4])
    t1 = tf.convert_to_tensor(a, dtype=tf.int64)
    t2 = tf.ones([2, 3])
    t3 = tf.fill([3, 2, 1], 9.0)

运行结果

tf.Tensor([0. 0. 0. 0.], shape=(4,), dtype=float32) # t0
tf.Tensor([2 4], shape=(2,), dtype=int64) # t1
tf.Tensor(
[[1. 1. 1.]
 [1. 1. 1.]], shape=(2, 3), dtype=float32) # t2
tf.Tensor(
[[[9.]
  [9.]]

 [[9.]
  [9.]]

 [[9.]
  [9.]]], shape=(3, 2, 1), dtype=float32) # t3

3. 随机(random.normal、random.truncated_normal、random.uniform)

def tensorGenerate():
    t0 = tf.random.normal([2, 2], mean=0.5, stddev=1)
    t1 = tf.random.truncated_normal([2, 2], mean=0.5, stddev=1)
    t2 = tf.random.uniform([2, 2], minval=0, maxval=1)

运行结果(截断后数据全部位于 μ ± 2 σ \mu\pm2\sigma μ±2σ内,更集中)

# 正态分布
tf.Tensor(
[[-1.8150437   0.50705945]
 [-0.4460429   0.05013335]], shape=(2, 2), dtype=float32)
# 正态分布+截断
tf.Tensor(
[[ 0.6039666  -0.30695528]
 [ 0.09469539  1.6119573 ]], shape=(2, 2), dtype=float32)
# 均匀分布
tf.Tensor(
[[0.6138203  0.5804186 ]
 [0.6753099  0.84330475]], shape=(2, 2), dtype=float32)

三、常用函数

1. 格式转换(cast),统计(max、min、mean、sum)

def commonFunction():
    t0 = tf.random.normal([2, 3], mean=5, stddev=1)
    print(tf.argmax(t0, axis=0), tf.argmin(t0, axis=1))
    t1 = tf.cast(t0, tf.int32)
    t2 = tf.reduce_max(t0, axis=0)
    t3 = tf.reduce_min(t0)
    t4 = tf.reduce_mean(t0, axis=1)
    t5 = tf.reduce_sum(t0)

运行结果

# axis=x 可以理解为对第x个中括号进行操作(维度)
tf.Tensor(
[[4.8231106 6.1618514 5.448559 ]
 [6.0605617 4.4781694 4.707378 ]], shape=(2, 3), dtype=float32) # origin
tf.Tensor([1 0 0], shape=(3,), dtype=int64) # argmax
tf.Tensor([0 1], shape=(2,), dtype=int64) #argmin
tf.Tensor(
[[4 6 5]
 [6 4 4]], shape=(2, 3), dtype=int32) # cast
tf.Tensor([6.0605617 6.1618514 5.448559 ], shape=(3,), dtype=float32) # max
tf.Tensor(4.4781694, shape=(), dtype=float32) # min
tf.Tensor([5.4778404 5.0820365], shape=(2,), dtype=float32) # mean
tf.Tensor(31.67963, shape=(), dtype=float32) # sum

2. 运算(加减乘除幂)

def commonFunction():
    a = tf.random.normal([1, 3])
    b = tf.ones([1, 3])
    print("a={}, b={}".format(a, b))
    print('a+b=', tf.add(a, b))
    print('a-b=', tf.subtract(a, b))
    print('ab=', tf.multiply(a, b)) # 对应元素乘法
    print('a*b=', tf.matmul(a, tf.transpose(b))) # 矩阵乘法
    print('a/b=', tf.divide(a, b))
    print('a^2=', tf.square(a))
    print('a^10=', tf.pow(a, 10))
    print('sqrt(a)=', tf.sqrt(a))

运行结果


a=[[1.9298642  0.82699674 2.0935786 ]], b=[[1. 1. 1.]]
a+b= tf.Tensor([[2.9298642 1.8269968 3.0935786]], shape=(1, 3), dtype=float32)
a-b= tf.Tensor([[ 0.92986417 -0.17300326  1.0935786 ]], shape=(1, 3), dtype=float32)
ab= tf.Tensor([[1.9298642  0.82699674 2.0935786 ]], shape=(1, 3), dtype=float32)
a*b= tf.Tensor([[3.7303138]], shape=(1, 1), dtype=float32)
a/b= tf.Tensor([[1.9298642  0.82699674 2.0935786 ]], shape=(1, 3), dtype=float32)
a^2= tf.Tensor([[3.7243757 0.6839236 4.3830714]], shape=(1, 3), dtype=float32)
a^10= tf.Tensor([[7.1658453e+02 1.4963666e-01 1.6176802e+03]], shape=(1, 3), dtype=float32)
sqrt(a)= tf.Tensor([[1.3891956 0.9093936 1.4469204]], shape=(1, 3), dtype=float32)

3. 数据配对(data.Dataset.from_tensor_slices)

def commonFunction():
    features = tf.constant([12, 23, 10, 17])
    labels = tf.constant([0, 1, 1, 0])
    dataset = tf.data.Dataset.from_tensor_slices((features, labels))
    print(dataset)
    for data in dataset:
        print(data)

运行结果

# dataset
<TensorSliceDataset element_spec=(TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None))>
# data
(<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>)

4. 求梯度、更新(assign_sub/add)

def commonFunction():
    lr = 0.1
    with tf.GradientTape() as tape:
        w = tf.Variable(tf.constant(10.0))  # initialize
        loss = tf.square(w)  # define loss function
    grad = tape.gradient(loss, w)
    w.assign_sub(lr*grad)

L ( w ) = w 2 ⇒ ∇ L w = 2 w ⇒ w = 10.0 − η ∇ L w ∣ w = 10.0 = 10.0 − 0.1 ∗ 20.0 = 8.0 L(w) = w^2 \Rightarrow \nabla L_w=2w \Rightarrow w=10.0-\eta\nabla L_w|_{w=10.0}=10.0-0.1*20.0=8.0 L(w)=w2Lw=2ww=10.0ηLww=10.0=10.00.120.0=8.0
运行结果

tf.Tensor(20.0, shape=(), dtype=float32)
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=8.0>

5. softmax多类分类相关(one-hot、softmax)

def commonFunction():
    classes = 3
    y = tf.constant([1.01, 2.01, -0.66])
    ypro = tf.nn.softmax(y)
    labels= tf.constant([1, 0, 2, 0])
    output = tf.one_hot(labels, depth=classes)

运行结果

tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32)  # softmax
tf.Tensor(
[[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]
 [1. 0. 0.]], shape=(4, 3), dtype=float32)  # onehot

四、导入数据

1. 导入sklearn、pandas库

from sklearn import datasets
import pandas as pd

关于sklearn(0.19.0)安装及错误解决,可以参考:sklearn(0.19.0)安装及错误解决(by. 青柚子)

2. 导入鸢尾花数据集并显示

def loadData():
    dataset = datasets.load_iris()
    x = dataset.data
    y = dataset.target

    x = pd.DataFrame(x, columns=['花萼长度', '花萼宽度', '花瓣长度', '花瓣宽度'])
    pd.set_option('display.unicode.east_asian_width', True) # 列对齐
    print("data add index: \n", x)

    x['类别'] = y
    print("data add a column: \n", x)

输出结果为

data add index: 
      花萼长度  花萼宽度  花瓣长度  花瓣宽度
0         5.1       3.5       1.4       0.2
1         4.9       3.0       1.4       0.2
2         4.7       3.2       1.3       0.2
3         4.6       3.1       1.5       0.2
4         5.0       3.6       1.4       0.2
..        ...       ...       ...       ...
145       6.7       3.0       5.2       2.3
146       6.3       2.5       5.0       1.9
147       6.5       3.0       5.2       2.0
148       6.2       3.4       5.4       2.3
149       5.9       3.0       5.1       1.8

[150 rows x 4 columns]
data add a column: 
      花萼长度  花萼宽度  花瓣长度  花瓣宽度  类别
0         5.1       3.5       1.4       0.2     0
1         4.9       3.0       1.4       0.2     0
2         4.7       3.2       1.3       0.2     0
3         4.6       3.1       1.5       0.2     0
4         5.0       3.6       1.4       0.2     0
..        ...       ...       ...       ...   ...
145       6.7       3.0       5.2       2.3     2
146       6.3       2.5       5.0       1.9     2
147       6.5       3.0       5.2       2.0     2
148       6.2       3.4       5.4       2.3     2
149       5.9       3.0       5.1       1.8     2

[150 rows x 5 columns]

由此可见,此数据集的个数为150个

五、神经网络搭建与训练(一层)

1. 源码

import numpy as np
import tensorflow as tf
from sklearn import datasets
import matplotlib.pyplot as plt
import time as t


def loadData(size):
    # 数据集读入
    dataset = datasets.load_iris()
    x = dataset.data
    y = dataset.target

    # 数据集乱序(seed相同即可)
    np.random.seed(27)
    np.random.shuffle(x)
    np.random.seed(27)
    np.random.shuffle(y)
    # tf.random.set_seed(127)

    # 数据集分为训练集和测试集(8:2)
    x_train = x[:120]
    y_train = y[:120]
    x_test = x[120:]
    y_test = y[120:]

    # 规范数据为浮点数
    x_train = tf.cast(x_train, tf.float32)
    x_test = tf.cast(x_test, tf.float32)

    # 配对并封装成batch
    data_train = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(size)
    data_test = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(size)
    return data_train, data_test


def train(batch_size, epoches, lr):
    # 载入数据
    data_train, data_test = loadData(batch_size)
    # 初始化参数空间,注意标准差stddev要小一点,否则训练时会有问题
    w1 = tf.Variable(tf.random.truncated_normal([4, 3], stddev=0.1))
    b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1))

    train_loss = []  # 记录训练数据的loss
    test_acc = []  # 记录测试数据的accuracy
    t1 = t.time()
    for epoch in range(epoches):  # 每一次epoch遍历一次数据集
        Loss = 0
        for step, (x_train, y_train) in enumerate(data_train):  # 每一个step遍历一个batch
            with tf.GradientTape() as tape:
                s1 = tf.matmul(x_train, w1) + b1  # 正向传播
                y = tf.nn.softmax(s1)  # 输出(softmax多类分类)
                y0 = tf.one_hot(y_train, depth=3)  # 独热编码处理
                loss = tf.reduce_mean(tf.square(y - y0))  # 计算loss
                Loss += loss
            grads = tape.gradient(loss, [w1, b1])  # 计算梯度
            w1.assign_sub(lr * grads[0])  # 参数空间更新
            b1.assign_sub(lr * grads[1])

        train_loss.append(Loss / 4)  # 每一次epoch有四个step,算平均loss记录

        total_correct, total_number = 0, 0
        for x_test, y_test in data_test:
            s1 = tf.matmul(x_test, w1) + b1
            y = tf.nn.softmax(s1)
            type = tf.argmax(y, axis=1)  # 预测结果(最大概率类)
            type = tf.cast(type, dtype=y_test.dtype)
            correct = tf.cast(tf.equal(type, y_test), dtype=tf.int32)  # 判断预测和标签是否相等
            correct = tf.reduce_sum(correct)
            total_correct += int(correct)
            total_number += x_test.shape[0]

        acc = total_correct / total_number
        test_acc.append(acc)

    t2 = t.time()
    print('训练+测试时间:{}s'.format(t2-t1))
    # loss曲线作图(训练集)
    plt.figure()
    plt.subplot(121)
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.plot(train_loss, label='$Loss$')
    plt.legend()
    plt.title('Loss Function Curve')

    # accuracy曲线作图(测试集)
    plt.subplot(122)
    plt.xlabel('Epoch')
    plt.ylabel('Accuracy')
    plt.plot(test_acc, label='$Accuracy$')
    plt.legend()
    plt.title('Acc Curve')
    plt.show()


if __name__ == '__main__':
    train(batch_size=32, epoches=500, lr=0.1)

2. 训练结果

训练+测试时间:10.287723541259766 s

Loss&Acc

在利用MD编辑公式的时候,很感谢大神总结了LaTeX的相关表达形式 超详细 LaTeX数学公式(by. ViatorSun)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代表最低水平的hhhuster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值