-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_model.py
57 lines (46 loc) · 1.66 KB
/
training_model.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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.utils import to_categorical
MODEL_PATH = 'C:/Users/PC/CourseProjects/playing_with_ai/model.h5'
# Check if TensorFlow is using GPU
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
# Configure TensorFlow to avoid GPU memory growth issues
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
# Restrict TensorFlow to only allocate a specific amount of memory on the first GPU
tf.config.experimental.set_memory_growth(gpus[0], True)
except RuntimeError as e:
print(e)
# Load and prepare data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# Build the model
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Train the model
model.fit(x_train, y_train, epochs=5, batch_size=32, validation_split=0.2)
# Evaluate the model
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_accuracy:.4f}')
# Make predictions
for index in range(2):
predictions = model.predict(x_test)
predicted_class = predictions[index].argmax()
print(f'Predicted class for the {index} test image: {predicted_class}')
model.save('C:/Users/PC/CourseProjects/playing_with_ai/model.keras')
input()