【问题标题】:Python/Keras: LeakyRelu using tensorflowPython/Keras:使用张量流的 LeakyRelu
【发布时间】:2021-08-18 20:35:30
【问题描述】:

我在安装 keras 时遇到问题。以下内容给我带来了太多麻烦(即使在终端上进行更新):

from keras.layers import Dense, Activation
from keras.models import Sequential

因此,我没有使用ann = Sequential() 初始化 ANN,而是使用ann = tf.keras.models.Sequential()。这通过导入:

import tensorflow as tf
from tensorflow import keras

我想使用 LeakyReLU 作为激活函数。然而,这个实现起来似乎有所不同,与其他人的做法相比,keras 文档对我的帮助并没有那么大。

我已经看到需要 ann.add(LeakyReLU(alpha=0.05))。但是,其他参数如 unit 或 input_dim 呢?如何使用我的代码实现这一点?

# Initialising the ANN
ann = tf.keras.models.Sequential()

# Adding the input layer and the first hidden layer
ann.add(tf.keras.layers.Dense(units=32, activation='relu'))

# Adding the second hidden layer
ann.add(tf.keras.layers.Dense(units=32, activation='relu'))

# Adding the output layer
ann.add(tf.keras.layers.Dense(units=1))

【问题讨论】:

    标签: python tensorflow keras deep-learning relu


    【解决方案1】:

    首先你可以使用from tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense, Activation直接导入SequentialDenseActivation

    你可以像这样实现LeakyReLU

    from tensorflow import keras
    
    model = keras.models.Sequential([
        keras.layers.Dense(10),
        keras.layers.LeakyReLU(alpha=0.05)
    ])
    

    您可以在声明keras documentation 中给出的层后指定LeakuReLU 激活函数。

    【讨论】:

      【解决方案2】:

      要在层中使用 LeakyReLU,您可以这样做:

      ann.add(tf.keras.layers.Dense(
        units=32, activation=tf.keras.layers.LeakyReLU(alpha=0.3)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-18
        相关资源
        最近更新 更多