-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtensorflow-test.py
More file actions
59 lines (47 loc) · 1.95 KB
/
Copy pathtensorflow-test.py
File metadata and controls
59 lines (47 loc) · 1.95 KB
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
58
59
import os
"""
'0': Show all logs (default).
'1': Filter out INFO logs.
'2': Filter out WARNING logs.
'3': Filter out ERROR logs.
"""
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Suppress warnings and info (only errors will be shown)
import tensorflow as tf
import numpy as np
# ✅ Check if GPU is available
gpus = tf.config.list_physical_devices('GPU')
if gpus:
print("✅ GPU detected:", gpus[0])
# Set memory growth to avoid allocating all memory upfront
tf.config.experimental.set_memory_growth(gpus[0], True)
# Force using GPU:0 by running operations inside the tf.device context
with tf.device(f'/{gpus[0].name.split(":", 1)[-1]}'): # (value: '/GPU:0') Corrected usage of the device name
# x = tf.random.normal([10000, 20000])
# print(x)
# ✅ Load MNIST dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize
# ✅ Define a simple neural network
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
# ✅ Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
"""
To resolve:
1. install python3.10
2. created venv310
3. added opencv-python-headless to requirements, as you need DNN
4. reran
"""
# ✅ Train the model (will use GPU if available)
model.fit(x_train, y_train, epochs=3, batch_size=128) # <-- this line causes stack script
# ✅ Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {accuracy:.4f}")
else:
print("❌ No GPU detected, running on CPU.")