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

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

tf.segment_sum和tf.unsorted_segment_sum理解实例的更多相关文章

  1. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...

  2. 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope

    tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...

  3. tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()

    函数原型: tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)   Defined in tensorflo ...

  4. tf.name_scope()和tf.variable_scope() (转)

    网络层中变量存在两个问题: 随着层数的增多,导致变量名的增多: 在调用函数的时候,会重复生成变量,但他们存储的都是一样的变量.   tf.variable不能解决这个问题. 变量作用域使用tf.var ...

  5. tf.unstack()、tf.stack()

    tf.unstack 原型: unstack( value, num=None, axis=0, name='unstack' ) 官方解释:https://tensorflow.google.cn/ ...

  6. tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别

    在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...

  7. 【TensorFlow基础】tf.add 和 tf.nn.bias_add 的区别

    1. tf.add(x,  y, name) Args: x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`, ...

  8. TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

    https://blog.csdn.net/lanchunhui/article/details/61712830 https://www.cnblogs.com/silence-tommy/p/70 ...

  9. TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系

    1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要 ...

随机推荐

  1. python3.6 SSL module is not available

    pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail ...

  2. 条形码生成库 BarcodeLib

    官方介绍 在ASP.NET,Windows,Reporting Service,Crystal Reports 和 RDLC Reports应用程序中轻松生成条形码 生成准确的条形码图像,并可以保存为 ...

  3. javascript 常用知识点

    1:浏览器是有缓存的,开发中可以通过快捷键绕过缓存 对于Windows驱动的系统:Ctrl + F5 对于Mac驱动的系统:Command + Shift + R. 2:精度问题 (符点和大数字可能会 ...

  4. Building microservices with ASP.NET Core (without MVC)(转)

    There are several reasons why it makes sense to build super-lightweight HTTP services (or, despite a ...

  5. bootstrap-datapicker 时间约束

    <div class="input-group date date-picker" id="StartTime"> <input type=& ...

  6. BZOJ 2157 旅行(树链剖分码农题)

    写了5KB,1发AC... 题意:给出一颗树,支持5种操作. 1.修改某条边的权值.2.将u到v的经过的边的权值取负.3.求u到v的经过的边的权值总和.4.求u到v的经过的边的权值最大值.5.求u到v ...

  7. hdu3507 Print Article(斜率优化入门)(pascal)

    Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique, he s ...

  8. 秒杀多线程第八篇 经典线程同步 信号量Semaphore (续)

    java semaphore实现: Semaphore当前在多线程环境下被扩放使用,操作系统的信号量是个很重要的概念,在进程控制方面都有应用.Java 并发库 的Semaphore 可以很轻松完成信号 ...

  9. 使用oVal进行Java Bean 验证的注意事项

    如果需要不同条件验证不同的属性的时候,需要使用profiles属性,每个校验注解符中都有.注意:oVal默认是启用所有的profiles,所以在对单独一个profile进行启用的时候,需要先disab ...

  10. 洛谷 P1233 木棍加工 解题报告

    P1233 木棍加工 题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的: 第一根棍子的准备时间 ...