常见张量函数介绍

np.newaxis:插入新维度
例子1:

a = np.array([1,2,3,4])
aa = a[:,np.newaxis]
print(a)
print(aa)

输出:

[1 2 3 4]
[[1]
 [2]
 [3]
 [4]]

例子2:

a=np.array([1,2,3,4])
aa=a[np.newaxis,:]
print(a)
print (aa)

输出:

[1 2 3 4]
[[1 2 3 4]]

tf.expand_dims也可以对维度进行添加

a = tf.random.normal([4,35,8])
tf.expand_dims(a,axis=0).shape
#tensorshape([1,4,35,8])
tf.expand_dims(a,axis=3).shape
#tensorshape([4,35,8,1])
a = tf.random.normal([4,35,8])
tf.expand_dims(a,axis=0).shape
#tensorshape([1,4,35,8])
tf.expand_dims(a,axis=3).shape
#tensorshape([4,35,8,1])
tf.expand_dims(a,axis=-1).shape
#tensorshape([4,35,8,1])
tf.expand_dims(a,axis=-4).shape
#tensorshape([1,4,35,8])

squeeze dim:对维度为1的维也可以去掉,比如4个班级,35个学生,每个学生7门成绩:【4,35,7,1】,可以转换成【4,35,7】

tf.squeeze(tf.zeros([1,2,1,1,3])).shape
#tensorshape([2,3])
a = tf.zeros([1,2,1,3])
tf.squeeze(a,axis=0).shape
tensorshape([2,1,3])
tf.squeeze(a,axis=2).shape
tensorshape([1,2,3])
tf.squeeze(a,axis=-2).shape
tensorshape([1,2,3])
tf.squeeze(a,axis=-4).shape
tensorshape([2,1,3])

broadcast:张量叠加

例如
a@x+b
【7,784】@【784,10】+【10】
【7,10】+【10】
到这一步的时候,会将b转换成【7,10】
再执行【7,10】+【7,10】

key idea
*insert 1 dim ahead if needed
*expand dims with size 1 to same size
*feature maps:[4,32,32,3]
*bias:[3]->[1,1,1,32]->[4,32,32,3]

实际上broadcast相当于先执行expand_dim(axis=0)[1,10],在执行tf.tile[b,10]

a = tf.ones([3,4])
a1 = tf.broadcast_to(a,[2,3,4])

在这里插入图片描述
例子:
1.自动扩张
在这里插入图片描述
2.显式扩张
在这里插入图片描述
*

合并与分割

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])
a = tf.ones([4,32,8])
b = tf.ones([4,3,8])
tf.concat([a,b],axis=1).shape
tensorshape([4,35,8])

crate a new dim
stack:

a.shape
#tensorshape([4,35,8])
b.shape
#tensorshape([4,35,8])
tf.concat([a,b],axis=-1).shape
#tensorshape([4,35,16])
tf.stack([a,b],axis=0).shape
#tensorshape([2,4,35,8])
tf.stack([a,b],axis=3).shape
#tensorshape([4,35,8,2])

unstack:将合并之后的tensor切分成原来的样子
在这里插入图片描述
unstack:
在这里插入图片描述
*

tensor的数据统计

a = tf.noes([2,2])
tf.norm(a)#张量的二范数:2
tf.sqrt(tf.reduce_sum(tf.sqre(a)))#2
tf.norm(a,ord=2,axis=1)#在第一个维度上求二范数:[1.4142135,1.4142135]
tf.norm(a,ord=1)#一范数:|1|+|1|+|1|+|1|=4
tf.norm(a,ord=1,axis=0)#在第0个维度求一范数:[2,2]
tf.norm(a,ord=1,axis=1)#在第1个维度求一范数:[2,2]

a = tf.random.normal([4,10])
tf.reduce_min(a),tf.reduce_max(a),tf.reduce_mean(a)
tf.reduce_min(a,axis=1),tf.reduce_max(a,axis=1),tf.reduce_mean(a,axis=1)
#shape=(4,)
最大值与最小值所在位置
tf.argmax(a).shape
#[10]
tf.argmax(a)#[10]
tf.argmin(a)#[10]

tf.equal:
a = tf.constant([1,3,2,2,5])
b = tf.range(5)#0,1,2,3,4
tf.equal(a,b)#F,F,T,F,F
res = tf.equal(a,b)
tf.reduce_sum(tf.cast(res,dtype = tf.int32))#得出匹配数

accuracy
a
#array([0.1,0.2,0.7],[0.9,0.05,0.05])
pred = tf.cast(tf.argmax(a,axis=1),dtype = tf.int32)
#array([2,0])
y
#array([2,1])
tf.equal(y,pred)
#array([True,False])
#计算正确的个数
correct = tf.reduce_sum(tf.cast(tf.equal(y,pred),dtype = tf.int32))
#1

#去除tensor中的不重复元素
a = tf.range(5)
print(tf.unique(a))
#array([0, 1, 2, 3, 4]),idx=([…])
b = tf.constant([4,2,2,4,3])
print(tf.unique(b))
#array([4, 2, 3]),idx=([…])

#通过tf.gather还原
unique,idx = tf.unique(b)
#array([4, 2, 3])
c = tf.gather(unique,idx)
print©
#得到tf.Tensor([4 2 2 4 3], shape=(5,), dtype=int32)

张量排序

sort:得到排序后的结果
argsort:得到排序后的index
a = tf.random.shuffle(tf.range(5))
print(a)#[3 0 1 4 2]
b = tf.sort(a,direction = ‘DESCENDING’)
print(b)#[4 3 2 1 0]
idx = tf.argsort(a,direction = ‘DESCENDING’)
print(idx)#[3 0 4 2 1]
#通过idx和tf.gather得到排序后的张量
a1 = tf.gather(a,idx)
print(a1)#[4 3 2 1 0]
topk:得到最大的前某个

top_k accuracy

import tensorflow as tf
#计算top_k accuracy
def accuracy(output, target, topk):
    maxk = max(topk)
    batch_size = target.shape[0]

    pred = tf.math.top_k(output, maxk).indices
    pred = tf.transpose(pred, perm = [1, 0])
    target_ = tf.broadcast_to(target, pred.shape)
    correct = tf.equal(pred,target_)#[k,b]

    res = []
    for k in topk:
        correct_k = tf.cast(tf.reshape(correct[:k],-1),dtype = tf.float32)
        correct_k = tf.reduce_sum(correct_k)
        acc = float(correct_k/batch_size)
        res.append(acc)

    return acc

output = tf.random.normal([10, 6])
output = tf.math.softmax(output, axis = 1)
target = tf.random.uniform([10], maxval = 6, dtype = tf.int32)
print("prob:",output.numpy())
pred = tf.argmax(output, axis = 1)
print("pred:", pred.numpy())
print("label:", target.numpy())

topk=(1,2,3,4,5,6)
acc = accuracy(output, target, topk)#这样topk的调用有错,不知道为啥
print("top_1_6 acc:", acc)

填充与复制

pad

import tensorflow as tf
a = tf.reshape(tf.range(9),[3,3])#0--8
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
b = tf.pad(a,[[0,0],[0,0]])
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
c = tf.pad(a,[[1,0],[0,0]])#【【上行,下行】,【左列,右列】】
# [[0 0 0]
#  [0 1 2]
#  [3 4 5]
#  [6 7 8]]
#多维度的pad
a = tf.random.normal([4,28,28,3])
b = tf.pad(a,[[0,0],[2,2],[2,2],[0,0]])
print(b.shape)#(4, 32, 32, 3)

tile

a = tf.reshape(tf.range(9),[3,3])#0--8
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
b = tf.tile(a,[1,2])#[对应维度复制的次数],1表示当前维度不复制
print(b.numpy())
c = tf.tile(a,[2,1])
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]
#  [0 1 2]
#  [3 4 5]
#  [6 7 8]]
d = tf.tile(a,[2,2])
# [[0 1 2 0 1 2]
#  [3 4 5 3 4 5]
#  [6 7 8 6 7 8]
#  [0 1 2 0 1 2]
#  [3 4 5 3 4 5]
#  [6 7 8 6 7 8]]

broadcast_to

a = tf.reshape(tf.range(9),[3,3])#0--8
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
aa = tf.expand_dims(a,axis=0)#shape=(1,3,3)
bb = tf.tile(aa,[2,1,1])
print(bb.numpy())
cc = tf.broadcast_to(aa,[2,3,3])
print(cc.numpy())
# [[[0 1 2]
#   [3 4 5]
#   [6 7 8]]
# 
#  [[0 1 2]
#   [3 4 5]
#   [6 7 8]]]

张量限幅

clip_by_value
relu
clip_by_norm
gradient clipping

clip_by_value

a = tf.range(0,10)
# [0 1 2 3 4 5 6 7 8 9]
b = tf.maximum(a,2)
# [2 2 2 3 4 5 6 7 8 9]
c = tf.minimum(a,8)
# [0 1 2 3 4 5 6 7 8 8]
d = tf.clip_by_value(a,2,8)
# [2 2 2 3 4 5 6 7 8 8]

tf.nn.relu

a = a-5
# [-5 -4 -3 -2 -1  0  1  2  3  4]
b = tf.nn.relu(a)
# [0 0 0 0 0 0 1 2 3 4]
c = tf.maximum(a,0)
# [0 0 0 0 0 0 1 2 3 4]

clip_by_norm#根据范数进行裁剪

a = tf.random.normal([2,2],mean=10)
print(a.numpy())
b = tf.norm(a)#a的二范数
print(b.numpy())
aa = tf.clip_by_norm(a,15)
print(aa.numpy())
bb = tf.norm(aa)
print(bb.numpy())

高阶操作
where

a = tf.random.normal([3,3])
# [[ 0.01809306 -0.37906095 -0.26759133]
#  [-0.03647839 -0.9332207  -1.6251019 ]
#  [ 0.63349974  0.0932993   1.5583222 ]]
mask = a>0#返回bool类型的tensor
# [[ True False False]
#  [False False False]
#  [ True  True  True]]
a_mask = tf.boolean_mask(a,mask)
# [0.01809306 0.63349974 0.0932993  1.5583222 ]
indices = tf.where(mask)
a_in = tf.gather_nd(a,indices)
# [0.01809306 0.63349974 0.0932993  1.5583222 ]

wher(cond,A,B)

A = tf.ones([3,3])
B = tf.zeros([3,3])
ab = tf.where(mask,A,B)#mask中True替换为A中对应元素,false替换为B中对应元素
# mask
# [[ True  True False]
#  [ True False False]
#  [ True False  True]]
# [[1. 1. 0.]
#  [1. 0. 0.]
#  [1. 0. 1.]]

scatter_nd

indices = tf.constant([[4],[3],[1],[7]])
updates = tf.constant([9,10,11,12])
shape = tf.constant([8])
print(shape.shape)
a = tf.scatter_nd(indices,updates,shape)
#按indices的位置把updates中对应元素更新到shape形状,数值全为1的tensor中
# [ 0 11  0 10  9  0  0 12]
indices = tf.constant([[0],[2]])
updates = tf.constant([[[5,5,5,5],[6,6,6,6],
                        [7,7,7,7],[8,8,8,8]],
                       [[5,5,5,5],[6,6,6,6],
                        [7,7,7,7],[8,8,8,8]]])
shape = tf.constant([4,4,4])
a = tf.scatter_nd(indices,updates,shape)
# [[[5 5 5 5]
 #  [6 6 6 6]
 #  [7 7 7 7]
 #  [8 8 8 8]]
 #
 # [[0 0 0 0]
 #  [0 0 0 0]
 #  [0 0 0 0]
 #  [0 0 0 0]]
 #
 # [[5 5 5 5]
 #  [6 6 6 6]
 #  [7 7 7 7]
 #  [8 8 8 8]]
 #
 # [[0 0 0 0]
 #  [0 0 0 0]
 #  [0 0 0 0]
 #  [0 0 0 0]]]

meshgrid

#通过numpy生成x轴,y轴-2到2间的25个点
points = []
for y in np.linspace(-2,2,5):
    for x in np.linspace(-2,2,5):
        points.append([x,y])
points = np.array(points)
# [[-2. -2.]
#  [-1. -2.]
#  [ 0. -2.]
#  [ 1. -2.]
#  [ 2. -2.]
#  [-2. -1.]
#  [-1. -1.]
#  [ 0. -1.]
#  [ 1. -1.]
#  [ 2. -1.]
#  [-2.  0.]
#  [-1.  0.]
#  [ 0.  0.]
#  [ 1.  0.]
#  [ 2.  0.]
#  [-2.  1.]
#  [-1.  1.]
#  [ 0.  1.]
#  [ 1.  1.]
#  [ 2.  1.]
#  [-2.  2.]
#  [-1.  2.]
#  [ 0.  2.]
#  [ 1.  2.]
#  [ 2.  2.]]

通过tensorflow生成这些点,meshgrid

x = tf.linspace(-2.,2,5)
y = tf.linspace(-2.,2,5)
points_x,points_y = tf.meshgrid(x,y)
#points_x
# [[-2. -1.  0.  1.  2.]
#  [-2. -1.  0.  1.  2.]
#  [-2. -1.  0.  1.  2.]
#  [-2. -1.  0.  1.  2.]
#  [-2. -1.  0.  1.  2.]]
#points_y
# [[-2. -2. -2. -2. -2.]
#  [-1. -1. -1. -1. -1.]
#  [ 0.  0.  0.  0.  0.]
#  [ 1.  1.  1.  1.  1.]
#  [ 2.  2.  2.  2.  2.]]

points = tf.stack([points_x,points_y], axis=2)
# [[[-2. -2.]
#   [-1. -2.]
#   [ 0. -2.]
#   [ 1. -2.]
#   [ 2. -2.]]
#
#  [[-2. -1.]
#   [-1. -1.]
#   [ 0. -1.]
#   [ 1. -1.]
#   [ 2. -1.]]
#
#  [[-2.  0.]
#   [-1.  0.]
#   [ 0.  0.]
#   [ 1.  0.]
#   [ 2.  0.]]
#
#  [[-2.  1.]
#   [-1.  1.]
#   [ 0.  1.]
#   [ 1.  1.]
#   [ 2.  1.]]
#
#  [[-2.  2.]
#   [-1.  2.]
#   [ 0.  2.]
#   [ 1.  2.]
#   [ 2.  2.]]]

tf.cast 数据类型转换
cast(x, dtype, name=None)
第一个参数 x: 待转换的数据(张量)
第二个参数 dtype: 目标数据类型
第三个参数 name: 可选参数,定义操作的名称
int32转换成float32

t1 = tf.Variable([1,2,3,4,5])
t2 = tf.cast(t1,dtype=tf.float32)

交叉熵,预测问题中计算loss
tf.losses.categorical_crossentropy()
numpy.matrix.flat #一维迭代器
x1 = np.array([[1,2],[1,3]])
x1.falt[3]
#3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值