我正在尝试使用 Keras 学习具有简单密集层的 MNIST 数据集。我希望我的图像大小为 16*16 而不是 28*28。我用了很多方法,但都不管用。这是简单的密集网络:
import keras
import numpy as np
import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
train_images = mnist.train_images()
train_labels = mnist.train_labels()
test_images = mnist.test_images()
test_labels = mnist.test_labels()
# Normalize the images.
train_images = (train_images / 255) - 0.5
test_images = (test_images / 255) - 0.5
print(train_images.shape)
print(test_images.shape)
# Flatten the images.
train_images = train_images.reshape((-1, 784))
test_images = test_images.reshape((-1, 784))
print(train_images.shape)
print(test_images.shape)
# Build the model.
model = Sequential([
Dense(10, activation='softmax', input_shape=(784,)),
])
# Compile the model.
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'],
)
# Train the model.
model.fit(
train_images,
to_categorical(train_labels),
epochs=5,
batch_size=32,
)
# Evaluate the model.
model.evaluate(
test_images,
to_categorical(test_labels)
)
# Save the model to disk.
model.save_weights('model.h5')
慕田峪4524236
相关分类