import tensorflow as tf
(train_imgs, train_labels ) , (test_imgs , test_labels ) = tf.keras.datasets.mnist.load_data()
Вы должны измениться. MLP как ваша модель - не берите 2-мерные массивы. Изменить форму:
train_x = train_imgs.reshape(60000, 28*28)
test_x = test_imgs.reshape(10000, 28*28)
# you need to 1-hot encode your labels
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
def one_hot_encode(labels, universe=None):
"""
This one hot encoder works with categorical and numeric data, both.
`universe=` you use, if labels don't contain all categories/numbers.
Or if you want the 1hot encoding to be in a special order (not the
automatic alphabetic order).
`labels=` are the categories/numbers as labels to be 1hot encoded.
"""
if universe is None:
universe = sorted(list(set(labels)))
nums = LabelEncoder().fit(universe).transform(labels)
one_hot = OneHotEncoder(sparse=False).fit(np.array(universe).reshape(-1, 1))\
.transform(np.array(nums).reshape(-1, 1))
return one_hot
train_labels_1h = one_hot_encode(train_labels)
test_labels_1h = one_hot_encode(test_labels)
# define epochs
epochs = 5
# define model
def create_model():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(512,activation=tf.nn.relu,input_shape = (784,)))
model.add(tf.keras.layers.Dense(256,activation=tf.nn.relu) )
model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))
model.compile(loss='categorical_crossentropy' , optimizer='adam')
return model
# "instanciate" a model
model = create_model()
# then run
model.fit(train_x, train_labels_1h, epochs=epochs)
А затем использовать их для обучения и тестирования.