I'm trying to train a model using MNIST dataset from keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten
mnist = keras.datasets.mnist
(x_train,x_test),(y_train,y_test) = mnist.load_data()
import numpy as np
import matplotlib.pyplot as plt
print(np.min(x_train),np.max(x_train))
x_train = x_train/255.0
x_test = x_test/255.0
print(np.min(x_train),np.max(x_train))
print(x_test.min(),x_test.max())
model = Sequential()
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(128,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.summary()
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics='accuracy')
model.fit(x_train,y_train,epochs=10)
When I train the model my console throws this error:
ValueError: Data cardinality is ambiguous:
x sizes: 60000
y sizes: 10000
Make sure all arrays contain the same number of samples.
Full error:
ValueError Traceback (most recent call last)
c:\Users\USER\Documents\DSProject\text_Recog_tf.ipynb Cell 7' in <cell line: 2>()
1 model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics='accuracy')
----> 2 model.fit(x_train,y_train,epochs=10)
File c:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File c:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\data_adapter.py:1655, in _check_data_cardinality(data)
1651 msg = " {} sizes: {}\n".format(
1652 label, ", ".join(str(i.shape[0])
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)
ValueError: Data cardinality is ambiguous:
x sizes: 60000
y sizes: 10000
Make sure all arrays contain the same number of samples.
I'm not sure nor I understand what the error was because I'm just a novice following a tutorial video on youtube
CodePudding user response:
use this code to load the dataset
(x_train,y_train),(x_test,y_test) = mnist.load_data()
print(len(x_train), len(y_train))
the resultant print is
60000 60000
So now you have 60000 images and 60000 labels