python - 如何在迁移学习中使用初始层

标签 python machine-learning deep-learning

我想做迁移学习,我正在加载这些权重文件,但现在我不知道如何使用它的层来训练我的自定义模型。 任何帮助将不胜感激 以下是我尝试过的示例代码:

local_weights_file= '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.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

最佳答案

您需要将最后一层的输出作为最终模型的输入。 像这样的东西应该可以工作

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 a fully connected layer with 1,024 hidden units and ReLU activation
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) 

关于python - 如何在迁移学习中使用初始层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440297/

相关文章:

python - numpy 的 fft 结果的幅度要乘以采样周期?

machine-learning - 句子相似度算法的探讨

machine-learning - 使用 scikit-learn 对文本文档进行分类的交叉验证

machine-learning - FastAI关于使用TextList加载数据的问题

python - 如何使用 QWebView 和 PySide 对 WebKit 页面进行双缓冲?

Python - 将字节数组转换为 JSON 格式

python - 在 Django 中对分页 View 的第一页使用缓存

matlab - 使用 SVM 回归进行预测?

Tensorflow Inception FeedInputs : unable to find feed output input

machine-learning - 如何在内存和批处理方面使用大数据集进行多标签图像分类