示例
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/