我有一个训练有素的模型的结果,以numpy输出文件中的Flatten图层结束。 我尝试加载它们并将它们用作Dense图层的输入。
train_data = np.load(open('bottleneck_flat_features_train.npy'))
train_labels = np.array([0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2))
#
validation_data = np.load(open('bottleneck_flat_features_validation.npy'))
validation_labels = np.array([0] * (nb_validation_samples / 2) + [1] * (nb_validation_samples / 2))
#
top_m = Sequential()
top_m.add(Dense(2,input_shape=train_data.shape[1:], activation='sigmoid', name='top_dense1'))
top_m.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
#
top_m.fit(train_data, train_labels,
nb_epoch=nb_epoch, batch_size=my_batch_size,
validation_data=(validation_data, validation_labels))
但是我收到以下错误消息:
ValueError: Error when checking model target: expected top_dense1 to have
shape (None, 2) but got array with shape (13, 1)
我的输入尺寸是(16,1536) - 这个有限的越野跑的16张图像,1536个特征。
>>> train_data.shape
(16, 1536)
致密层应该是一维1536长阵列。
>>> train_data.shape[1]
1536
我该怎么办? 非常感谢!
答案 0 :(得分:1)
你不想编译并使用top_m代替模型吗?
答案 1 :(得分:1)
我发现了我的问题 - 我没有正确定义标签。我已将模型编译切换为稀疏分类交叉熵模式。
我目前的代码是
def train_top_model():
train_data = np.load(open('bottleneck_flat_features_train.npy'))
train_labels = np.array([0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2))
#
validation_data = np.load(open('bottleneck_flat_features_validation.npy'))
validation_labels = np.array([0] * (nb_validation_samples / 2) + [1] * (nb_validation_samples / 2))
#
top_m = Sequential()
top_m.add(Dense(2,input_shape=train_data.shape[1:], activation='softmax', name='top_dense1'))
top_m.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
#
top_m.fit(train_data, train_labels,
nb_epoch=nb_epoch, batch_size=my_batch_size,
validation_data=(validation_data, validation_labels))
现在它起作用并收敛。