Tensorflow学习笔记----维度变换、broadcasting、数学运算

一.维度变换

**tf.reshape(a,[……]):**可以重新定义元素的shape以便数据被更好的利用

#要保证reshape中[]中的所有值之积 = a的各维的数之积
a = tf.random.normal([4,28,28,3])
print(tf.reshape(a,[4,784,3]).shape)    #out:(4,784,3),4*28*28*3 = 4*784*3
print(tf.reshape(a,[4,-1,3]).shape)    #out:(4,784,3),这里-1相当于省略中间的一位,使得系统自动计算出来
#恢复时可能会出错,需要记得原本的content,也就是原本的维度的顺序及维度的数


	tf.transpose():可将原本维度转置(轴交换),设置特定维度顺序,即content修改
a = tf.random.normal([4,3,2,1])
print(tf.transpose(a).shape)    #out:(1, 2, 3, 4),彻底逆转
print(tf.transpose(a,perm=[0,1,3,2]).shape) #out:(4, 3, 1, 2),按照perm中所写的下标顺序调整

	Expand dim:增加(展开)维度
#data:[classes,students,subjects] = [4,35,8]
a = tf.random.normal([4,35,8])#一个学校的数据
#若想增加一个学校:[1,4,35,8]+[1,4,35,8] = [2,4,35,8]
print(tf.expand_dims(a,axis=0).shape)   #out:(1, 4, 35, 8)  在第0个间隙增加
print(tf.expand_dims(a,axis=3).shape)   #out:(4, 35, 8, 1)  在第3个间隙增加


	Squeeze dim:减少维度,可以减少维度元素数为1的维度
print(tf.squeeze(tf.zeros([1,2,1,1,3])).shape)  #out:(2, 3)
print(tf.squeeze(tf.zeros([1,2,1,3]),axis=0).shape) #out:(2, 1, 3)  减少特定的维度

二.Broadcasting广播机制

没有复制数据却可以使数据扩张:tf.broadcast_to()
原理:1、当两个tensor相加时,若两者维度不一致,则在相应位置添加一个维度;2.将维度为1的tensor扩展为同样维度,从右边最小维度开始,往左延伸,若相应的位置没有维度,则插入相应的维度,且扩张为相应的size。
在这里插入图片描述
broadcasting

  • 没有复制数据却可以使数据扩张:tf.broadcast_to()
    原理:1、当两个tensor相加时,若两者维度不一致,则在相应位置添加一个维度;2.将维度为1的tensor扩展为同样维度
import tensorflow as tf
x = tf.random.normal([4,32,32,3])
print((x+tf.random.normal([3])).shape)  #out:(4, 32, 32, 3),此时[3]已经扩张了
print((x+tf.random.normal([32,32,1])).shape)  #out:(4, 32, 32, 3),此时[3]已经扩张了
print((x+tf.random.normal([2,32,32,1])).shape)  #out:会报错,只能由1扩展,第一位不相等且不为1
tf.broadcast_to([……],[……]):将前面的[……]扩展为后面的[……],并返回
b = tf.broadcast_to(tf.random.normal([4,1,1,1]),x.shape)
print(b.shape)  #out:(4, 32, 32, 3)

三.数学运算

+、-、*、/:元素加减乘除
、pow、square:平方、次方(b3=b^3=pow(b,3))
sqrt:平方根
//、%:整除、取余
exp、log:对数
@、matmul:矩阵运算
reduce_mean、max、min、sum:均值、最大值、最小值、和

a = tf.ones([2,2])
# tf.Tensor(
# [[1. 1.]
#  [1. 1.]], shape=(2, 2), dtype=float32)
b = tf.fill([2,2],2.)
# tf.Tensor(
# [[2. 2.]
#  [2. 2.]], shape=(2, 2), dtype=float32)

#+、-、*、/、//、%运算
print(a+b,a-b,a*b,a/b,b//a,b%a)
#log、exp运算
print(tf.math.log(a))   #对数运算
# tf.Tensor(
# [[0. 0.]
#  [0. 0.]], shape=(2, 2), dtype=float32)
print(tf.exp(a))
# tf.Tensor(
# [[2.7182817 2.7182817]
#  [2.7182817 2.7182817]], shape=(2, 2), dtype=float32)

#注意:这里的log(a)只能运算以2为底的自然对数log(a,2)
#若是想计算其他对数,可根据公式:log(b,a)/log(c,a)=log(b,c)
print(tf.math.log(8.)/tf.math.log(2.))  #out:tf.Tensor(3.0, shape=(), dtype=float32):即log(8,2)=3

#**、pow、square:平方、次方(b**3=b^3=pow(b,3))
print(tf.pow(b,3))  #这里也等同于b**3
# tf.Tensor(
# [[8. 8.]
#  [8. 8.]], shape=(2, 2), dtype=float32)
print(tf.sqrt(b))   #开方
# tf.Tensor(
# [[1.4142135 1.4142135]
#  [1.4142135 1.4142135]], shape=(2, 2), dtype=float32)

#@、matmul:矩阵相乘运算
print(a@b)  #这里也等同于(tf.matmul(a,b))
# tf.Tensor(
# [[4. 4.]
#  [4. 4.]], shape=(2, 2), dtype=float32)

print(x@W+b)
# tf.Tensor(
# [[2.1]
#  [2.1]
#  [2.1]
#  [2.1]], shape=(4, 1), dtype=float32)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值