-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_network_models.py
68 lines (58 loc) · 2.09 KB
/
neural_network_models.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
58
59
60
61
62
63
64
65
66
67
68
from functools import partial
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.losses import BinaryCrossentropy # type: ignore
from tensorflow.keras.optimizers.legacy import Adam # type: ignore
from utils import clf_score, plot_decision_boundary
def predict_with_threshold(model, input_data, threshold=0.5):
probabilities = tf.nn.sigmoid(model.predict(input_data)).numpy()
return (probabilities > threshold).astype(int)
def relu_neural_net(
training_data, training_labels, test_data, test_labels, plot=True, lambda_=None
):
training_data = training_data.reshape(-1, 2)
training_labels = training_labels.reshape(-1, 1)
test_data = test_data.reshape(-1, 2)
test_labels = test_labels.reshape(-1, 1)
model = tf.keras.Sequential(
[
tf.keras.layers.Dense(
units=120,
activation="relu",
kernel_regularizer=tf.keras.regularizers.l2(lambda_),
),
tf.keras.layers.Dense(
units=40,
activation="sigmoid",
kernel_regularizer=tf.keras.regularizers.l2(lambda_),
),
tf.keras.layers.Dense(
units=20,
activation="relu",
kernel_regularizer=tf.keras.regularizers.l2(lambda_),
),
tf.keras.layers.Dense(units=1, activation="linear"),
]
)
model.compile(
loss=BinaryCrossentropy(from_logits=True),
optimizer=Adam(learning_rate=0.01),
)
model.fit(training_data, training_labels, epochs=100)
clf_score(
partial(predict_with_threshold, model),
training_data,
training_labels,
test_data,
test_labels,
)
if plot:
plot_decision_boundary(
training_data, training_labels, partial(predict_with_threshold, model)
)
def print_weights(model):
for i, layer in enumerate(model.layers):
weights = layer.get_weights()
print(f"Layer {i} weights: \n{weights[0]}")
print(f"Layer {i} biases: \n{weights[1]}")