我需要在TensorFlow中实现感知器,但是,在TensorFlow中似乎无法使用重组分(单位步长)激活。它不在tf.
中,不在tf.nn.
中,不在tf.keras.activations.
中。我猜是因为TensorFlow是基于梯度的库,而重糖苷激活没有梯度。
我想知道为什么没有此基本功能。任何解决方法?制作感知器。
TensorFlow没有重糖苷(单位步长)激活功能,可能是因为TF是基于梯度的库,而重糖苷没有梯度。我必须使用装饰器@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;