我需要在TensorFlow中实现感知器,但是heaviside (单元步骤)激活似乎在TensorFlow中不可用。它不在tf.
中,不在tf.nn.
中,也不在tf.keras.activations.
中。我猜是因为TensorFlow是基于梯度的库,而heaviside激活没有梯度。
我不知道为什么这个基本功能不存在。有什么解决办法吗?制造一个感知器。
发布于 2020-02-13 19:19:19
TensorFlow没有heaviside (单位台阶)激活功能,可能是因为TF是梯度库,而heaviside没有梯度。我不得不用装饰器@tf.custom_gradient
实现我自己的heaviside
#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;
https://stackoverflow.com/questions/59495271
复制相似问题