我正在尝试编码MNIST。它具有包含图像像素数据信息的数据集。
此代码中有尺寸相关的问题。 我是python的初学者,不知道如何解决尺寸问题。
from keras.datasets import mnist
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()
from keras import models
from keras import layers
network=models.Sequential()
network.add(layers.Dense(512,activation='relu',input_shape=(28*28,)))
network.add(layers.Dense(10,activation='softmax'))
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
train_images=train_images.reshape((60000,28*28))
train_images=train_images.astype('float32')/255
test_images=test_images.reshape((10000,28*28))
test_images=test_images.astype('float32')/255
from keras.utils import to_categorical
train_labels=to_categorical(train_labels)
test_labels=to_categorical(train_labels)
network.fit(train_images, train_labels, epochs=10,batch_size=128)
test_loss,test_acc=network.evaluate(test_images,test_labels)
我得到的错误:
ValueError:检查目标时出错:预期density_2具有2 尺寸,但数组的形状为(60000,10,2)
如何解决? 我没有得到解决方案。请帮助我。
答案 0 :(得分:0)
传递给to_categorical()
的参数是导致出现上述错误的原因。所以,尝试更改
train_labels=to_categorical(train_labels)
test_labels=to_categorical(train_labels)
到
train_labels=to_categorical(train_labels, 10)
test_labels=to_categorical(test_labels, 10)
答案 1 :(得分:0)
尝试以这种方式进行操作,而无需重塑test_images
我将其重命名只是为了约定
from keras import models
from keras import layers
from keras.datasets import mnist
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape((60000, 28 * 28))
x_train = x_train.astype('float32') / 255
y_train = to_categorical(y_train)
# model
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
print(x_train.ndim) # 2
print(x_train.shape) # (60000, 784)
print(x_test.ndim) # 3
print(x_test.shape) # (10000, 28, 28)
print(y_train.ndim) # 2
print(y_train.shape) # (60000, 10)
print(y_test.ndim) # 1
print(y_test.shape) # (10000,)
network.fit(x_train, y_train, epochs=10, batch_size=128)
输出
128/60000 [..............................] - ETA: 2:39 - loss: 2.3697 - acc: 0.1406
1152/60000 [..............................] - ETA: 20s - loss: 1.2849 - acc: 0.6285
2560/60000 [>.............................] - ETA: 10s - loss: 0.9101 - acc: 0.7441
3968/60000 [>.............................] - ETA: 7s - loss: 0.7705 - acc: 0.7815
5248/60000 [=>............................] - ETA: 5s - loss: 0.6864 - acc: 0.8043
6528/60000 [==>...........................] - ETA: 4s - loss: 0.6268 - acc: 0.8202
7808/60000 [==>...........................] - ETA: 4s - loss: 0.5903 - acc: 0.8295
9216/60000 [===>..........................] - ETA: 3s - loss: 0.5513 - acc: 0.8409
10496/60000 [====>.........................] - ETA: 3s - loss: 0.5221 - acc: 0.8491
11904/60000 [====>.........................] - ETA: 3s - loss: 0.4945 - acc: 0.8576
13312/60000 [=====>........................] - ETA: 3s - loss: 0.4764 - acc: 0.8629
14592/60000 [======>.......................] - ETA: 2s - loss: 0.4584 - acc: 0.8682
16000/60000 [=======>......................] - ETA: 2s - loss: 0.4428 - acc: 0.8724
17408/60000 [=======>......................] - ETA: 2s - loss: 0.4298 - acc: 0.8758
18816/60000 [========>.....................] - ETA: 2s - loss: 0.4181 - acc: 0.8792
20224/60000 [=========>....................] - ETA: 2s - loss: 0.4058 - acc: 0.8828
21120/60000 [=========>....................] - ETA: 2s - loss: 0.3996 - acc: 0.8847
21888/60000 [=========>....................] - ETA: 2s - loss: 0.3934 - acc: 0.8865
22784/60000 [==========>...................] - ETA: 2s - loss: 0.3856 - acc: 0.8889
23808/60000 [==========>...................] - ETA: 2s - loss: 0.3799 - acc: 0.8907
24960/60000 [===========>..................] - ETA: 1s - loss: 0.3734 - acc: 0.8925
26368/60000 [============>.................] - ETA: 1s - loss: 0.3649 - acc: 0.8951
27776/60000 [============>.................] - ETA: 1s - loss: 0.3577 - acc: 0.8968
29184/60000 [=============>................] - ETA: 1s - loss: 0.3513 - acc: 0.8990
30464/60000 [==============>...............] - ETA: 1s - loss: 0.3461 - acc: 0.9007
31872/60000 [==============>...............] - ETA: 1s - loss: 0.3391 - acc: 0.9023
33280/60000 [===============>..............] - ETA: 1s - loss: 0.3336 - acc: 0.9037
34688/60000 [================>.............] - ETA: 1s - loss: 0.3280 - acc: 0.9051
35968/60000 [================>.............] - ETA: 1s - loss: 0.3231 - acc: 0.9065
37248/60000 [=================>............] - ETA: 1s - loss: 0.3188 - acc: 0.9078
38528/60000 [==================>...........] - ETA: 1s - loss: 0.3131 - acc: 0.9095
39936/60000 [==================>...........] - ETA: 1s - loss: 0.3081 - acc: 0.9109
41216/60000 [===================>..........] - ETA: 0s - loss: 0.3034 - acc: 0.9123
42496/60000 [====================>.........] - ETA: 0s - loss: 0.2993 - acc: 0.9134
43648/60000 [====================>.........] - ETA: 0s - loss: 0.2960 - acc: 0.9145
44544/60000 [=====================>........] - ETA: 0s - loss: 0.2929 - acc: 0.9154
45312/60000 [=====================>........] - ETA: 0s - loss: 0.2900 - acc: 0.9162
46208/60000 [======================>.......] - ETA: 0s - loss: 0.2872 - acc: 0.9170
46976/60000 [======================>.......] - ETA: 0s - loss: 0.2859 - acc: 0.9174
48000/60000 [=======================>......] - ETA: 0s - loss: 0.2831 - acc: 0.9180
49280/60000 [=======================>......] - ETA: 0s - loss: 0.2800 - acc: 0.9190
50560/60000 [========================>.....] - ETA: 0s - loss: 0.2768 - acc: 0.9197
51840/60000 [========================>.....] - ETA: 0s - loss: 0.2749 - acc: 0.9203
53120/60000 [=========================>....] - ETA: 0s - loss: 0.2719 - acc: 0.9211
54400/60000 [==========================>...] - ETA: 0s - loss: 0.2692 - acc: 0.9219
55808/60000 [==========================>...] - ETA: 0s - loss: 0.2661 - acc: 0.9227
57216/60000 [===========================>..] - ETA: 0s - loss: 0.2634 - acc: 0.9236
58496/60000 [============================>.] - ETA: 0s - loss: 0.2607 - acc: 0.9244
59904/60000 [============================>.] - ETA: 0s - loss: 0.2579 - acc: 0.9253
60000/60000 [==============================] - 3s 48us/step - loss: 0.2576 - acc: 0.9254
Epoch 2/10
...
Epoch 10/10
128/60000 [..............................] - ETA: 2s - loss: 0.0089 - acc: 0.9922
1280/60000 [..............................] - ETA: 2s - loss: 0.0095 - acc: 0.9961
2560/60000 [>.............................] - ETA: 2s - loss: 0.0071 - acc: 0.9977
3840/60000 [>.............................] - ETA: 2s - loss: 0.0079 - acc: 0.9977
4992/60000 [=>............................] - ETA: 2s - loss: 0.0077 - acc: 0.9976
6272/60000 [==>...........................] - ETA: 2s - loss: 0.0073 - acc: 0.9976
7552/60000 [==>...........................] - ETA: 2s - loss: 0.0074 - acc: 0.9975
8448/60000 [===>..........................] - ETA: 2s - loss: 0.0073 - acc: 0.9974
9728/60000 [===>..........................] - ETA: 2s - loss: 0.0079 - acc: 0.9972
11008/60000 [====>.........................] - ETA: 2s - loss: 0.0088 - acc: 0.9970
12160/60000 [=====>........................] - ETA: 2s - loss: 0.0090 - acc: 0.9970
13440/60000 [=====>........................] - ETA: 2s - loss: 0.0093 - acc: 0.9969
14720/60000 [======>.......................] - ETA: 1s - loss: 0.0093 - acc: 0.9971
16128/60000 [=======>......................] - ETA: 1s - loss: 0.0093 - acc: 0.9972
17024/60000 [=======>......................] - ETA: 1s - loss: 0.0093 - acc: 0.9972
17664/60000 [=======>......................] - ETA: 1s - loss: 0.0092 - acc: 0.9973
18560/60000 [========>.....................] - ETA: 1s - loss: 0.0102 - acc: 0.9972
19328/60000 [========>.....................] - ETA: 1s - loss: 0.0102 - acc: 0.9971
20096/60000 [=========>....................] - ETA: 1s - loss: 0.0102 - acc: 0.9971
21504/60000 [=========>....................] - ETA: 1s - loss: 0.0100 - acc: 0.9972
22784/60000 [==========>...................] - ETA: 1s - loss: 0.0096 - acc: 0.9973
24192/60000 [===========>..................] - ETA: 1s - loss: 0.0094 - acc: 0.9974
25344/60000 [===========>..................] - ETA: 1s - loss: 0.0093 - acc: 0.9974
26624/60000 [============>.................] - ETA: 1s - loss: 0.0094 - acc: 0.9974
27904/60000 [============>.................] - ETA: 1s - loss: 0.0095 - acc: 0.9974
29312/60000 [=============>................] - ETA: 1s - loss: 0.0096 - acc: 0.9974
30592/60000 [==============>...............] - ETA: 1s - loss: 0.0096 - acc: 0.9973
31872/60000 [==============>...............] - ETA: 1s - loss: 0.0095 - acc: 0.9974
33152/60000 [===============>..............] - ETA: 1s - loss: 0.0096 - acc: 0.9974
34432/60000 [================>.............] - ETA: 1s - loss: 0.0095 - acc: 0.9974
35840/60000 [================>.............] - ETA: 1s - loss: 0.0096 - acc: 0.9973
36992/60000 [=================>............] - ETA: 1s - loss: 0.0095 - acc: 0.9974
38272/60000 [==================>...........] - ETA: 0s - loss: 0.0095 - acc: 0.9974
38784/60000 [==================>...........] - ETA: 0s - loss: 0.0094 - acc: 0.9974
39680/60000 [==================>...........] - ETA: 0s - loss: 0.0094 - acc: 0.9973
40448/60000 [===================>..........] - ETA: 0s - loss: 0.0095 - acc: 0.9973
41216/60000 [===================>..........] - ETA: 0s - loss: 0.0095 - acc: 0.9973
42240/60000 [====================>.........] - ETA: 0s - loss: 0.0095 - acc: 0.9973
43520/60000 [====================>.........] - ETA: 0s - loss: 0.0096 - acc: 0.9973
44800/60000 [=====================>........] - ETA: 0s - loss: 0.0095 - acc: 0.9973
46080/60000 [======================>.......] - ETA: 0s - loss: 0.0094 - acc: 0.9973
47360/60000 [======================>.......] - ETA: 0s - loss: 0.0096 - acc: 0.9972
48384/60000 [=======================>......] - ETA: 0s - loss: 0.0097 - acc: 0.9971
49664/60000 [=======================>......] - ETA: 0s - loss: 0.0098 - acc: 0.9971
50944/60000 [========================>.....] - ETA: 0s - loss: 0.0097 - acc: 0.9971
52096/60000 [=========================>....] - ETA: 0s - loss: 0.0097 - acc: 0.9971
53504/60000 [=========================>....] - ETA: 0s - loss: 0.0098 - acc: 0.9971
54784/60000 [==========================>...] - ETA: 0s - loss: 0.0099 - acc: 0.9971
56064/60000 [===========================>..] - ETA: 0s - loss: 0.0099 - acc: 0.9971
57472/60000 [===========================>..] - ETA: 0s - loss: 0.0100 - acc: 0.9970
58752/60000 [============================>.] - ETA: 0s - loss: 0.0101 - acc: 0.9971
59904/60000 [============================>.] - ETA: 0s - loss: 0.0100 - acc: 0.9971
60000/60000 [==============================] - 3s 45us/step - loss: 0.0100 - acc: 0.9971
答案 2 :(得分:-2)
您能否提供错误的完整答案? 显然是给您一个3维的数组(可能是黑白图像)。因此,一种选择是获取前两个2D数组(每个通道一个数组)。
但是您应该提供更多信息,以便了解当时的情况。
祝你好运