当运行下面的代码时,我得到一个错误,说有形状不匹配,在谷歌上搜索后,我发现这通常是由于当目标不是单码编码时,使用cross_entropy作为损失函数引起的。由于我的编码不是一次性编码,因此我使用了 sparse_categorical_crossentropy ,但是错误仍然存在。
这是我收到的错误:
Train on 60000 samples
Epoch 1/100
32/60000 [..............................] - eta: 1:18
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-123-4ce1d4d047d4> in <module>()
----> 1 model.fit(x_train,y_train,batch_size=32,epochs=100,verbose=1)
25 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/nn_ops.py in sparse_softmax_cross_entropy_with_logits(_sentinel,labels,logits,name)
3391 "should equal the shape of logits except for the last "
3392 "dimension (received %s)." % (labels_static_shape,-> 3393 logits.get_shape()))
3394 # Check if no reshapes are required.
3395 if logits.get_shape().ndims == 2:
ValueError: Shape mismatch: The shape of labels (received (32,)) should equal the shape of logits except
for the last dimension (received (5408,10)).
这是我的代码:
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
(x_train,y_train),(x_test,y_test) = fashion_mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
x_train = np.reshape(x_train,(-1,28,1))
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3,strides=2,input_shape=[28,1],activation='relu'))
model.add(tf.keras.layers.Dense(units = 10,activation='softmax'))
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['sparse_categorical_accuracy'])
model.fit(x_train,verbose=1)