人工智能学习第一篇(tensorflow笔记)

本文是在学习北大课程“人工智能实践:tensorflow笔记”的基础上,自己做的笔记,用于温故知新。

张量(Tensor):多维数组(列表)
阶:张量的维数(从0开始)
张量可以表示0阶到n阶数组(列表)
eg1:

import tensorflow as tf

a=tf.constant([1,5],dtype=tf.int64)
print(a)
print(a.dtype)
print(a.shape)

结果:

tf.Tensor([1 5], shape=(2,), dtype=int64)
#张量的形状看shape有几个逗号,逗号隔开了几个数字,这个张量就是几维的
#本例中,‘,’隔开了一个数字,即一维张量,2表示张量中有两个元素(即1和5)
<dtype: 'int64'>
(2,)

eg2:

#tf.convert_to_tensor(数据名,dtype=数据类型(可选))
#因为很多时候数据类型是numpy格式
import tensorflow as tf
import numpy as np
a=np.arange(0,5)
b=tf.convert_to_tensor(a,dtype=tf.int64)
print(a)
print(b)

结果:

[0 1 2 3 4]
tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)

eg3:

import tensorflow as tf
a=tf.zeros([2,3])#创建全为0的张量 tf.zeros(维度)
b=tf.ones(4)#创建全为1的张量 tf.ones(维度)
c=tf.fill([2,2],9)#创建全为指定值的张量 tf.fill(维度,指定值)
print(a)
print(b)
print(c)

结果:

tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]], shape=(2, 3), dtype=float32)
tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32)
tf.Tensor(
[[9 9]
 [9 9]], shape=(2, 2), dtype=int32)

维度:
一维:直接写个数
二维:用[行,列]
多维:[n,m,j,k,…](n,m,j,k,表示每个维有几个元素)

生成随机数

  • 生成正态分布的随机数,默认均值为0,标准差为1
    tf.random.normal(维度,mean=均值,stddec=标准差)
  • 生成截断式正态分布的随机数
    tf.random.truncated_normal(维度,mean=均值,stddev=标准差)
  • 生成均匀分布随机数
    tf.random.uniform(维度,minval=最小值,maxval=最大值)
    最小最大为前闭后开区间

常用函数

  • 强制tensor转换为该数据类型
    tf.cast(张量名,dtype=数据类型)
  • 计算张量维度上元素的最小值
    tf.reduce_min(张量名)
  • 计算张量维度上元素的最大值
    tf.reduce_max(张量名)

eg:

import tensorflow as tf

x1=tf.constant([1,2,3],dtype=tf.float64)
print(x1)
x2=tf.cast(x1,tf.int32)
print(x2)
print(tf.reduce_min(x2),tf.reduce_max(x2))

结果:

tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32)

axis函数
在一个二维张量或数组中,可以通过调整axis等于0或1控制执行维度。
axis=0表示跨行(经度,竖着),axis=1表示跨列(纬度,横着)
如果不指定axis,则所有元素参与计算
eg:

import tensorflow as tf

x=tf.constant([[1,2,3],[1,2,3]])
print(x)
print(tf.reduce_mean(x))
print(tf.reduce_sum(x,axis=0))
print(tf.reduce_sum(x,axis=1))

结果:

tf.Tensor(
[[1 2 3]
 [1 2 3]], shape=(2, 3), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([6 6], shape=(2,), dtype=int32)

tf.Variable(初始值):将变量标记为“可训练”,被标记的变量会在反向传播中记录梯度信息。神经网络训练中,常用该函数标记待训练参数。
w=tf.Variable(tf.random.normal([2,2],mean=0,stddev=1))

TensorFlow中的数学运算

  • 对应元素的四则运算:tf.add(张量1,张量2),tf.subtract(张量1,张量2),tf.multiply(张量1,张量2),tf.divide(张量1,张量2)
    只有相同维度的张量才能进行四则运算
  • 平方、次方与开方:tf.square(张量名),tf.pow(张量名,n次方数),tf.sqrt(张量名)
  • 矩阵乘:tf.matmul
    eg1:
a=tf.ones([1,3])
b=tf.fill([1,3],3.)
print(a)
print(b)
print(tf.add(a,b))
print(tf.subtract(a,b))
print(tf.multiply(a,b))
print(tf.divide(b,a))

结果:

tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
tf.Tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32)
tf.Tensor([[-2. -2. -2.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)

(tf.ones()生成的张量内部是float型,我第一遍敲时,b里的3没加‘.’,是int型,所以在add时报错了…不能直接加!byw,python真是一行行执行代码,哪里不对才会中止运行,前面对的就直接跑)

eg2:

a=tf.fill([1,3],2.)
print(a)
print(tf.pow(a,3))#n次方
print(tf.square(a))#平方
print(tf.sqrt(a))#开方

结果:

tf.Tensor([[2. 2. 2.]], shape=(1, 3), dtype=float32)
tf.Tensor([[8. 8. 8.]], shape=(1, 3), dtype=float32)
tf.Tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32)
tf.Tensor([[1.4142135 1.4142135 1.4142135]], shape=(1, 3), dtype=float32)

(再一次,2后面没加点,报错如下

NotFoundError: Could not find valid device for node.
Node:{{node Sqrt}}
All kernels registered for op Sqrt :
  device='CPU'; T in [DT_FLOAT]
  device='CPU'; T in [DT_HALF]
  device='CPU'; T in [DT_DOUBLE]
  device='CPU'; T in [DT_BFLOAT16]
  device='CPU'; T in [DT_COMPLEX64]
  device='CPU'; T in [DT_COMPLEX128]
 [Op:Sqrt]

eg3:

a=tf.ones([2,3])
b=tf.fill([3,2],3.)
print(tf.matmul(a,b))

结果:

tf.Tensor(
[[9. 9.]
 [9. 9.]], shape=(2, 2), dtype=float32)

tf.data.Dataset.from_tensor_slices():切分传入张量的第一维度,生成输入特征/标签对,构建数据集
data=tf.data.Dataset.from_tensor_slices((输入特征,标签))
(Numpy,Tensor格式都可用该语句读入数据)
eg:

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 element in dataset:
    print(element)

结果:

<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
(<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>)

tf.GradientTape():with结构记录计算过程,gradient求出张量的梯度。
with tf.GradientTape() as tape:
若干个计算过程
grad=tape.gradient(函数,对谁求导)
eg:

with tf.GradientTape() as tape:
    w=tf.Variable(tf.constant(3.0))
    loss=tf.pow(w,2)
grad=tape.gradient(loss,w)
print(grad)

输出:

tf.Tensor(6.0, shape=(), dtype=float32)

enumerate(列表名):是python的内建函数,他可遍历每个元素(如列表,元组或字符串),组合为:索引 元素,常在for循环中使用。
eg:

seq=['one','two','three']
for i,element in enumerate(seq):
    print(i,element)

输出:

0 one
1 two
2 three

tf.one_hot(待转换数据,depth=分几类):将待转换数据转换为one-hot形式的数据输出
独热编码(one-hot encoding):在分类问题中,常用独热码做标签,标记类别:1表示是,0表示非。
eg:

classes=3
labels=tf.constant([1,0,2])
output=tf.one_hot(labels,depth=classes)
print(output)

输出:

tf.Tensor(
[[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]], shape=(3, 3), dtype=float32)

若labels=tf.constant([0,1,2]),则输出为:

tf.Tensor(
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]], shape=(3, 3), dtype=float32)

tf.nn.softmax():当n分类的n个输出(y0,y1,…,yn-1)通过softmax函数,便符合概率分布了。(每个输出的概率在0~1且所有输出概率合为1)
eg:

y=tf.constant([1.01,2.01,-0.66])
y_pro=tf.nn.softmax(y)
print('After softmax,y_pro is :',y_pro)

输出:

After softmax,y_pro is : tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32)

assign_sub(参数要自减的内容):赋值操作,更新参数的值并返回。
调用assign_sub()前,先用tf.Variable()定义变量w为可训练的(可自更新)
eg:

w=tf.Variable(4)
w.assign_sub(1)#w-=1
print(w)

输出:

<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

tf.argmax(张量名,axis=操作轴):返回张量沿指定维度最大值的索引。(同理还有tf.argmin()
eg:

test=np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
print(test)
print(tf.argmax(test,axis=0))#注意,返回的是索引号(即数组下标)而不是值
print(tf.argmax(test,axis=1))

输出:

[[1 2 3]
 [2 3 4]
 [3 4 5]
 [4 5 6]]
tf.Tensor([3 3 3], shape=(3,), dtype=int64)
tf.Tensor([2 2 2 2], shape=(4,), dtype=int64)

鸢尾花数据显示:

from sklearn import datasets
from pandas import DataFrame
import pandas as pd

x_data=datasets.load_iris().data#.data返回iris数据集所有输入特征
y_data=datasets.load_iris().target#.target返回iris数据集所有标签
print('x_data from datasets: \n',x_data)
print('y_data from datasets: \n',y_data) 

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

x_data['类别']=y_data#新加一列,列标签为‘类别’,数据为y_data
print('x_data add a column: \n',x_data)

搭建一个简单的神经网络(鸢尾花)

# -*- coding: utf-8 -*-
#利用鸢尾花数据集,实现前向传播、反向传播,可视化loss曲线

#导入所需模块
import tensorflow as tf
from sklearn import datasets
from matplotlib import pyplot as plt
import numpy as np

#导入数据,分别为输入特征和标签
x_data=datasets.load_iris().data
y_data=datasets.load_iris().target

#随机打乱数据(因为原始数据是顺序的,不打乱会影响准确率)
#seed,随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
np.random.seed(116)
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记录4个step生成的4个loss的和

#训练部分
for epoch in range(epoch):#数据集级别迭代
    for step,(x_train,y_train) in enumerate(train_db):#batch级别迭代,x_train,y_train此前都以batch的形式存入train_db中了,所以这时候可以当新的干净的变量使用
        with tf.GradientTape() as tape:#记录梯度信息
            y=tf.matmul(x_train,w1)+b1#前向传播过程计算y
            y=tf.nn.softmax(y)#符合概率分布
            y_=tf.one_hot(y_train,depth=3)#y_为标准答案,即标签
            loss=tf.reduce_mean(tf.square(y_-y))#损失函数计算预测值和标准值之间的差距
            loss_all+=loss.numpy()
        #计算loss对各个参数的梯度
        grads=tape.gradient(loss,[w1,b1])
        
        #实现梯度更新   
        w1.assign_sub(lr*grads[0])#实现w1自更新,w1=w1-lr*w1_grad
        b1.assign_sub(lr*grads[1])#实现lr自更新,b=b-lr*b_grad
        
    #每个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
    #计算当前参数前向传播后的准确率,显示当前acc
    for x_test,y_test in test_db:
        #使用更新后的参数进行预测
        y=tf.matmul(x_test,w1)+b1#y为预测结果
        y=tf.nn.softmax(y)#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]
    #总的准确率
    acc=total_correct/total_number
    test_acc.append(acc)
    print('Test_acc:',acc)
    print('-------------------------')
    
#绘制loss曲线
plt.title('Loss Function Curve')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.plot(train_loss_results,label='$Loss$')
plt.legend()#画出曲线图标
plt.show()#画出图像

#绘制acc曲线
plt.title('Acc Curve')
plt.xlabel('Epoch')
plt.ylabel('Acc')
plt.plot(test_acc,label='$Accuracy$')
plt.legend()#画出曲线图标
plt.show()#画出图像   
    
    



结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值