由于numpy形状张量流,keras无法训练模型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了由于numpy形状张量流,keras无法训练模型相关的知识,希望对你有一定的参考价值。

我是新手,我正在尝试用Keras训练我的模型。我有14节课。

以下是我的训练和测试数据的形状:

print('train data shape:', X_train.shape)
print('one hot shape:', y_train.shape)
print('one hot shape:', y_test.shape)
print('Number of images in x_train', x_train.shape[0])
print('Number of images in x_test', x_test.shape[0])

输出:

train data shape: (77623, 28, 28, 1)
one hot shape: (77623, 14, 14)
one hot shape: (500, 14, 14)
Number of images in x_train 77623
Number of images in x_test 500

这是我的模型:

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(14, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])


print(model.summary())

型号摘要:

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_58 (Conv2D)           (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_59 (Conv2D)           (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_27 (MaxPooling (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_53 (Dropout)         (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_27 (Flatten)         (None, 9216)              0         
_________________________________________________________________
dense_52 (Dense)             (None, 128)               1179776   
_________________________________________________________________
dropout_54 (Dropout)         (None, 128)               0         
_________________________________________________________________
dense_53 (Dense)             (None, 14)                1806      
=================================================================
Total params: 1,200,398
Trainable params: 1,200,398
Non-trainable params: 0
_________________________________________________________________

这是对fit方法的调用:

history = model.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=0,
          validation_data=(X_test, y_test), callbacks=[TQDMNotebookCallback()])

但我得到这个错误:

Error when checking target: expected dense_53 to have 2 dimensions, but got array with shape (77623, 14, 14)
答案

也许你必须把你的input_shape=(28,28,1),因为你的图像是28x28灰度

另一答案

检查你的输出形状:它应该是(num_samples, classes),而不是(num_samples, 14, 14)

以上是关于由于numpy形状张量流,keras无法训练模型的主要内容,如果未能解决你的问题,请参考以下文章

在 Keras 中训练对象检测模型时出现不兼容张量形状的问题

尝试连接 keras 模型:ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型浮点数)

多层感知器 (MLP) Keras 张量流模型

Keras:ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型列表)

如何计算张量流模型中可训练参数的总数?

如何测试我在真实图片上训练过的张量流模型?