-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorflow-Test-Accuracy.py
More file actions
32 lines (25 loc) · 984 Bytes
/
Copy pathTensorflow-Test-Accuracy.py
File metadata and controls
32 lines (25 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import SGD
# Load the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Normalize the images
train_images = train_images / 255.0
test_images = test_images / 255.0
# Create a sequential model
model = Sequential([
Flatten(input_shape=(28, 28)), # Flatten the input
Dense(128, activation='relu'), # First dense layer
Dense(10, activation='softmax') # Output layer
])
# Compile the model
model.compile(optimizer=SGD(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(train_images, train_labels, epochs=5)
# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)