本文来自 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]]

将索引值相同的进行求和,其余的按顺序计算。

最新文章

  1. Django入门
  2. Bash:-set设置位置变量结合while和shift使用
  3. andriod arcgis加载影像TIF
  4. 根据评分,用js输出评价星星的样式
  5. lex&yacc5--YYSTYPE
  6. phpexcel 一些基本的设置 (表格的基本属性)
  7. 6.ListView
  8. Curl命令使用方法
  9. oracle 查询重复数据
  10. jQuery(3)——DOM操作
  11. Android Camera(一)
  12. 深度学习 for java http://deeplearning4j.org/
  13. npm 模块化管理
  14. 使用第三方工具Thumbnailator动态改变图片尺寸
  15. Docker安装MySQL并配置my.cnf
  16. Swift的函数与函数指针、闭包Closure等相关内容介绍
  17. js基础(常用语法、类型、函数)
  18. Prism 的 TabControl 导航
  19. ZJOI2018 round^2 游记
  20. Java Spring-Bean

热门文章

  1. application/x-www-form-urlencoded 与 application/json区别
  2. jenkins配置slave节点 构建项目并执行操作
  3. 【Java】JAVA开发人员常见环境工具安装
  4. BZOJ5074 小B的数字
  5. 浅谈Android发展趋势分析
  6. Bootstrap 环境安装
  7. Linux网络接口配置文件解析
  8. redis分布式(主从复制)
  9. Zend Hash table 详解--转
  10. Codeforces 671C. Ultimate Weirdness of an Array(数论+线段树)