tensorflow - 如何检查张量是张量流中的单个值?
问题描述
我想检查张量是否只是一个数字。有什么办法可以使这种检查比 更好shape == ()
?
tensor_number = tf.constant(1)
tensor_not_number = tf.constant([1])
print(tensor_number.shape == ())
print(tensor_not_number.shape == ())
>> True
>> False
解决方案
标量张量的秩为零。我们可以tf.rank
用来检查张量是否是标量。
a=tf.constant(np.random.rand(3,3,3))
b=tf.constant(np.random.rand())
c=tf.constant(np.random.rand(1))
tf.print(tf.rank(a))
tf.print(tf.rank(b))
tf.print(tf.rank(c))
if tf.rank(b) == 0:
tf.print('b is a scalar')
'''
3
0
1
b is a scalar
'''
推荐阅读
- c# - 如何将 RadFixedPage 的内容设置为网格
- sql - SQL:如何检查 9 列中的任何 2 列是否具有相同的值?
- android - 如何创建多行多列 RecyclerView?
- docker-compose - 如何防止 docker-compose up 更改文件夹权限
- c++ - 我可以使用winapi以非常特殊的方式映射网络驱动器吗?
- python - 获取行中数字的平均值 SQL (Sqlite3)
- r - 如何使用 R 计算数据集中不同组的平均值
- enums - 有没有办法将枚举文字与 UML 类图中的类相关联?
- python - 在python中的另一个类中创建具有随机参数的类的实例
- r - 防止在使用 ggsave() 的函数时生成 Rplot.emf