提问者:小点点

我如何强制我的训练数据与我的神经网络的输出形状相匹配?


我尝试在使用VGG19的Keras.Applications上使用迁移学习示例。 我试着在cifar10数据集上训练,所以10个类。 我的模型(在概念上)很简单,因为它只是VGG19减去最上面的三层,然后是一些额外的可以训练的层。

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[dimensione(50000,32,32,3)]和y_test[dimensione(50000,10)]进行训练时

我得到一个错误

“ValueError:检查目标时出错:Logistic应具有4个维度,但得到形状为(50000,10)的数组”

因此,由于某种原因,模型没有意识到它的输出形状应该是一个10x1的矢量,对10个类进行one-hot编码。

我怎样才能使它的尺寸一致呢? 我不完全理解keras在这里期望的输出维度。 当我使用model.summary()时,Logistic层产生的输出形状应该是(None,1,1,10),当它被平坦化时,应该只给出我感兴趣的向量。 但是我不是很确定我如何平坦输出以强制给定的输出大小,或者改变我的训练数据大小以匹配输出张量的大小。


共1个答案

匿名用户

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

否则,你可以

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