首页 > 解决方案 > 如何强制我的训练数据匹配我的神经网络的输出形状?

问题描述

我正在尝试使用 VGG19 在keras.applications上使用迁移学习示例。我正在尝试在 cifar10 数据集上进行训练,所以有 10 个课程。我的模型(在概念上)很简单,因为它只是 VGG 19 减去前三层,然后是一些可训练的额外层。

import tensorflow as tf
from keras.utils import to_categorical
from keras.applications import VGG19
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D, Input
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

#%%
# Specify input and number of classes
input_tensor = Input(shape=(32, 32, 3))
num_classes=10
#Load the data (cifar100), if label mode is fine then 100 classes
(X_train,y_train),(X_test,y_test)=tf.keras.datasets.cifar10.load_data()
#One_Hot_encode y data
y_test=to_categorical(y_test,num_classes=num_classes,dtype='int32')
y_train=to_categorical(y_train,num_classes=num_classes,dtype='int32')
#%%
# create the base pre-trained model
base_model = VGG19(weights='imagenet', include_top=False,
                   input_tensor=input_tensor)

# Add a fully connected layer and then a logistic layer
x = base_model.output
# # let's add a fully-connected layer
x = Dense(1024, activation='relu',name='Fully_Connected')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(num_classes, activation='softmax',name='Logistic')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])


# train the model on the new data for a few epochs
model.fit(X_train,y_train,epochs=10)

#%%
model.evaluate(X_test,y_test)

现在,当我尝试使用 X_train [(50000, 32, 32, 3) 的维度] 和 y_test ((50000,10) 的维度进行训练时,

我收到一个错误:

ValueError: Error when checking target: expected Logistic to have 4 
dimensions, but got array with shape (50000, 10)

因此,出于某种原因,该模型没有意识到其输出形状应该是一个 1x10 向量,并且对 10 个类具有 one-hot 编码。

我怎样才能使尺寸一致?我不完全理解 keras 在这里期望的输出尺寸。当我执行 model.summary() 时,Logistic 层产生的输出形状应该是 (None, 1, 1, 10),当展平时应该只给出一个

标签: pythontensorflowkeras

解决方案


没有顶层的 VGG19 不会返回全连接层,而是返回 2D 特征空间(我相信是 Conv2D/max pooling2d 的输出)。您可能希望在 VGG 之后放置一个 flatten,这将是最好的实际选择,因为它会使您的输出 shape (None,10)

否则,你可以做

y_train = np.reshape(y_train, (50000,1,1,10))

推荐阅读