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

时间:2020-06-22 14:51:51

标签: python tensorflow keras

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

here is an axample with cases like st. moris and google.com here.
second sentence.

现在,当我尝试使用X_train [尺寸为(50000,32,32,3)]和y_test(尺寸为(50000,10),

我得到一个错误:

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)

因此由于某种原因,该模型没有意识到其输出形状应该是1x10矢量,并且对10个类进行了一键编码。

如何使尺寸一致?我不完全理解keras在这里期望的输出尺寸。当我执行model.summary()时,逻辑层得出的输出形状应该为(None,1,1,10),在展平时应该给出一个

1 个答案:

答案 0 :(得分:0)

没有顶层的VGG19不会返回完全连接的层,而是返回2D特征空间(我相信是Conv2D / max pooling2d的输出)。您可能想要在VGG后面放置一个展平的平面,这将是最佳的实际选择,因为它将使您的输出形状为(None,10)

否则,您可以

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