tensorflow2.0-维度变换

33 篇文章 0 订阅
30 篇文章 0 订阅

reshape(合并维度)

a = tf.random.normal((4,28,28,3))
a_1 = tf.reshape(a,[4,784,3]).shape    #1
a_1 = tf.reshape(a,[4,-1,3]).shape     #和1等价
a_2 = tf.reshape(a,[4,784*3]).shape    #2
a_2 = tf.reshape(a,[4,-1]).shape       #和2等价
a
print (a_1)
print (a_2)
"""
28*28转为一个维度
28*28*3转为一个维度
(4, 784, 3)
(4, 2352)
"""

transpose(改变维度顺序)

a = tf.random.normal((4,28,28,3))
b =tf.transpose(a).shape                    #逆序输出
c =tf.transpose(a,perm=[0,1,3,2]).shape     #定义输出顺序
print (b)
print (c)
"""
(3, 28, 28, 4)
(4, 28, 3, 28)
"""

expand_dims(增加)

a1 = tf.random.normal((4,35,8))
b1 = tf.expand_dims(a1,axis=0).shape
b2 = tf.expand_dims(a1,axis=3).shape
b3 = tf.expand_dims(a1,axis=-1).shape#倒数第一个位置
b4 = tf.expand_dims(a1,axis=-4).shape#倒数第四个位置
print (b1)
print (b2)
print (b3)
print (b4)
"""
(1, 4, 35, 8)
(4, 35, 8, 1)
(4, 35, 8, 1)
(1, 4, 35, 8)
"""

squeeze(减少)

s = tf.squeeze(tf.zeros([1,2,1,1,4])).shape #减少所有维度为1的维度
r1 = tf.zeros([1,2,1,3])
s1 = tf.squeeze(r1,axis=0).shape            #减少下标为0的维度
s2 = tf.squeeze(r1,axis=-2).shape           #减少下标为-2的维度
print (s)
print (s1)
print (s2)
"""
(2, 4)
(2, 1, 3)
(1, 2, 3)
"""
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值