Tensorflow,平方根激活函数实现(整形误差)

Tensorflow, square root activation function implementation (shaping error)

为了实现分类神经网络,我找到了一些非常有用的教程,比如 this one (2 hidden layer, one-hot-encoding output, dropout regularization, normalization etc.) which helped me with a bit of the learning curve behind Tensorflow API. However, by reading the publication on SQRT activation functions,看到乐观的反馈,我想在我的神经网络架构中进行试验。

在 Tensorflow API 中没有找到它之后,我查看了如何定义自定义激活函数,找到了 ,并认为它 'should be possible' 可以用 Tensorflow 原语实现.

所以如果 SQRT 激活函数需要这样(请原谅粘贴,看起来比我自己打出来的更好):

我插入了这段代码而不是隐藏层 ReLU 函数:

# ==== old activation function
# b = bias value x bias weight
# inputs = x data 
# w = weights
y = tf.nn.relu( tf.add( tf.matmul(w, tf.transpose(inputs)), b))

# ===== new act function
net = tf.cast( tf.add( tf.matmul(w, tf.transpose(inputs)), b), tf.float32)  # net input to activation function
cond = tf.greater_equal(net, tf.constant(0.0, dtype=tf.float32))            # >= condition
condTrue = tf.sqrt(net)                                   # if True
minOne = tf.constant(-1.0, shape=(N,1) dtype=tf.float32)  # -1 constant value
condFalse = tf.matmul(minOne, tf.sqrt( tf.abs(net)))      # if False
y = tf.cond(cond, lambda: condTrue, lambda: condFalse)    # act. function output 

但是如果我尝试 运行 这段代码,我会得到一个整形错误:

ValueError("Dimensions must be equal, but are 1 and 107 for 'MatMul_2' (op: 'MatMul') with input shapes: [107,1], [107,?].",)

有人可以看看代码片段并告诉我我的方法是否正确吗?除了指出输入之间的等级问题的错误外,我怀疑我更大的问题仍然是理解和围绕基于矩阵的张量流运算符。

在所有乘法、加法和转置之间,我忘记了张量所需的基础数据形状必须是什么。我的代码是否会正确定义预期的激活函数(以及 back-prop 导数呢?),如果没有,请描述我在哪里以及如何出错?

任何帮助将不胜感激,我想更好地理解这个问题(因为我还在学习API)

您可以使用更简单的激活函数实现逻辑:

x = tf.constant([ -4, 4, -2, 2, 0], tf.float32)
act = tf.sign(x)* tf.sqrt(tf.abs(x))

with tf.Session() as sess:
   print(sess.run(act))

#[-2.  2. -1.4142135 1.4142135 0. ]