Tensorflow 相当于 numpy.diff [英] Tensorflow equivalent to numpy.diff

查看:29
本文介绍了Tensorflow 相当于 numpy.diff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有与 numpy.diff?

计算沿给定轴的第 n 个离散差.

Calculate the n-th discrete difference along given axis.

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

For my project I only need 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)))

这篇关于Tensorflow 相当于 numpy.diff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆