-
Notifications
You must be signed in to change notification settings - Fork 2
/
neural_network.py
68 lines (56 loc) · 1.93 KB
/
neural_network.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
import tensorflow as tf
import numpy as np
import keras
from time import time
from tensorflow.python.keras.callbacks import TensorBoard
from sklearn.utils import class_weight
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
import matplotlib.pyplot as plt
from keras.models import load_model
def learn(bac_array, user_input_array):
X = bac_array
y = user_input_array
# creates model
model = keras.Sequential([
keras.layers.Dense(input_shape=(1,), units=2),
keras.layers.Dense(32, activation=tf.nn.relu),
keras.layers.Dense(16, activation=tf.nn.relu),
keras.layers.Dense(8, activation=tf.nn.relu),
keras.layers.Dense(1)
])
# sets optimization function, learning rate and loss function for network
optimizer = tf.train.AdamOptimizer()
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
EPOCHS = 10000
# trains model
history = model.fit(
X, y,
epochs=EPOCHS,
verbose=1,)
# saves model (duh)
model.save('nn_model.h5')
# predicts perceived drunkenness for any bac
def predict(bac):
model = load_model('nn_model.h5')
optimizer = tf.train.AdamOptimizer()
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
predictions = model.predict(bac)
return predictions
def show_curve():
model = load_model('nn_model.h5')
optimizer = tf.train.AdamOptimizer()
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
bac_array = []
for i in range(100):
bac_array.append(i*(0.5/100))
predictions = model.predict(bac_array)
print("should plot now")
plt.plot(bac_array, predictions)
plt.show()