python - 我如何对 tensorflow 中的前n项求和?

标签 python tensorflow

示例

a = [[1, 0, 1, 0], [0, 1, 1 ,0]]
result = [[a[0,0], a[0,0]+a[0,1], a[0,0]+a[0,1]+a[0,2], a[0,0]+a[0,1]+a[0,2]+a[0,3]], [a[1,0], a[1,0]+a[1,1], a[1,0]+a[1,1]+a[1,2], a[1,0]+a[1,1]+a[1,2]+a[1,3]]
       = [[1, 1, 2, 2], [0, 1, 2 ,2]]

我不知道该怎么做,请帮助我。谢谢

最佳答案

您正在查找累计总和,tf.cumsum ,沿第二轴:

a = tf.Variable([[1, 0, 1, 0], [0, 1, 1 ,0]])
init_op = tf.initialize_all_variables()

# run the graph and calculate the cumsum
with tf.Session() as sess:
    sess.run(init_op) #execute init_op
    print (sess.run(tf.cumsum(a, axis=1)))):

[[1 1 2 2]
 [0 1 2 2]]

关于python - 我如何对 tensorflow 中的前n项求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58131442/

相关文章:

python - tensorflow.keras.dataset.mnist.load_data() 返回内容的说明

python - Django 将自定义表单参数传递给 Formset

python 长时间运行的守护进程作业处理器

go - 在 Golang 应用服务器中重新加载 tensorflow 模型

python - 训练 acc 减少,验证增加。训练损失、验证损失减少

python - 在 3.3 中加载 python 模块时,我用什么代替 PyString_AsString

python - InboundEmailMessage 引发 UnicodeDecodeError 异常

python - 多标签分类的 Tensorflow Argmax 等价物

python - TensorFlow 'global_step' 变量未针对指数衰减进行更新

python - 由于 AdamOptimizer.compute_gradients 的输入大小错误而导致 ValueError