-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTraffic_app.py
42 lines (35 loc) · 1.38 KB
/
Traffic_app.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
import json
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Load synthetic data
def load_synthetic_data(filename="genenrate_traffic_data (2).py"):
with open(filename, "r") as infile:
data = json.load(infile)
return data
# Prepare data for training
def prepare_data(data):
X = []
y = []
for sample in data:
X.append([sample["traffic_volume"], sample["spike_detected"], sample["unauthorized_access"]])
y.append(1 if sample["spike_detected"] or sample["unauthorized_access"] else 0)
return np.array(X), np.array(y)
# Define and train the model
def train_phi3_model(X, y):
model = Sequential()
model.add(Dense(16, input_dim=X.shape[1], activation="relu"))
model.add(Dense(8, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(X, y, epochs=10, batch_size=16, validation_split=0.2)
# Save the trained model as an .h5 file
model.save("phi3_traffic_model.h5")
print("Model trained and saved as phi3_traffic_model.h5")
return model
if __name__ == "__main__":
# Load data and train the model
data = load_synthetic_data()
X, y = prepare_data(data)
model = train_phi3_model(X, y)