首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对具有比预训练分类器更少的目标标签的数据的迁移学习

对具有比预训练分类器更少的目标标签的数据的迁移学习
EN

Stack Overflow用户
提问于 2020-08-18 21:17:21
回答 2查看 63关注 0票数 0

假设有一个预先训练的模型(base_model),该模型已经使用大型数据集进行了训练,以预测7种人类情绪,例如

代码语言:javascript
复制
'Anger', 'Disgust', 'Fear', 'Happiness', 'Sadness', 'Surprise','Neutral'

现在,为了建立一个迁移学习模型,我将删除最后一层"base_model",冻结它们的权重,使它们不可训练,然后添加一个我自己的可训练的微调图层。

我想知道如何在一个较小的数据集上训练这个新编译的模型"model_finetuned“,该数据集只包含7种情绪中的3种。

代码语言:javascript
复制
'Anger', 'Sadness', 'Surprise'

任何以Python代码形式提供的帮助或建议都将不胜感激。提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2020-08-18 22:05:36

正如您正确解释的那样,您可以冻结预定义的模型宽度,并在模型的末尾添加完全连接的层进行微调。

有两种方法可以利用预先训练的网络:特征提取和微调。

  • Feature extraction:包括使用先前网络学习的表示从新样本中提取感兴趣的特征。然后通过一个新的分类器运行这些特征,该分类器是从头开始训练的。(Cold是最后一个完全连接的层)

  • Fine-tuning:包括解冻用于特征提取的冻结模型库的几个顶层,并对模型的新添加部分进行联合训练。

使用预先训练好的vgg16的示例:

代码语言:javascript
复制
#Load pretrained vgg16 network
from torchvision.models import vgg16

num_classes = 3
pretrained_model = vgg16(pretrained=True)
pretrained_model.eval()
pretrained_model.to(device)

#Extracting the first part of the model
feature_extractor = pretrained_model.features

#Define feature classifier
feature_classifier = nn.Sequential(
nn.Linear(4*4*512,256),
nn.ReLU(),
nn.Linear(256, num_classes))

#
model = nn.Sequential(
feature_extractor,
nn.Flatten(),
feature_classifier)

正如您所看到的,您必须在最后一个完全连接的层中指定模型的输出。在您的情况下将是(num_classes = 3)。

票数 1
EN

Stack Overflow用户

发布于 2020-08-19 06:09:08

这是我几天前使用Tensorflow Keras处理的代码示例

代码语言:javascript
复制
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Sequential
from tensorflow.python.keras.layers import Dense

num_classes= 3

# Include the path of the weights for the pretrained model
resnet_weights_path='imagenet'

# Create your model
model= Sequential()

# Include the pre-trained model. In this case, ResNet50
model.add(ResNet50(include_top=False,pooling='avg',weights=resnet_weights_path ))

# Add as many extra layers as you need, according to you problem
# You can also try it directly

# Add the final layer that makes predictions. Suit yourself with the activation function 
model.add(Dense(num_classes,activation='softmax'))

# Don't train the pre-trained model
model.layers[0].trainable=False

# Compile your model according to your needs
model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])

现在你的模型已经准备好接受训练了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63469500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档