Python 如何在tensorflow中实现指标功能?

Python 如何在tensorflow中实现指标功能?,python,tensorflow,Python,Tensorflow,我想实现这样一个函数:如果x==k,f(x)=1,否则f(x)=0(k是一个参数)。所以我使用了tf.equal和tf.cast,我的代码如下: 将tensorflow导入为tf a=范围(12) a=tf.变量(a) b=6 b=tf.变量(b) a=tf.重塑(a[3,4]) sess=tf.Session() init=tf.global_variables_initializer() sess.run(初始化) c=tf.相等(a,b) d=tf.cast(c,tf.int32) 打印s

我想实现这样一个函数:如果x==k,f(x)=1,否则f(x)=0(k是一个参数)。所以我使用了tf.equal和tf.cast,我的代码如下:

将tensorflow导入为tf
a=范围(12)
a=tf.变量(a)
b=6
b=tf.变量(b)
a=tf.重塑(a[3,4])
sess=tf.Session()
init=tf.global_variables_initializer()
sess.run(初始化)
c=tf.相等(a,b)
d=tf.cast(c,tf.int32)
打印sess.run(c)
打印sess.run(d)

看起来不错,但问题是tf.梯度(d,a)和tf.梯度(d,b)都没有。我尝试了tf.梯度(c,a)并得到了TypedError。有没有合适的方法来实现这个功能?

我甚至不确定这里是否定义了渐变

如果a=b,则指示器功能为f(a,b)=1,否则为0。远离a=b,该函数为常数(零),因此具有零导数。在a=b的任何点上,函数都是不连续的,因此这里没有导数


更直观地说:导数不存在于函数中有“跳跃”的地方。

我甚至不确定这里是否定义了梯度

如果a=b,则指示器功能为f(a,b)=1,否则为0。远离a=b,该函数为常数(零),因此具有零导数。在a=b的任何点上,函数都是不连续的,因此这里没有导数


更直观地说:在函数中有“跳跃”的地方不存在导数。

可以用正态分布的PDF来近似指示函数。我也是TensorFlow的新手,所以请随时指出任何问题

##I am using tensorflow2
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import tensorflow_probability as tfp 

a = tf.range(12)
a = tf.Variable(a)
b = 6
b = tf.Variable(b)
a = tf.reshape(a, [3, 4])

## Define the PDF of a normal distribution to approximate the indicator function
dist = tfp.distributions.Normal(0., 0.1)
scalar = dist.prob(0) # a normalization constant
#since the pdf at data zero is not one

## Implement the approximazed indicator function
a = tf.cast(a, dtype= tf.float32)
b = tf.cast(b, dtype= tf.float32)
c = dist.prob(a-b)/scalar
#d = tf.cast(c, tf.int32)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(c))

## calcualte the gradient
c_a = tf.gradients(c, a) 
print(sess.run(c_a))

可以使用正态分布的PDF来近似指示函数。我也是TensorFlow的新手,所以请随时指出任何问题

##I am using tensorflow2
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import tensorflow_probability as tfp 

a = tf.range(12)
a = tf.Variable(a)
b = 6
b = tf.Variable(b)
a = tf.reshape(a, [3, 4])

## Define the PDF of a normal distribution to approximate the indicator function
dist = tfp.distributions.Normal(0., 0.1)
scalar = dist.prob(0) # a normalization constant
#since the pdf at data zero is not one

## Implement the approximazed indicator function
a = tf.cast(a, dtype= tf.float32)
b = tf.cast(b, dtype= tf.float32)
c = dist.prob(a-b)/scalar
#d = tf.cast(c, tf.int32)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(c))

## calcualte the gradient
c_a = tf.gradients(c, a) 
print(sess.run(c_a))

我同意,但是像max pooling和relu一样,我只想将梯度反向传播到一些变量。我明白你的观点——确实,relu(x)的梯度没有定义为x=0。但它是在它的任一侧定义的:d relu/dx=1表示x>0,0表示x<0。如果x>y,则d max(x,y)/dx=1,如果xR。我认为技术上它可能是一个分布,因为它有一个积分。我同意,但就像max pooling和relu一样,我只想把梯度反向传播到一些变量,我明白你的意思了——确实,relu(x)的梯度没有定义在x=0。但它是在它的任一侧定义的:d relu/dx=1表示x>0,0表示x<0。如果x>y,则d max(x,y)/dx=1,如果xR。我认为技术上它可能是一个分布,因为它有一个积分。它在Tensorflow1中工作。然后我还尝试在Tensorflow2中实现,但它返回“none”。不确定原因…它在Tensorflow1中工作。然后我还尝试在Tensorflow2中实现,但它返回“none”。不确定原因。。。