-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple ffnn example.py
48 lines (36 loc) · 1.23 KB
/
simple ffnn example.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
# simple ffnn for binary classifications
import tensorflow as tf
import csv
from sklearn.model_selection import train_test_split
# insert data set between ""
with open("") as f:
reader = csv.reader(f)
next(reader)
# format data set
data = []
for row in reader:
data.append({
"evidence": [float(cell) for cell in row[:4]],
"label": 1 if row[4] == "0" else 0
})
# divide into training and testing set
evidence = [row["evidence"] for row in data]
labels = [row["label"] for row in data]
X_training, X_testing, y_training, y_testing = train_test_split(
evidence, labels, test_size = 0.4
)
# neural network structure, sequential means one layer after the other
model = tf.keras.models.Sequential()
# hidden/dense layer, for example: 8 units, ReLU activation function
model.add(tf.keras.layers.Dense(8, input_shape = (4,), activation = "relu"))
# add output layer, 1 unit, sigmoid activation
model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
# training and optimizing model
model.compile(
optimizer = "adam",
loss = "binary_crossentropy",
metrics = ["accuracy"]
)
model.fit(X_training, y_training, epochs = 20)
# evaluation
model.evaluate(X_testing, y_testing, verbose = 2)