tensorflow Shapes and Shaping

本文详细介绍了TensorFlow中几个常用的基础操作,包括获取张量的形状、大小和秩,以及如何重塑张量、压缩和扩展张量的维度。通过具体的代码示例,帮助读者更好地理解和掌握这些基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值