我在做一个关于使用mnist的colabs的小项目。当我得到这个令人烦恼的错误时,我决定改变数据集。不久前我也发了一个关于这个的帖子。但是这个问题没有解决。告诉我我哪里出错了。
我的代码:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
data = keras.datasets.cifar10
df = data.load_data()
(X_train, y_train),(X_test, y_test) = df
X_train_flat = X_train.reshape(-1,32*32)
X_test_flat = X_test.reshape(-1, 32*32)
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
#first hidden layer
Dense(units=10,
input_shape = (1024,),
activation='sigmoid'),
#second hidden layer
#Dense(units=10,
# input_shape = (784,),
# activation='sigmoid')
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(X_test_flat, y_train, epochs=15)#im getting the error on this line
y_pred = model.predict(X_test_flat)
model.evaluate(X_test_flat, y_test)
错误是:
ValueError Traceback (most recent call last)
<ipython-input-13-5508f48e747b> in <module>
----> 1 model.fit(X_test_flat, y_train, epochs=15)
1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py in _check_data_cardinality(data)
1653 for i in tf.nest.flatten(single_data)))
1654 msg += "Make sure all arrays contain the same number of samples."
-> 1655 raise ValueError(msg)
1656
1657
ValueError: Data cardinality is ambiguous:
x sizes: 30000
y sizes: 50000
Make sure all arrays contain the same number of samples.
为任何格式问题道歉。我不在我的pc rn上
1条答案
按热度按时间8fq7wneg1#
数据集整形会导致错误,您可以通过在模型定义中使用
Flatten layer
来实现此错误。您可以简单地删除这一行:
可以在模型定义中定义数据集输入形状的展平层,如下:
现在,将
X_test_flat
替换为X_train
,将X_test_flat
替换为X_test
,用于模型训练、评估和预测。请看一下类似的分类模型here。