tf.segment_sum和tf.unsorted_segment_sum理解实例
2024-08-26 23:58:37
本文来自 guotong1988 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/guotong1988/article/details/77622790
import tensorflow as tf
c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
result = tf.segment_sum(c, tf.constant([0, 0, 1]))#第二个参数长度必须为3
result_ = tf.segment_sum(c, tf.constant([0, 1, 1]))
result__ = tf.segment_sum(c, tf.constant([0, 1, 2]))
result2 = tf.unsorted_segment_sum(c, tf.constant([2, 1, 1]),3)#第二个参数长度必须为3
result3 = tf.unsorted_segment_sum(c, tf.constant([1, 0, 1]),2)
#result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),2) #错误,segment_ids[0] = 2 is out of range [0, 2)
result4 = tf.unsorted_segment_sum(c, tf.constant([2, 0, 1]),3)
result5 = tf.unsorted_segment_sum(c, tf.constant([3, 1, 0]),5)
sess = tf.Session()
print("result")
print(sess.run(result))
print("result_")
print(sess.run(result_))
print("result__")
print(sess.run(result__))
print("result2")
print(sess.run(result2))
print("result3")
print(sess.run(result3))
print("result4")
print(sess.run(result4))
print("result5")
print(sess.run(result5))
运行结果:
result
[[0 0 0 0]
[5 6 7 8]]
result_
[[1 2 3 4]
[4 4 4 4]]
result__
[[ 1 2 3 4]
[-1 -2 -3 -4]
[ 5 6 7 8]]
result2
[[0 0 0 0]
[4 4 4 4]
[1 2 3 4]]
result3
[[-1 -2 -3 -4]
[ 6 8 10 12]]
result4
[[-1 -2 -3 -4]
[ 5 6 7 8]
[ 1 2 3 4]]
result5
[[ 5 6 7 8]
[-1 -2 -3 -4]
[ 0 0 0 0]
[ 1 2 3 4]
[ 0 0 0 0]]
将索引值相同的进行求和,其余的按顺序计算。
最新文章
- Django入门
- Bash:-set设置位置变量结合while和shift使用
- andriod arcgis加载影像TIF
- 根据评分,用js输出评价星星的样式
- lex&;yacc5--YYSTYPE
- phpexcel 一些基本的设置 (表格的基本属性)
- 6.ListView
- Curl命令使用方法
- oracle 查询重复数据
- jQuery(3)——DOM操作
- Android Camera(一)
- 深度学习 for java http://deeplearning4j.org/
- npm 模块化管理
- 使用第三方工具Thumbnailator动态改变图片尺寸
- Docker安装MySQL并配置my.cnf
- Swift的函数与函数指针、闭包Closure等相关内容介绍
- js基础(常用语法、类型、函数)
- Prism 的 TabControl 导航
- ZJOI2018 round^2 游记
- Java Spring-Bean