2017-08-19 62 views
0

在我CNN主動函數的形式爲:如何在TensorFlow中用Python創建分段激活函數?

abs(X)< tou f = 1.716tanh(0.667x) 
x >= tou  f = 1.716[tanh(2tou/3)+tanh'(2tou/3)(x-tou)] 
x <= -tou f = 1.716[tanh(-2tou/3)+tanh'(-2tou/3)(x+tou)] 

tou是一個常數。

所以,在TensorFlow中可以創建自己的激活函數。我不想用C++編寫它,並重新編譯整個TensorFlow。

如何使用TensorFlow中的可用功能來實現它?

+0

三個條件似乎不一致。例如,當x == tou時會發生什麼? –

+0

第三種情況是錯誤的。我已經編輯過了。請嘗試。謝謝〜 –

回答

0

在tensorflow很容易寫自己的激活功能,如果它包括已經存在ops,作爲你的情況下,你可以使用tf.case

f = tf.case({tf.less(tf.abs(x), tou): lambda: 7.716 * tf.tanh(0.667 * x), 
     tf.greater_equal(x, tou): lambda: 1.716 * tf.tanh(2 * tou/3) + 1.716 * tf.tanh(2 * tou/3) * (x - tou)}, 
     default=lambda: 1.716 * tf.tanh(-2 * tou/3) + 1.716 * tf.tanh(-2 * tou/3) * (x + tou), exclusive=True) 
+0

第三個條件是錯誤的。我已經編輯過了。請看一看。謝謝〜 –

+0

根據新的條件編輯了我的答案。 –