gpt4 book ai didi

python - Tensorflow 相当于 numpy.diff

转载 作者:太空狗 更新时间:2023-10-29 18:26:09 26 4
gpt4 key购买 nike

有没有等价于numpy.diff的tensorflow ?

Calculate the n-th discrete difference along given axis.

对于我的项目,我只需要 n=1

最佳答案

试试这个:

def tf_diff_axis_0(a):
return a[1:]-a[:-1]

def tf_diff_axis_1(a):
return a[:,1:]-a[:,:-1]

检查:

import numpy as np
import tensorflow as tf

x0=np.arange(5)+np.zeros((5,5))
sess = tf.Session()
np.diff(x0, axis=0) == sess.run(tf_diff_axis_0(tf.constant(x0)))
np.diff(x0, axis=1) == sess.run(tf_diff_axis_1(tf.constant(x0)))

关于python - Tensorflow 相当于 numpy.diff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42609618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com