TensorFlow语法(3)

数据的合并与分割

1.tf.constant( )合并
将两组数据合并,axis指定合并的维度。除了要合并的维度不一样,其他维度要相等。
例:[class1-4,students,scores]
[class5-6,students,scores]
六个班级的学生成绩分两次录入,将所有数据合并在一起。

a = tf.ones([4,35,8])
b = tf.ones([2,35,8])
c = tf.concat([a,b],axis = 0)
c.shape
>>TensorShape([6,35,8])

2.tf.stack( )合并
增加一个维度,axis指定增加维度的索引位置,要求所有的维度相等。
例:shcool1:[classes,students,scores]
school2:[classes,students,scores]
将两个学校的学生成绩合并在一起。

a = tf.ones([4,35,8])
b = tf.ones([4,35,8])
c = tf.concat([a,b],axis = 0)
c.shape
>>TensorShape([24,35,8])

3.tf.unstack( )切割
axis指定拆分维度的索引位置,维度数位n,就会拆分成n个新的tensor,固定的分割为长度为1的操作。

c = tf.ones([24,35,8])
a,b = f.unstack(c, axis = 0)
a.shape,b.shape
>>TensorShape([4,35,8]),TensorShape([4,35,8])
res = tf.unstack(c,axis= 3)
res[0].shape,res[1].shape
>>TensorShape([2,4,35]),TensorShape([2,4,35])
len(res)
>>8

4.tf.split( )分割
tf.split( )分割更为灵活。axis指定拆分维度的索引位置, num_or_size_splits指定拆分数量。
例:num_or_size_splits=[2,4,2]就是将8拆分成三个tensor,axis=3对应的分别为,2,4,2。

c = tf.ones([24,35,8])
res = tf.split(c, axis= 3, num_or_size_splits = 2)
res[0].shape,res[1].shape
>>TensorShape([2,4,35,4]),TensorShape([2,4,35,4])
len(res)
>>2
res = tf.split(c, axis= 3, num_or_size_splits = [2,4,2])
res[0].shape,res[1].shape,res[2].shape
>>TensorShape([2,4,35,2]),TensorShape([2,4,35,4]),TensorShape([2,4,35,2])

数据统计

1.tf.norm( )
向量的范数,分别为二阶范数,最大范数和一阶范数。
tf.
在这里插入图片描述
二阶范数

a = tf.ones([22])
b = tf.norm(a)
>><tf.Tensor:id=192,shape=(),dtype=float32,numpy=2.0>
c = tf.sqrt(tf.reduce_sum(tf.square(a)))
>><tf.Tensor:id=192,shape=(),dtype=float32,numpy=2.0>

norm(a)=(1)
L1.norm
ord指定使用哪种范数式,ord=1(一阶范数),ord=2(二阶范数);axis指定维度数。

a = tf.ones([2,2])
b = tf.norm(a, ord = 2, axis = 1)
>><tf.Tensor:id=271,shape=(2,),dtype=float32,numpy=array([1,4142135,1,4142135]),dtype=float32>
c = tf.norm(a, ord = 1)
>><tf.Tensor:id=255,shape=( ),dtype=float32,numpy=4.0>
d = tf.norm(a, ord = 1, axis = 0)
>><tf.Tensor:id=260,shape=(2,),dtype=float32,numpy=array([2.,2.]),dtype=float32>

2.tf.reduce_min/max
求取最大值,最小值,均值,不指定参数会求取全局的最大值,最小值和均值。
例:a=[4,10],axis=1,表示求每一行得到最小值,返回4个值。

a = tf.random.normal([4, 10])
tf.reduce_min(a),tf.reduce_max(a).tf.reduce_mean(a)
>><tf.Tensor:id=283,shape=(),dtype=float32,numpy=1.1872448>,<tf.Tensor:id=285,shape=(),dtype=float32,numpy=2.1353827>,<tf.Tensor:id=287,shape=(),dtype=float32,numpy=0.3523524>
tf.reduce_min(a,axis=1)
>><tf.Tensor:id=292,shape=(),dtype=float32,numpy=array([-0.3937837,-1.1872448,-1.079889,-1.1366792]),dtype=float32>

3.tf.argmax/argmin
求最大值和最小值的位置,axis指定维度,没有写axis默认为0。
例:a[4,10],axis=0,则表示4行10列,在0维度上求取每一列的最大值的位置索引。dtype=int32表示的式index的数据类型。

a.shape
>>TensorShape([4,10])
tf.argmax(a,axis=0).shape
>>TensorShape([10])
tf.argmax(a,axis=0)
>><tf.Tensor:id=305,shape=(10,),dtype=int64,numpy=array([0,0,2,3,1,3,0,1,2,0])>

4.tf.equal
比较相同位置的值是否相等,相等返回True,否则返回False。
例如:a[1,2,3,2,5], b[0,1,2,3,4]
全部不等,返回5个False。将返回的bool转换成int,然后累加得到的数值就是值相等的个数。tf.cast(res, dtype=tf.int32)表示转换数据类型。

a = tf.constant([1,2,3,2,5])
b = tf,range(5)
tf.equal(a, b)
>><tf.Tensor:id=170,shape=(5,),dtype=bool,numpy=array([False,False,False,False,False])>
res = tf.equal(a, b)
tf.rerduce_sum(tf.cast(res, dtype = tf,int32))
>><tf.Tensor:id=175,shape=(),dtype=int32,numpy=0>

5.tf,unique
去除重复元素
例:a[4,2,2,4,3]去除重复元素后返回y[4,2,3],同时显示y元素的索引号在a中对应的位置。
使用tf.gather( )可以将去除后的元素还原。

a = tf.constant([4,2,2,4,3])
tf.equal(a, b)
>>Unique<y=tf.Tensor:id=356,shape=(3,),dtype=int32,numpy=array([4,2,3]),dtype=int32>,idx=<tf.Tensor:id=357,shape=(5,),dtype=int32,numpy=array([0,1,1,0,2]),dtype=int32>
tf.gather(equal,idx)
>><tf.Tensor:id=368,shape=(5,),dtype=int32,numpy=array([4,2,2,4,3]),dtype=int32>

排序

1.sort/argsort
tf.sort( ),完成张量在某一个维度上的排序,返回一个排序后的tensor,direction=‘DESCENDING’(降序)指定排序方式,没有写默认为升序。
tf.argsort( ),返回排序的索引值。

a = tf.random.shuffle(tf.range(5))  #numpy=array([2,0,3,4,1])
tf.sort(a, direction='DESCENDING')
>><tf.Tensor:id=397,shape=(5,),dtype=int32,numpy=array([4,3,2,1,0])>

tf.argsort(a, direction='DESCENDING')
>><tf.Tensor:id=409,shape=(5,),dtype=int32,numpy=array([3,2,0,4,1])>

idx = tf.argsort(a, direction='DESCENDING')
tf.gater(a,idx)
>><tf.Tensor:id=422,shape=(5,),dtype=int32,numpy=array([4,3,2,1,0])>
a = tf.random.uniform([3,3],maxval=10,dtype=tf.int32)  
array([[4,6,8],
	   [9,4,7],
	   [4,5,1]])
tf.sort(a)
array([[4,6,8],
	   [4,7,9],
	   [1,4,5]])
tf.sort(a, direction='DESCENDING')
array([[8,6,4],
	   [9,7,4],
	   [5,4,1]])
idx = tf.argsort(a)
array([[0,1,2],
	   [1,2,0],
	   [2,0,1]])

2.top_k
tf.math.top_k(a, n )返回前n个排序后的tensor,

>>a  
array([[4,6,8],
	   [9,4,7],
	   [4,5,1]])
res = tf.math.top_k(a,2)
res.indices
<tf.Tensor:id=467,shape=(3,2),dtype=int32,
numpy=array([[2,1],
	         [0,2],
	         [1,0]])>
res.value
<tf.Tensor:id=466,shape=(3,2),dtype=int32,
numpy=array([[8,6],
	         [9,7],
	         [5,4]])>

3.Top_k Accuracy
这个概念是单标签多分类中的,先说我们认识的acc,这个是求softmax后的最大值索引,只要对应上就是预测正确。
最大值的索引可能与标签没有对应上,只要最大值索引排序中前k个有对应的正确标签就说明预测对了,举个例子:
预测值A,B的得分为:
A: [0.2,0.3,0.4,0.1]
B: [0.15,0.3,0.05,0.5]
求argmax(最大值的索引)后为:
A: [2,1,0,3]
B: [3,1,0,2]
正确标签y为
A:[0,1,0,0]
B:[0,1,0,0]
真实的A,B的位置argmax后为:
[1]
[1]
top1表示取预测值的第一组数据与真实值的索引比较,top1直接为0,因为最大值索引对应不上。
但top2为100%,最大值排序后相应的索引顺序
[2,1]
[3,1]
索引都是第二个正确,那top2 acc肯定是100%了。
top3也为100%,最大值排序后相应的索引顺序
[2,1,0]
[3,1,0]

数据的填充与复制

1.tf.pad( )填充
如图:
tf.pad( a, [ [1,2] ] ) 将一维数据a的左边添加一个数据,右边添加两个数据;
tf.pad( b, [ [0,1], [1,1] ] )将二维数据左右各加一列,上面不添加小面添加一行。
pad中的第一个维度表示行,第二个维度表示列。


a[4,28,28,3],其中batch=4,channl=3,不需要padding,只需要将图片的行和列进行padding,所以只在行和列中添加数据,下面的例子中表示在图片的左右个添加两列,上下各添加两行,所以图片的size=[4,32,32.3]

padding的目的是为了保持卷积后的feature_map的size与image的size一样。

2.tf.tile( )复制
tf.tlie()在指定维度上进行数据的复制,占用内存。
tf.tile(a,[1,2])其中的[1, 2]表示第一个维度不变,第二个维度变为原来的两倍,即3列变6列。
同样的[2,1]表示第一个维度变两倍,第二个维度不变,即3行变6行。
在这里插入图片描述

3.tf.boadcast_to( )复制
tf.boadcast_to( )复制不占用内存
在这里插入图片描述

张量限幅

1.tf.clip_by_value()
根据值进行裁剪
例:
tf.clip_by_value(a,2,8)表示将小于2的值赋值为2,大于8的值赋值为8。
tf.maximum()表示max(x,0)函数:if x<0,x=0;else x
tf.minimum()表示min(x,0)函数:if x>0,x=0;else x
maximum和minimum一次只能对一边进行限幅,如果对两边进行限幅可以联合使用minimum( maximum( a, 2 ),8)等价于clip_by_value(a,2,8)
在这里插入图片描述
2.tf.clip_by_norm()
根据范数进行裁剪
目的:等比例放缩
例:将a变为aa,首先创建一个a,计算a的二阶范数(先平方和再开根号),tf.clip_by_norm(a,15)将a的二阶范数限制在15。达到将a的数等比例的缩小。
在函数中的目的是不改变梯度方向,从而改善梯度爆炸与梯度消失的问题。
tf.clip_by_global_norm(),用于梯度截断。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值