1.tensor的创建
# 从Numpy和list转换
tf.convert_to_tensor(np.ones([2,3]))
tf.convert_to_tensor([1,2])
# tf.zeros
tf.zeros([]) #标量
tf.zeros([1]) # 1维
tf.zeros([2,2]) # 2*2
#tf.zeros_like
tf.zeros_like(a)
# 等价
tf.zeros(a.shape)
# tf.ones
# 和zeros的使用一样
# tf.ones_like
# Fill 自由填充
tf.fill([2,2],0) # 2*2矩阵 , 全为0
# Normal 随机初始化
tf.random.normal([2,2],mean=1,stddev=1) # 从高斯(1,1)分布生成2*2矩阵
tf.random.truncated_normal([2,2],mean=0,sttdev=1) # 截断的正态分布
# 把正态分布两边给截断了,因为sigmoid函数的首尾梯度几乎为0,所以截断后有好处
# 均匀分布
tf.random.uniform([2,2],minval= 0,maxval=1) # 0 到 1 均匀分布
# 随机打散
idx = tf.range(10)
tf.random.shuffle(idx)
#按同样的随机顺序打散a,b
a = tf.gather(a,idx)
b = tf.gather(b,idx)
#tf.constant
tf.constant(1)
tf.constant([1])
tf.constant([2,3])
2.索引和切片
# 切片 start:end 左闭右开
# start默认是0,end默认是-1
a[-1:] # 取走最后一个元素的向量
a[-1] # 返回最后一个元素
a[:2] # 0 1 索引的元素
a[:-1] # 到倒数第二个
a[:] # 选全部
# start:end:step
# step 默认为1
a = [0,1,2,3,4,5,6,7,8,9]
a[:B:2] # 步长为2,0到B
a[A::2] # 步长为2,A到末尾
# 逆序 ::-1
a[::-1] # 逆着选择(倒序)
a[::-2] # 隔着
a[2::-2] # 从2号位置倒着选择
# ... 省略任意长的冒号
a[0,:,:,:,:]
#等价于
a[0,...]
a[...,0]
a[0,...,2]
a[1,0,...,0]
# Selective indexing
#tf.gather 给出来的索引去检索
a[4,35,8] # 班级 学生 科目
tf.gather(a,axis=0,indices=[2,3]).shape # axis=0:选择班级 ,indices=[2,3] 选择2,3个班级
TensorShape([2,35,8])
tf.gather(a,axis=0,indices=[2,1,3,0]).shape
tf.gather(a,axis=1,indices=[2,3,7,9,16]).shape #axis=1选择学生,第2,3,7,9,16个
TensorShape([4,5,8])
#tf.gather_nd 在多个维度的指定
tf.gather_nd(a,[0]) .shape# 返回第0号班级 a[0]
TensorShape([35,8])
tf.gather_nd(a,[0,1]).shape # 0号班级的一号学生 a[0,1]
TensorShape([8])
tf.gather_nd(a,[0,1,2]).shape # 0班级1号学生2门课
TensorShape([])
tf.gather_nd(a,[[0,1,2]]).shape # 0班级1号学生2门课 但是夹了个[] [a[0,1,2]]
TensorShape([1])
tf.gather_nd(a,[[0,0],[1,1]]).shape # 取0号班级0号学生+1号班级1号学生 [2,8]
TensorShape([2,8])
tf.gather_nd(a,[[0,0],[1,1],[2,2]]).shape # [3,8]
tf.gather_nd(a,[[0,0,0],[1,1,1],[2,2,2]]).shape #[3]
tf.gather_nd(a,[[[0,0,0],[1,1,1],[2,2,2]]]).shape # shape[1,3]-->[[98,99,100]]
#tf.boolean_mask 掩码操作
a[4,28,28,3]
tf.boolean_mask(a,mask=[True,True,False],axis=3) #[4,28,28,2]
3.维度变换
shape,ndim
reshape
expand_dims/squeeze
a[4,28,28,3]
tf.reshape(a,[4,784,3])
# 转置
# tf.transpose
tf.transpose(a)
a[4,3,2,1]
tf.transpose(a,perm=[0,1,3,2]) #[放原来的0维,1维,3维,2维]
[4,3,1,2]
#expand dim 增加维度
tf.expand_dims(a,axis=) # axis=1,代表在1位置增加一个轴
#squeeze 减少维度 只能去掉1维的
tf.squeeze(a,axis=) # axis是指定的维度
4.合并与分割
tf.concat
tf.split
tf.stack
a = tf.ones([4,35,8])
b = tf.ones([2,35,8])
c = tf.concat([a,b],axis=0) # axis=0,在第一维上合并
[6,35,8]
a = tf.ones([4,32,8])
b = tf.ones([4,3,8])
c = tf.concat([a,b],axis=1) # 合并第二维
[4,35,8]
# concat 需要拼接的维度可以不等,但是其他的维度要相等
a[4,35,8]
b[4,35,8]
tf.stack([a,b],axis=0) # 在0轴前创建一个新维度
[2,4,35,8]
# 要求所有的维度相等
# unstack
c[2,4,35,8]
aa,bb = tf.unstack(c,axis=0)
[4,35,8][4,35,8]
res = tf.unstack(c,axis=3) # res返回的是8个数组,即把第三维拆分了8个
#split
c[2,4,35,8]
res = tf.split(c,axis=3,num_or_size_splits=2) # [4,35,4][4,35,4]
res = tf.split(c,axis=3,num_or_size_splits=[2,2,4])
[2,4,35,2] [2,4,35,2] [2,4,35,4]
5.统计
norm
reduce_min/max
argmax/argmin
tf.equal
unique
tf.norm(a) # 二范式 默认
tf.norm(a,ord = 1) # 一范式
tf.norm(a,axis =1 ) # 在一个[] 里进行二范式求解
# [1 1] [ 根号2]
# [1 1] = [ 根号2]
#reduce_min/max/mean
#求最大最小均值,因为会有一个降维的过程,所以加了个reduce
tf.reduce_min(a)
tf.reduce_max(a)
tf.reduce_mean(a)
#也可以加axis指定维度
tf.reduce_min(a,axis =1 )
#argmax/argmin
tf.argmax(a) #返回最大值所在的位置
#equal 每个元素进行比较
tf.equal(a,b)
[True,True,False]
#求准确率
a
pre = tf.cast(tf.argmax(a,axis=1)) #cast是把True、False转换为1,0
y
tf.equal(y,pre)
correct = tf.reduce_sum(tf.cast(tf.equal(y,pre)))
acc = correct / num
#unique
#去重
#下图所示,除了返回去重后的表
#还返回了idx
#idx对应的是原表的数据在Unique里面的索引
#可以用gather还原
tf.gather(Unique,idx)
6.排序
Sort / argsort
Topk
a = tf.random.shuffle(tf.range(5)) # 随机打乱
tf.sort(a,direction='DESCENDING') #降序排列
tf.argsort(a,direction='DESCENDING')
#返回的是排序后的索引
#例如降序,返回的是[最大值的索引,次大值的索引....]
# ASCENDING 升序 , 默认
#top_k
res = tf.math.top_k(a,2)
'''
a = ([ 4,6,8],
[9,4,7],
[4,5,1])
'''
res.indices
#返回的是索引 (前2大的)
'''
[2,1]
[0,2]
[1,0]
'''
res.values #返回值
'''
[8,6]
[9,7]
[5,4]
'''
top-k accuracy
只要有一个是猜对了,就算对了
主要用来反应模型的好坏
考虑top-1
有2个样本的预测结果
b0:[0.1,0.2,0.7]
b1:[0.2,0.7,0.1]
正确label[2,0]
对他进行top_k(p,3).indices
返回索引
[2 1 0]
[1 0 2]
转置一下
b0 b1
[2 1]
[1 0]
[0 2]
如果是top1
比较[2 1] 和[2 0]
准确率为 1 / 2 =0.5
top2
比较
b0 b1
[2 1]
[1 0] 和
[2 0]
[2 0]
得到 (1+1) /2 =1
因为只要有一个对就算预测对了这个样本
以此类推
top3
100%
7.填充与复制
填充:
pad
复制:
tile
broadcast_to
'''
a= [0,1,2]
[3,4,5]
[6,7,8]
'''
tf.pad(a,[[1,0],[0,0]])
#[1,0] 表示填充行,在最上面填充1行,最下面填充0行
#[0,0] 表示填充列,在最左边填充0列,最右边填充0列
[0 0 0]
[0 1 2]
[3 4 5]
[6 7 8]
8.限幅
clip_by_value
relu
clip_by_norm
gradient clipping
#clip_by_value
tf.maximum(a,0)
tf.minimum(a,0)
#这两个函数每次只能对一边进行限幅
tf.clip_by_value(a,0,9) # 0 到 9
#relu
tf.nn.relu(a)
tf.maximum(a,0)
#clip_by_norm 等比放缩(不改变方向,只改变大小)
tf.clip_by_norm(a,15)
#clip_by_global_norm 整体的方向不变,缩放
#主要是为了防止梯度爆炸或者梯度归零
new_grads,total_norm = tf.clip_by_global_norm(grads,25)
9.高阶操作
where
scatter_nd
meshgrid
#where 拿坐标
mask = a>0
tf.boolean_mask(a,mask) #取值
ind = tf.where(mask) #取坐标
tf.gather_nd(a,ind) #取值
c = tf.where(mask,a,b)
#这个函数的意思是,mask中的true部分是取a的,false部分取b
'''
a= [1,1]
[1,1]
b = [2,2]
[2,2]
mask = [True,True]
[False,False]
c = [1,1]
[2,2]
'''
scatter_nd 用来更新
#meshgrid
y = tf.linspace(-2,2,5) # -2到2,生成5个数
x = tf.linspace(-2,2,5)
points_x,points_y = tf.meshgrid(x,y)
对应组合
可以用来画出等高线等
10.数据加载
keras.datasets
tf.data.Dataset.from_tensor_slices
shuffle
map
batch
repeat
数据集
加载MNIST
CIFAR10/100
10:10类大类
100:每类再分成10小类
shuffle 打散
map
batch
repeat 控制迭代次数
流程
共有条评论 网友评论