我是机器学习的新手。我正在按照本教程对VGG16模型进行微调。
该模型可以使用以下代码很好地加载:
vgg_model = tensorflow.keras.applications.vgg16.VGG16()
Run Code Online (Sandbox Code Playgroud)
但出现此错误:
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer object at 0x000001FA104CBB70>
Run Code Online (Sandbox Code Playgroud)
运行此代码时:
model = Sequential()
for layer in vgg_model.layers[:-1]:
model.add(layer)
Run Code Online (Sandbox Code Playgroud)
依存关系:
我正在关注此博客,但我想使用VGG16。
任何帮助解决此问题的方法将不胜感激。非常感谢。
Keras 应用程序提供了一些最流行的模型架构的实现,并在一些最流行的数据集上预训练了权重。这些预定义的模型对于类似于模型训练数据集的问题的迁移学习非常方便。
但是,如果我遇到一个非常不同的问题并且想要在新数据集上完全训练模型怎么办?如果我没有预训练的权重,如何使用应用程序中的模型根据我自己的数据集从头开始训练?
我的任务是根据缺陷对种子进行分类。我有 7 个类的大约 14k 图像(它们的大小不相等,有些类有更多照片,有些类有更少)。我尝试从头开始训练 Inception V3,准确率约为 90%。然后我尝试使用带有 ImageNet 权重的预训练模型进行迁移学习。我inception_v3
从applications
没有顶级 fc 层的情况下导入,然后在文档中添加了我自己的层。我以以下代码结束:
# Setting dimensions
img_width = 454
img_height = 227
###########################
# PART 1 - Creating Model #
###########################
# Creating InceptionV3 model without Fully-Connected layers
base_model = InceptionV3(weights='imagenet', include_top=False, input_shape = (img_height, img_width, 3))
# Adding layers which will be fine-tunned
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(7, activation='softmax')(x)
# Creating final model
model = Model(inputs=base_model.input, outputs=predictions)
# …
Run Code Online (Sandbox Code Playgroud) 遵循Pytorch Transfer学习教程之后,我感兴趣的是仅报告训练和测试的准确性以及混淆矩阵(例如使用sklearn混淆矩阵)。我怎样才能做到这一点?当前的教程仅报告火车/ Val的准确性,我很难确定如何在其中合并sklearn混淆矩阵代码。链接到此处的原始教程:https : //pytorch.org/tutorials/beginner/transfer_learning_tutorial.html
%matplotlib inline
from graphviz import Digraph
import torch
from torch.autograd import Variable
# Author: Sasank Chilamkurthy
from __future__ import print_function, division
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy
plt.ion()
data_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': …
Run Code Online (Sandbox Code Playgroud) python confusion-matrix scikit-learn pytorch transfer-learning
我计划根据手头的数据(由人们键入的注释)从头开始训练ELMo或Bert模型。我现在拥有的数据都是由不同的人键入的。拼写,格式和句子不一致都存在问题。阅读ELMo和Bert的论文后,我知道这两个模型都使用了很多类似Wikipedia的句子。我还没有找到Emlo或Bert模型的任何经过处理的训练样本或任何预处理教程。我的问题是:
nlp machine-learning pre-trained-model transfer-learning natural-language-processing
当我们在 Keras2 中进行迁移学习时,参数需要“input_shape”和“input_tensor”。但我只使用 input_tensor 而从未使用过 input_shape。我觉得只用input_tensor就够了,不知道什么时候用input_shape。我应该如何单独使用它们?
我同时使用了 input_tensor 和 input_shape 以及单独的值,并且仅采用 input_tensor 的值并忽略了 input_shape 。
vgg16_model = VGG16(include_top=False, weights='imagenet',
input_tensor = Input(shape=(150, 150, 3)),
input_shape=(224,224,3))
top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dense(1, activation='sigmoid'))
model = Model(input=vgg16_model.input, output=top_model(vgg16_model.output))
model.summary()
Run Code Online (Sandbox Code Playgroud)
Layer (type) Output Shape Param #
================================================================
input_6 (InputLayer) (None, 150, 150, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 150, 150, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 150, 150, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 75, 75, 64) 0
_________________________________________________________________
block2_conv......
Run Code Online (Sandbox Code Playgroud)
我预计这段代码中会出现一些错误,但是没有错误,并且该模型可以接受 (150, …
python machine-learning deep-learning keras transfer-learning
我正在尝试使用 keras 模型进行迁移学习,但一直坚持向模型添加新层。我试过下面的代码:
prev_model = load_model('final_model.h5') # loading the previously saved model.
new_model = Sequential()
new_model.add(prev_model)
new_model.add(Dense(256,activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(1,activation='sigmoid'))
Run Code Online (Sandbox Code Playgroud)
但得到:
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Flatten object at 0x00000000B74364A8>
Run Code Online (Sandbox Code Playgroud)
每当我使用.add()
添加图层时都会发生这种情况。
然后我发现
number_of_layers_to_freeze = 10
vgg_model = VGG16(include_top=False)
for i in range(number_of_layers_to_freeze):
vgg_model.layers[i].trainable = False
vgg_output = vgg_model.outputs[0]
output = keras.layers.Dense(10, activation="softmax")(vgg_output)
model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)
Run Code Online (Sandbox Code Playgroud)
在其他职位。但它导致
AttributeError: 'tuple' object has no attribute 'layer'
Run Code Online (Sandbox Code Playgroud)
我目前正在使用
keras 2.2.5
tensorflow-gpu 1.14.0
Run Code Online (Sandbox Code Playgroud)
是不是版本冲突导致的?
完整回溯 :(AttributeError: …
我尝试在 Keras 中开发 FCN-16 模型。我用类似的 FCN-16 模型权重初始化了权重。
def FCN8 (nClasses, input_height=256, input_width=256):
## input_height and width must be devisible by 32 because maxpooling with filter size = (2,2) is operated 5 times,
## which makes the input_height and width 2^5 = 32 times smaller
assert input_height % 32 == 0
assert input_width % 32 == 0
IMAGE_ORDERING = "channels_last"
img_input = Input(shape=(input_height, input_width, 3)) ## Assume 224,224,3
## Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1', data_format=IMAGE_ORDERING)(
img_input) …
Run Code Online (Sandbox Code Playgroud) computer-vision deep-learning keras tensorflow transfer-learning
考虑迁移学习,以便在 keras/tensorflow 中使用预训练模型。对于每个旧层,trained
参数设置false
为使其权重在训练期间不会更新,而最后一层已被新层替换,并且必须对这些层进行训练。特别是添加了两个带有神经元和 relu 激活函数的全连接隐藏层512
。1024
在这些层之后,使用 Dropout 层rate
0.2
。这意味着在每个训练时期,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 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习 BERT 进行文本分类。我在准备使用 BERT 的数据时发现一些问题。
从我的数据集中,我将情绪和评论分为:
X = df['sentiments']
y = df['reviews'] #it contains four different class of reviews
Run Code Online (Sandbox Code Playgroud)
下一个,
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
train_encodings = tokenizer(X_train, truncation=True, padding=True, max_length=512)
Run Code Online (Sandbox Code Playgroud)
这是我收到错误的地方:
X = df['sentiments']
y = df['reviews'] #it contains four different class of reviews
Run Code Online (Sandbox Code Playgroud)
当我尝试将 X 转换为列表并使用它时,出现另一个错误:
TypeError: TextEncodeInput must be Union[TextInputSequence, Tuple[InputSequence, InputSequence]]
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下问题出在哪里吗?之前我遵循了 20 个新闻数据集的教程,结果很有效。但现在当我在另一个项目中使用它时,它不起作用,我感到很难过。
谢谢。
keras ×7
python ×5
tensorflow ×5
nlp ×2
dropout ×1
finetunning ×1
natural-language-processing ×1
python-3.x ×1
pytorch ×1
scikit-learn ×1
vgg-net ×1