ml-2023SZ/zad9_keras.py

36 lines
1.0 KiB
Python
Raw Permalink Normal View History

2024-01-07 22:06:32 +01:00
import numpy as np
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.datasets import fashion_mnist
np.random.seed(10)
device = "gpu" if tf.config.list_physical_devices("GPU") else "cpu"
print(device)
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
train_images = train_images.reshape(-1, 28, 28, 1)
test_images = test_images.reshape(-1, 28, 28, 1)
neurons = 300
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(neurons, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\naccuracy:', test_acc)