TensorFlow 中的 Heaviside(单位步长)激活
Heaviside (unit step) activation in TensorFlow
我需要在 TensorFlow 中实现一个感知器,但是,heaviside(单位步长)激活似乎在 TensorFlow 中不可用。它不在 tf.
中,不在 tf.nn.
中,不在 tf.keras.activations.
中。我猜是因为 TensorFlow 是基于梯度的库,而 heaviside 激活没有梯度。
我想知道为什么没有这个基本功能。有什么解决方法吗?做一个感知器。
TensorFlow没有heaviside(单位步长)激活函数可能是因为TF是基于梯度的库,heaviside没有梯度。我必须使用装饰器 @tf.custom_gradient
:
来实现我自己的重边
#Heaviside (Unit Step) function with grad
@tf.custom_gradient
def heaviside(X):
List = [];
for I in range(BSIZE):
Item = tf.cond(X[I]<0, lambda: tf.constant([0], tf.float32),
lambda: tf.constant([1], tf.float32));
List.append(Item);
U = tf.stack(List);
#Heaviside half-maximum formula
#U = (tf.sign(X)+1)/2;
#Div is differentiation intermediate value
def grad(Div):
return Div*1; #Heaviside has no gradient, use 1.
return U,grad;
我需要在 TensorFlow 中实现一个感知器,但是,heaviside(单位步长)激活似乎在 TensorFlow 中不可用。它不在 tf.
中,不在 tf.nn.
中,不在 tf.keras.activations.
中。我猜是因为 TensorFlow 是基于梯度的库,而 heaviside 激活没有梯度。
我想知道为什么没有这个基本功能。有什么解决方法吗?做一个感知器。
TensorFlow没有heaviside(单位步长)激活函数可能是因为TF是基于梯度的库,heaviside没有梯度。我必须使用装饰器 @tf.custom_gradient
:
#Heaviside (Unit Step) function with grad
@tf.custom_gradient
def heaviside(X):
List = [];
for I in range(BSIZE):
Item = tf.cond(X[I]<0, lambda: tf.constant([0], tf.float32),
lambda: tf.constant([1], tf.float32));
List.append(Item);
U = tf.stack(List);
#Heaviside half-maximum formula
#U = (tf.sign(X)+1)/2;
#Div is differentiation intermediate value
def grad(Div):
return Div*1; #Heaviside has no gradient, use 1.
return U,grad;