-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn_2.py
More file actions
60 lines (47 loc) · 1.76 KB
/
nn_2.py
File metadata and controls
60 lines (47 loc) · 1.76 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
60
import numpy as np
from time import perf_counter
import tensorflow as tf
from tensorflow.keras import Sequential, Input
from tensorflow.keras.layers import Dense
from sklearn.datasets import make_moons
# ----- Dataset -----
# Non-linear classification problem (10000 samples, 20 features after expansion)
X, y = make_moons(n_samples=10000, noise=0.2, random_state=42)
X = np.hstack([X, np.random.randn(X.shape[0], 18)]) # add 18 noisy features → 20 total
y = y.reshape(-1, 1).astype(np.float32)
X = X.astype(np.float32)
# ----- TensorFlow NN -----
model = Sequential([
Input(shape=(20,)), # Input: 20 features
Dense(32, activation='relu'),
Dense(16, activation='relu'),
Dense(1, activation='sigmoid')
])
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.05),
loss='binary_crossentropy',
metrics=['accuracy'])
start = perf_counter()
history = model.fit(X, y, epochs=50, batch_size=32, verbose=0)
end = perf_counter()
print(f"TensorFlow training time: {end - start:.6f} sec")
loss, acc = model.evaluate(X, y, verbose=0)
print(f"TF Accuracy: {acc:.4f}, Loss: {loss:.4f}")
preds_tf = model.predict(X[:10], verbose=0)
print("TF Predictions (first 10):", preds_tf.flatten().tolist())
# ----- HPX NN -----
from ml_hpx import Layer, SGD, NeuralNetwork
layers = [
Layer(32, 20, "relu"),
Layer(16, 32, "relu"),
Layer(1, 16, "sigmoid")
]
optimizer = SGD(0.05)
nn = NeuralNetwork(layers, optimizer)
start = perf_counter()
nn.fit(X.tolist(), y.tolist(), 50)
end = perf_counter()
print(f"HPX NN training time: {end - start:.6f} sec")
loss, acc = nn.evaluate(X.tolist(), y.tolist())
print(f"HPX NN Accuracy: {acc:.4f}, Loss: {loss:.4f}")
preds_hpx = nn.predict(X[:10].tolist())
print("HPX Predictions (first 10):", preds_hpx)