Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改keras RELU激活功能的阈值_Python_Tensorflow_Keras_Neural Network_Relu - Fatal编程技术网

Python 更改keras RELU激活功能的阈值

Python 更改keras RELU激活功能的阈值,python,tensorflow,keras,neural-network,relu,Python,Tensorflow,Keras,Neural Network,Relu,在构建神经网络时,我试图更改激活函数的阈值Relu 因此,初始代码是下面编写的代码,其中relu阈值的默认值为0 model = Sequential([ Dense(n_inputs, input_shape=(n_inputs, ), activation = 'relu'), Dense(32, activation = 'relu'), Dense(2, activation='softmax') ]) 但是,Keras提供了相同的功能实现,可以参考并添加屏幕截

在构建神经网络时,我试图更改激活函数的阈值
Relu

因此,初始代码是下面编写的代码,其中relu阈值的默认值为0

model = Sequential([
    Dense(n_inputs, input_shape=(n_inputs, ), activation = 'relu'),
    Dense(32, activation = 'relu'),
    Dense(2, activation='softmax')
])
但是,Keras提供了相同的功能实现,可以参考并添加屏幕截图

因此,我将代码更改为以下内容,以传递一个自定义函数,但只得到以下错误

from keras.activations import relu
model = Sequential([
    Dense(n_inputs, input_shape=(n_inputs, ), activation = relu(threshold = 2)), 
    Dense(32, activation = relu(threshold = 2)),
    Dense(2, activation='softmax')
])
错误:
类型错误:relu()缺少1个必需的位置参数:“x”

我知道错误在于我没有在relu函数中使用x,但我无法传递类似的信息。语法要求我编写
model.add(layers.Activation(activations.relu))
,但这样我就无法更改阈值。这就是我需要解决方法或解决方案的地方

然后我使用了如下所述的对我有效的,但我想知道是否有一种方法可以使激活函数实现工作,因为添加层并不总是方便的,我想在稠密函数中进行更多修改

# help(tf.keras.backend.relu)
from tensorflow.keras import backend as K
def relu_advanced(alpha=0.0, max_value=None, threshold=0):        
    def relu_plus(x):
        return K.relu(x, 
                      alpha = tf.cast(alpha, tf.float32), 
                      max_value = max_value,
                      threshold= tf.cast(threshold, tf.float32))
    return relu_plus
对我有效的代码:-

from keras.layers import ReLU
model = Sequential([
    Dense(n_inputs, input_shape=(n_inputs, )),
    ReLU(threshold=4), 
    Dense(32),
    ReLU(threshold=4),
    Dense(2, activation='softmax')
])

你面临的错误是合理的。但是,您可以在
relu
函数中使用以下技巧来完成工作。通过这种方式,您可以定义一个函数,该函数采用必要的参数,例如
alpha
threshold
等,并且在函数体中,您可以定义另一个函数,该函数使用这些参数计算
relu激活,最后返回到上层函数

# help(tf.keras.backend.relu)
from tensorflow.keras import backend as K
def relu_advanced(alpha=0.0, max_value=None, threshold=0):        
    def relu_plus(x):
        return K.relu(x, 
                      alpha = tf.cast(alpha, tf.float32), 
                      max_value = max_value,
                      threshold= tf.cast(threshold, tf.float32))
    return relu_plus
样本:

foo = tf.constant([-10, -5, 0.0, 5, 10], dtype = tf.float32)
tf.keras.activations.relu(foo).numpy()
array([ 0.,  0.,  0.,  5., 10.], dtype=float32)

x = relu_advanced(threshold=1)
x(foo).numpy()
array([-0., -0.,  0.,  5., 10.], dtype=float32)
对于您的情况,只需按如下方式使用:

model = Sequential([
    Dense(64, input_shape=(32, ), activation = relu_advanced(threshold=2)), 
    Dense(32, activation = relu_advanced(threshold=2)),
    Dense(2, activation='softmax')
])