1.变换维度
```python
```python
```python
import os
os.environ['TF_CPP_MIN_L加粗样式OG_LEVEL'] = '2'#过滤爆红的信息
import tensorflow as tf
a=tf.random.normal([4,28,28,3])#建立一个四维的矩阵
print("矩阵:",a.shape)
print("维度:",a.ndim)
print(tf.reshape(a,[4,-1,3]).shape)#一个[]里面只能有一个-1,-1代指的值为:4*28*28*3/(4*3)`在这里插入代码片`
输出
矩阵: (4, 28, 28, 3)
维度: 4
(4, 784, 3)
//案例:
print(tf.reshape(a,[3,-1]).shape)#跟上同理
//输出:
(3, 3136)
2.转置
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'#过滤爆红的信息
import tensorflow as tf
a=tf.random.normal([4,28,28,3])#建立一个四维的矩阵
print(tf.transpose(a).shape)#转置
print(tf.transpose(a,perm=[0,3,1,2]).shape)#按参数转置
输出:
(3, 28, 28, 4)
(4, 3, 28, 28)
3.增加轴
规律:axis为正数在前加,axis为负数在后加
b=tf.expand_dims(a,axis=0)#在axis=0处增加一个轴
print(b.shape)
print(tf.expand_dims(a,axis=-1).shape)#在最后面加一个轴
#输出:
(1, 4, 28, 28, 3)
(4, 28, 28, 3, 1)
4.减轴
print(tf.squeeze(b,axis=0).shape)
#输出
(4, 28, 28, 3)