-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.py
More file actions
45 lines (37 loc) · 1.22 KB
/
nn.py
File metadata and controls
45 lines (37 loc) · 1.22 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
from ml_hpx import Layer, SGD, NeuralNetwork
import numpy as np
from time import perf_counter
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Input
# ----- Dataset -----
X = np.array([[0,0], [0,1], [1,0], [1,1]], dtype=np.float32)
y = np.array([[0], [1], [1], [0]], dtype=np.float32)
# ----- HPX NN -----
layers = [
Layer(4, 2, "relu"),
Layer(1, 4, "sigmoid")
]
optimizer = SGD(0.1)
nn = NeuralNetwork(layers, optimizer)
start = perf_counter()
nn.fit(X.tolist(), y.tolist(), 1000) # 5000 epochs
end = perf_counter()
print(f"HPX NN training time: {end - start:.6f} sec")
preds = nn.predict(X.tolist())
print("HPX Predictions:", preds)
# ----- TensorFlow NN -----
model = Sequential([
Input(shape=(2,)), # explicitly declare input shape
Dense(4, activation='relu'),
Dense(1, activation='sigmoid')
])
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.1),
loss='binary_crossentropy',
metrics=['accuracy'])
start = perf_counter()
model.fit(X, y, epochs=1000, verbose=0)
end = perf_counter()
print(f"TensorFlow training time: {end - start:.6f} sec")
preds_tf = model.predict(X)
print("TF Predictions:", preds_tf.tolist())