【问题标题】:Heaviside (unit step) activation in TensorFlowTensorFlow 中的 Heaviside(单元步骤)激活
【发布时间】:2020-04-17 02:24:50
【问题描述】:

我需要在 TensorFlow 中实现感知器,但是,在 TensorFlow 中似乎不提供重载(单位步骤)激活。它不在tf.,不在tf.nn.,不在tf.keras.activations.。我猜是因为 TensorFlow 是基于梯度的库,而重质激活没有梯度。

我想知道为什么没有这个基本功能。有什么解决方法吗?做一个感知器。

【问题讨论】:

    标签: tensorflow neural-network perceptron activation


    【解决方案1】:

    TensorFlow 没有 heaviside (unit step) 激活函数可能是因为 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;
    

    【讨论】:

    • 如何实现 BSIZE?
    • @user23657 任何整数值,假设你有 1000 个样本,将整个 epoch 作为批处理,因此 BSIZE=1000,反正它不需要太多 RAM
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多