【问题标题】:Transfer learning, wrong dense layer's shape迁移学习,错误的密集层形状
【发布时间】:2019-02-13 00:13:19
【问题描述】:

我正在尝试将迁移学习应用于我的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 层中的尺寸顺序,我尝试转置它,但我得到了同样的错误。

【问题讨论】:

    标签: python keras keras-layer


    【解决方案1】:

    也许这个简单的例子会有所帮助:

    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]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2021-08-17
      相关资源
      最近更新 更多