tf.shape(tf.Tensor) vs tf.Tensor.shape

本文介绍了在TensorFlow中获取Tensor形状的两种方法:通过Tensor对象的shape属性和使用tf.shape操作。此外,还展示了如何根据给定矩阵的列数创建相同大小的零矢量。
部署运行你感兴趣的模型镜像

有两种方法可以获取 tf.Tensor 的形状。

可以通过查看 tf.Tensor 对象的 shape 属性获取这些信息。该方法会返回一个 TensorShape 对象。

可以调用 tf.shape 操作,返回 tf.Tensor对象。

>> from __future__ import absolute_import
>> from __future__ import division
>> from __future__ import print_function

>> import numpy as np
>> import tensorflow as tf

>> squarish_squares = tf.Variable([ [4, 9], [16, 25] ], tf.int32)
>> sess = tf.Session()
>> shape1 = squarish_squares.shape
>> shape2 = tf.shape(squarish_squares)
>> shape1
TensorShape([Dimension(2), Dimension(2)])
>> shape2
<tf.Tensor 'Shape:0' shape=(2,) dtype=int32>
>> sess.run(shape2)
array([2, 2])

以下代码展示了如何创建大小与给定矩阵中的列数相同的零矢量:

zeros = tf.zeros(my_matrix.shape[1])

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值