本文是个人的学习笔记,是跟随北大曹健老师的视频课学习的
链接: 人工智能实践:Tensorflow笔记(北京大学 软件与微电子学院 曹健老师)
import tensorflow as tf
if __name__ == '__main__':
print(tf.version.VERSION)
目前的版本号为
2.11.0
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)
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
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)
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
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)
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>)
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)=w2⇒∇Lw=2w⇒w=10.0−η∇Lw∣w=10.0=10.0−0.1∗20.0=8.0
运行结果
tf.Tensor(20.0, shape=(), dtype=float32)
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=8.0>
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
from sklearn import datasets
import pandas as pd
关于sklearn(0.19.0)安装及错误解决,可以参考:sklearn(0.19.0)安装及错误解决(by. 青柚子)
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个
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)
训练+测试时间:10.287723541259766 s
在利用MD编辑公式的时候,很感谢大神总结了LaTeX的相关表达形式 超详细 LaTeX数学公式(by. ViatorSun)