-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNN_full.py
126 lines (84 loc) · 2.4 KB
/
NN_full.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# coding: utf-8
# In[1]:
import os
import sys
import pandas as pd
import numpy as np
import random
import pickle
import sklearn
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.layers import Dropout
from keras.wrappers.scikit_learn import KerasClassifier
from keras.constraints import maxnorm
from twilio_functions import send_sms
from prep import train_prep, test_prep
from keras.utils.training_utils import multi_gpu_model
import csv
sys.path.insert(0, 'helpers')
from config import *
# In[9]:
log = open("deploy.log", "a")
sys.stdout = log
# In[ ]:
x_df, y_df = train_prep(sample = sample, narrow = narrow)
# # Neural Network
# In[7]:
# Function to create model, required for KerasClassifier
def create_model(optimizer='Adagrad',
init_mode='he_normal',
activation = 'softsign',
dropout_rate=0.9,
weight_constraint=5,
neurons = 1500):
# create model
clf = Sequential()
clf.add(Dense(neurons,
kernel_initializer=init_mode,
input_dim=len(x_df.columns),
activation=activation,
kernel_constraint=maxnorm(weight_constraint)))
clf.add(Dropout(dropout_rate))
clf.add(Dense(1,
activation='sigmoid'))
parallel_model = multi_gpu_model(clf, gpus=gpus)
# Compile model
parallel_model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return parallel_model
# In[8]:
clf = KerasClassifier(build_fn=create_model,
epochs=100,
batch_size=150,
verbose=0)
# In[ ]:
try:
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
print('Starting to train...')
clf.fit(x_df.values,
y_df.values)
# summarize results
body = "Finished training."
sms = send_sms(body)
print(sms)
except:
body = "Training failed"
sms = send_sms(body)
print(sms)
# In[ ]:
print('Starting model save...')
clf.model.save('clf.h5')
# In[1]:
x_test = test_prep(narrow = True)
# In[ ]:
clf_probs = clf.predict_proba(x_test)
# In[ ]:
with open('clf_probs.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(clf_probs)
csvFile.close()
body = 'Predictions should be written to clf_probs.csv'
sms = send_sms(body)
print(sms)