转移学习,错误的致密层形状 [英] Transfer learning, wrong dense layer's shape

查看:100
本文介绍了转移学习,错误的致密层形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将转移学习应用于我的ANN以进行图像分类. 我找到了一个例子,我将对网络进行个性化设置.

I am trying to apply transfer learning to my ANN for image classification. I have found an example of it, and I would personalize the network.

这里有主要的代码块:

model = VGG19(weights='imagenet',
                  include_top=False,
                  input_shape=(224, 224, 3))
batch_size = 16

for layer in model.layers[:5]:
    layer.trainable = False

x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(16, activation="sigmoid")(x)

model_final = Model(input = model.input, output = predictions)

model_final.fit_generator(
train_generator,
samples_per_epoch = nb_train_samples,
epochs = epochs,
validation_data = validation_generator,
validation_steps = nb_validation_samples,
callbacks = [checkpoint, early])

当我运行上面的代码时,出现此错误:

When I run the code above I get this error:

ValueError: Error when checking target: expected dense_3 to have shape (16,) but got array with shape (1,).

我想问题出在dense层中尺寸的顺序有关,我曾尝试对它进行转置,但是我遇到了同样的错误.

I suppose that the problem is about the dimensions' order in the dense layer, I have tried to transpose it, but I get the same error.

推荐答案

也许这个简单的示例可以帮助您:

Maybe this simple example can help:

import numpy as np

test = np.array([1,2,3])
print(test.shape) # (3,)

test = test[np.newaxis]
print(test.shape) # (1, 3)  

尝试在您的train_generator输出中应用[np.newaxis].

Try apply [np.newaxis] in your train_generator output.

这篇关于转移学习,错误的致密层形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆