【发布时间】:2019-01-15 17:54:39
【问题描述】:
我正在尝试实现和测试我在一篇论文中读到的激活函数。
我正在使用带有 tensorflow 后端的 Keras,我想将激活函数提供给我的模型的 fit 方法。这是函数的数学形式:
我尝试通过两种方式实现这一点:
def function_1(x):
cond1 = tf.greater(x , 2.0)
cond2 = tf.logical_and(tf.less_equal(x, 2.0), tf.greater_equal(x, 0.0))
cond3 = tf.logical_and(tf.less(x, 0.0), tf.greater_equal(x, -2.0))
cond4 = tf.less(x, -2.0)
y = tf.where(cond1, tf.constant(1.0) , tf.where(cond2,
x - 0.25*tf.square(x), tf.where(cond3, x + 0.25*tf.square(x),
tf.where(cond4, tf.constant(-1.0), tf.constant(-1.0)))))
return y
def function_2(x):
cond1 = tf.greater(x , 2.0)
cond2 = tf.logical_and(tf.less_equal(x, 2.0), tf.greater_equal(x, 0.0))
cond3 = tf.logical_and(tf.less(x, 0.0), tf.greater_equal(x, -2.0))
cond4 = tf.less(x, -2.0)
y = tf.case({cond1: lambda x: tf.constant(1.0), cond2: lambda x: x -
0.25*tf.square(x), cond3: lambda x: x + 0.25*tf.square(x),
cond4: lambda x: tf.constant(-1.0)}, exclusive = True)
return y
在这两种情况下,我都会遇到同样的错误:
InvalidArgumentError: Shapes must be equal rank, but are 0 and 2 for 'dense_22/Select' (op: 'Select') with input shapes: [?,5], [], [].
正确的方法是什么?我的代码有什么问题?
【问题讨论】:
标签: python tensorflow keras neural-network activation-function