python - 如何在tensorflow2中改变像numpy和pytorch这样的张量?
问题描述
我想在 Tensorflow2.1 中实现 Informer(一种深度学习模型)。有一些这样的代码:
Q = tensor.shape(B,H,L,D) (for example, Q is a tensor and shape is (B,H,L,D) B is batch)
index = tensor.shape(B,C) (C < L)
Q_reduce = Q[torch.arange(B)[:, None, None], torch.arange(H)[None, :, None], index, :]
print(Q_reduce.shape)
# get shape is (B,B,C,D)
如何在 Tensorflow2 中获得相同的结果?我像这样使用 tf.gather:
Q_reduce = tf.gather(Q,index, axis=-2)
# get shape is (B,H,B,C,D)
解决方案
推荐阅读
- php - 使用 preg_match 验证日期 dd-MON-yyyy
- visual-studio-code - 所有文档的VS Code DocumentSelector?
- angular - 如何拦截 :enter & :leave Angular 中的动画并改变 Diff?
- python - Apache Beam python 流式写入每小时 avro 文件
- ios - 有什么方法可以定期在后台模式(iOS)下发送 HTTP 请求?
- java - 使用 NumberFormat 在 JTextFormattedField 上不设置文本
- visual-studio - 为什么 SSRS 在已经定义时要求定义查询参数?
- react-native - 如何在运行时在反应本机的字典对象数组中添加动态数据
- html - HTML 边框出现在单元格之外
- jenkins - 从 Jenkins 管道并行步骤收集数据