-
Notifications
You must be signed in to change notification settings - Fork 4
/
nn2.py
144 lines (120 loc) · 4.26 KB
/
nn2.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
######################################################################################################
'''
Attempt to use the tflearn and tensorflow python libraries to teach the machine to identify hit bottom
profiles. Will also be used to compare output from the hand written neural network
Some functions will be collected from a different script "tf_neuralnet.py" which was used as the
script to run the network prior to the creation of this file
This is a revised script to make use of the new features that are inputted after the first review of the
neural network was completed.
'''
######################################################################################################
# importing libraries
from __future__ import division, print_function, absolute_import
import os.path
import matplotlib.pyplot as plt
from netCDF4.utils import ncinfo
from netCDF4 import Dataset
import numpy as np
import tflearn
import random
import hitbottom as hb
import tf_neuralnet as tf
######################################################################################################
# setting up data for feeding into neural network
# checking to see if model already exists
if (os.path.isfile('net2.index') == True):
print("\nExisting neural network model found")
print("loading...\n")
model.load('net2')
else:
print("\nneural network model not found")
print("training new neural network...\n")
# importing the training set data
dat1 = []
with open("nn_training_data_1.txt") as f:
next(f)
for line in f:
dat1.append([x.strip() for x in line.split(",")])
tr = [1, 0]
fls = [0, 1]
y_train = []
X_train = []
filename_train = []
n1 = len(dat1)
for i in range(0,n1):
X_train.append([float(dat1[i][1]),float(dat1[i][2]),float(dat1[i][3]),float(dat1[i][4])])
filename_train.append(dat1[i][5])
if (int(dat1[i][0]) == 1):
y_train.append(tr)
else:
y_train.append(fls)
# importing cross validation data
dat2 = []
with open("nn_crossvalidation_data_1.txt") as f:
next(f)
for line in f:
dat2.append([x.strip() for x in line.split(",")])
y_val = []
X_val = []
filename_crossval = []
n2 = len(dat2)
for i in range(0,n2):
X_val.append([float(dat2[i][1]),float(dat2[i][2]),float(dat2[i][3]),float(dat2[i][4])])
filename_crossval.append(dat2[i][2])
if (int(dat2[i][0]) == 1):
y_val.append(tr)
else:
y_val.append(fls)
# importing test data
dat3 = []
with open("nn_test_data_1.txt") as f:
next(f)
for line in f:
dat3.append([x.strip() for x in line.split(",")])
filename_test = []
X_test = []
y_test = []
z_test = []
T_test = []
n3 = len(dat3)
for i in range(0,n3):
filename_test.append(dat3[i][5])
X_test.append([float(dat3[i][1]),float(dat3[i][2]),float(dat3[i][3]),float(dat3[i][4])])
y_test.append(int(dat3[i][0]))
z_test.append(float(dat3[i][6]))
T_test.append(float(dat3[i][7]))
# filtering through the data to remove majority of poor points
[X_new, y_new] = tf.reduce_data(X_train,y_train)
X_train = X_new
y_train = y_new
[X_new, y_new] = tf.reduce_data(X_val,y_val)
X_val = X_new
y_val = y_new
# printing out dimensions of the training data
print("Data dimensions:")
print("Input:",len(X_train),len(X_train[0]))
print("Outputs:",len(y_train),len(y_train[0]))
print("\n")
######################################################################################################
# running neural network with tflearnS
# checking to see if model already exists
if (os.path.isfile('net2.index') == True):
pass
else:
# testing the tflearn reading data function
net = tflearn.input_data(shape=[None,4])
net = tflearn.fully_connected(net, 5, activation='sigmoid')
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net)
# creating the model
model = tflearn.DNN(net)
model.fit(X_train,y_train, n_epoch=25, validation_set = (X_val, y_val), \
show_metric=True, run_id="XBT hit bottom data")
#model.save('net2')
# using model to make predictions on test set
pred = model.predict(X_test)
# collecting key statistics on the data
[precision, files_incorrect] = tf.statistics(pred, z_test, T_test, y_test, filename_test, 1, 0, 1)
# evaluation of the neural network
tf.results(filename_test, files_incorrect, pred, z_test, T_test, False)
######################################################################################################