Keras ValueError:ValueError:检查目标时出错:预期density_4具有形状(None,2),但数组的形状为(2592,1)Python3 [英] Keras ValueError: ValueError: Error when checking target: expected dense_4 to have shape (None, 2) but got array with shape (2592, 1) Python3

查看:67
本文介绍了Keras ValueError:ValueError:检查目标时出错:预期density_4具有形状(None,2),但数组的形状为(2592,1)Python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Keras 2.0.8,Python 3.6.1和Tensorflow后端中训练模型时遇到问题.

I am having an issue when trying to train my model in Keras 2.0.8, Python 3.6.1, and a Tensorflow Backend.

错误消息:

ValueError:检查目标时出错:预期density_4具有形状(None,2),但数组的形状为(2592,1)

ValueError: Error when checking target: expected dense_4 to have shape (None, 2) but got array with shape (2592, 1)

X_train = numpy.swapaxes(X_train, 1, 3)
X_test = numpy.swapaxes(X_test, 1, 3)

print("X_train shape: ") --> size = (2592, 1, 1366, 96)
print("-----")
print("X_test shape") --> size = (648, 1, 1366, 96)
print("-----")
print(Y_train.shape) --> size = (2592,)
print("-----")
print("Y_test shape") --> size = (648,)

相关代码段:

K.set_image_dim_ordering('th')
K.set_image_data_format('channels_first')

def create_model(weights_path=None):
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same", input_shape=(1, 1366, 96)))
    model.add(Conv2D(64, (3, 3), activation='relu', dim_ordering="th"))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.1))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.1))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(2, activation='softmax'))
    if weights_path:
        model.load_weights(weights_path)
    return model

model = create_model()
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.SGD(lr=0.01),
              metrics=['accuracy'])
history = model.fit(X_train, Y_train,
      batch_size=32,
      epochs=100,
      verbose=1,
      validation_data=(X_test, Y_test))

第142行,我调用model.fit()就是出现此错误的地方

我已尝试解决此错误的内容 引用了这些堆栈溢出帖子:

Things I have tried to fix this error Referenced these stack overflow posts:

我尝试使用以下代码重塑Y_test和Y_train numpy数组:

I tried to reshape the Y_test and Y_train numpy arrays using the following code:

Y_train.reshape(2592, 2)
Y_test.reshape(648, 2)

但是,出现以下错误:

ValueError:无法将大小为2592的数组重塑为形状(2592,2)

ValueError: cannot reshape array of size 2592 into shape (2592,2)

推荐答案

在使用categorical_crossentropy丢失时,必须使用一键编码的标签.为此,您可以使用keras.utils.np_utils

As you are using the categorical_crossentropy loss, you have to use one-hot encoded labels. For this you can use the function to_categorical from keras.utils.np_utils

from keras.utils import np_utils
y_train_onehot = np_utils.to_categorical(y_train)
y_test_onehot = np_utils.to_categorical(y_test)

然后使用一键编码标签来训练您的模型.

Then use the one-hot encoded labels to train your model.

这篇关于Keras ValueError:ValueError:检查目标时出错:预期density_4具有形状(None,2),但数组的形状为(2592,1)Python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆