python - Tensorflow 中哪些层受 dropout 层的影响?

考虑迁移学习,以便在 keras/tensorflow 中使用预训练模型。对于每个旧层,trained 参数设置为 false,这样它的权重在训练期间不会更新,而最后一层已被新层替换,这些必须受过训练。特别是添加了两个具有 5121024 神经元的完全连接的隐藏层以及 relu 激活函数。在这些层之后,使用 rate 0.2 的 Dropout 层。这意味着在每个训练时期 20% 的神经元被随机丢弃。

这个 dropout 层影响哪些层?它会影响所有网络,包括已设置 layer.trainable=false 的预训练层,还是只影响新添加的层?或者它只影响前一层(即具有 1024 个神经元的层)?

换句话说,在每个时期被 dropout 关闭的神经元属于哪一层?

import os

from tensorflow.keras import layers
from tensorflow.keras import Model
  
from tensorflow.keras.applications.inception_v3 import InceptionV3

local_weights_file = 'weights.h5'

pre_trained_model = InceptionV3(input_shape = (150, 150, 3), 
                                include_top = False, 
                                weights = None)

pre_trained_model.load_weights(local_weights_file)

for layer in pre_trained_model.layers:
  layer.trainable = False
  
# pre_trained_model.summary()

last_layer = pre_trained_model.get_layer('mixed7')
last_output = last_layer.output

# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add two fully connected layers with 512 and 1,024 hidden units and ReLU activation
x = layers.Dense(512, activation='relu')(x)
x = layers.Dense(1024, activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)                  
# Add a final sigmoid layer for classification
x = layers.Dense  (1, activation='sigmoid')(x)           

model = Model( pre_trained_model.input, x) 

model.compile(optimizer = RMSprop(lr=0.0001), 
              loss = 'binary_crossentropy', 
              metrics = ['accuracy'])

最佳答案

dropout层会影响上一层的输出。

如果我们查看您代码的特定部分:

x = layers.Dense(1024, activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)                  
# Add a final sigmoid layer for classification
x = layers.Dense  (1, activation='sigmoid')(x)  

在您的情况下,x = layers.Dense(1024, activation='relu')(x) 定义的层输出的 20% 将在传递之前随机丢弃到最后的 Dense 层。

https://stackoverflow.com/questions/63738681/

相关文章:

clojure - "let"的功能替代

python - 如何使用 numba 在 GPU 上推广快速矩阵乘法

swift - 在 RealityKit 中启用手势

scala - 为什么案例模式匹配匿名函数可以有两个签名,并且具有神奇的调用站点兼容性

python - 变量和参数有什么区别

swift - iOS-14 netServiceBrowser.searchForServices

Haskell - 如何将矩阵(二维数组)分成几组

reactjs - 使用 `new` 关键字作为参数的 useState

r - 按列计算唯一值

python - 如何将字符串拆分为大小相等的部分?