tensorflow Shapes and Shaping

来源:互联网 发布:java创建线程runnable 编辑:程序博客网 时间:2024/05/19 22:55

tf.shape(input, name=None)

mb = tf.constant([[1,2,3],[4,5,6]])shape = tf.shape(mb)sess.run(shape)

输出是:array([2, 3], dtype=int32)


tf.size(input, name=None)

Returns the size of a tensor.

mb = tf.constant([[1,2,3],[4,5,6]])shape = tf.shape(mb)shape = tf.size(mb)

输出是:6


tf.rank(input, name=None)
Returns the rank of a tensor.

注解:其中这个rank(阶),可以理解为数组的维度(几维数据)


mb = tf.constant([[1,2,3],[4,5,6]])sess.run(tf.rank(mb)


tf.reshape(tensor, shape, name=None

Reshapes a tensor

mb = tf.constant([[1,2,3],[4,5,6]])sess.run(tf.reshape(mb, [3,2]))
输出是:

array([[1, 2],
       [3, 4],
       [5, 6]], dtype=int32)


tf.squeeze(input, squeeze_dims=None, name=None)

Removes dimensions of size 1 from the shape of a tensor.

arr = tf.Variable(tf.truncated_normal([3,4,1,6,1], stddev=0.1))arr2 = tf.squeeze(arr)
输出是:
array([3, 4, 6], dtype=int32


tf.expand_dims(input, dim, name=None)
Inserts a dimension of 1 into a tensor's shape.

注释:在tensor中增加维度,其中dim表示增加的位置


ma = tf.constant([1.1, 1.2])tf.expand_dims(ma, 1)sess.run(mc)
输出是:
array([[ 1.10000002],
       [ 1.20000005]], dtype=float32)