我正在尝试将转移学习应用于我的ANN
以进行图像分类。
我找到了一个例子,我将对网络进行个性化设置。
这里有主要的代码块:
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])
当我运行上面的代码时,出现此错误:
ValueError: Error when checking target: expected dense_3 to have shape (16,) but got array with shape (1,)
。
我想问题出在dense
层中,是关于尺寸顺序的,我已经尝试过转置它,但是我遇到了同样的错误。
答案 0 :(得分:1)
也许这个简单的例子可以帮助您
import numpy as np
test = np.array([1,2,3])
print(test.shape) # (3,)
test = test[np.newaxis]
print(test.shape) # (1, 3)
尝试在您的[np.newaxis]
输出中应用train_generator
。