python - 如何在 tf.Tensor 中找到最大值?
问题描述
如何找到每个元素中的最大值以便得到 2、4、6、8?
import tensorflow as tf
a = tf.constant([
[[1, 2]], [[3, 4]],
[[5, 6]], [[7, 8]]])
我尝试了以下代码:
tf.reduce_max(a, keepdims=True)
但这只是给了我 8 作为输出,而忽略了其余的。
解决方案
您必须axis
像这样将参数更改为 -1:
import tensorflow as tf
a = tf.constant([
[[1, 2]], [[3, 4]],
[[5, 6]], [[7, 8]]])
print(tf.reduce_max(a, axis=-1, keepdims=False))
'''
tf.Tensor(
[[2]
[4]
[6]
[8]], shape=(4, 1), dtype=int32)
'''
因为你有一个 3D 张量并且想要访问最后一个维度。
推荐阅读
- ios - 如何从场景委托中调用在视图控制器中下载 URL 的函数?
- javascript - 电子邮件地址验证正则表达式
- oracle - 无法创建配置文件:TNS-04414:文件错误
- amazon-web-services - AppSync 与 Glue 集成
- html - 在动态生成的组件中每行最多显示 4 个图标
- python - 使用 tkinter 按钮在海龟中打开图像时出现错误“pyimage1”不存在
- sql - 由于“未满足的依赖项”,无法在 Ubuntu 上安装 SQL Server 命令行工具
- azure - 如何仅发布对 Azure 应用服务的更改或单独发布 DLL 和 PDF?
- python - 按最大值将 2 个数据集与特定字段上的 pandas 组合
- reactjs - 在 React JS 中是否可以使用子域