num_classes=10
(d_tr,l_tr),(d_val,l_val) = mnist.load_data()
l_tr = to_categorical(l_tr,num_classes)
l_val = to_categorical(l_val,num_classes)
d_tr = np.reshape(d_tr,(len(d_tr),28,28,1))
d_val = np.reshape(d_val,(len(d_val),28,28,1))
#d_tr = np.reshape(d_tr,(len(d_tr),784))
#d_val = np.reshape(d_val,(len(d_val),784))
print(l_tr.shape)
print(l_val.shape)
print(d_tr.shape)
print(d_val.shape)
m_0 = Input(shape=(28,28,1,))
m = Conv2D(25,(3,3),input_shape=(28,28,1),padding="same")(m_0)
m = Dropout(.3)(m)
m = Conv2D(40,(3,3),strides=2,padding="same")(m)
m = Dropout(.4)(m)
m = Conv2D(60,(3,3),kernel_regularizer=l2(.025),padding="same")(m)
m = Dropout(.4)(m)
m = Conv2D(60,(3,3),kernel_regularizer=l2(.025),strides=2,padding="same")(m)
m = BatchNormalization()(m)
m = Flatten()(m)
m = Dropout(.45)(m)
out = Dense(num_classes,activation="softmax")(m)
model = Model(inputs=m_0,outputs=out)
model.summary()
model.compile("adam",metrics=["accuracy"],losses="categorical_crossentropy")
model.fit(x=d_tr,y=l_tr,epochs=20,batch_size=64,validation_data=(d_val,l_val),callbacks=[CSVLogger('log_mnist.csv')])
我正在尝试使用功能性API(上面提供的代码)为MNIST创建一个简单的模型。 我试图找出我做错了什么,但找不到任何解决以下错误的方法:
ValueError: ('Error when checking model target: expected no data, but got:', array([[0., 0., 0., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.]], dtype=float32))
编辑:事实证明,当您将model.compile中的“ losss”关键字设置为“ loss”时,可以解决此问题。但是,如果不是关键字,为什么解释器会接受损失,我仍然不知道。如果您有任何信息,请回答。