tf.newaxis的作用 作者:马育民 • 2019-12-29 15:52 • 阅读:13054 # 概述 tf.newaxis的功能与np.newaxis的功能、用法相同,是增加维度的 与```tf.expand_dims()```功能相同,用法不同 # 例子 对一维数组改变维度 ``` a=tf.Variable([1,2,3,4]) print("a.shape:",a.shape) print("a:",a) print("-"*50) a2 = a[tf.newaxis, :] print("a2.shape:",a2.shape) print("a2:",a2) print("-"*50) a3 = a[:,tf.newaxis] print("a3.shape:",a3.shape) print("a3:",a3) ``` 执行结果: ``` a.shape: (4,) a: -------------------------------------------------- a2.shape: (1, 4) a2: tf.Tensor([[1 2 3 4]], shape=(1, 4), dtype=int32) -------------------------------------------------- a3.shape: (4, 1) a3: tf.Tensor( [[1] [2] [3] [4]], shape=(4, 1), dtype=int32) ``` # 例子2 对二维数组改变维度 ### 在原有的2个维度中间插入一个维度 ``` b=tf.constant([[1,2,3],[4,5,6]]) print("b.shape:",b.shape) print("b:",b) print("-"*50) b2 = b[:,tf.newaxis] print("b2.shape:",b2.shape) print("b2:",b2) ``` ### 在原有的维度后增加一个维度 ``` b=tf.constant([[1,2,3],[4,5,6]]) print("b.shape:",b.shape) print("b:",b) print("-"*50) b3 = b[:,:,tf.newaxis] print("b3.shape:",b3.shape) print("b3:",b3) ``` 原文出处:/show_1EF4hUvlJoiS.html