-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN.py
115 lines (76 loc) · 2.55 KB
/
NN.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import csv
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import feature_column
from tensorflow.keras import layers
# In[2]:
x_train = []
y_trainFourths = []
y_trainEighths = []
with open('./Pi Code/test.csv') as data:
reader = csv.reader(data, delimiter = ',')
a = []
b = []
c = []
d = []
for row in data:
arr = np.zeros(shape = (4, 2500,1))
total = row.split(",")
for j in range(0, 2500):
arr[0][j] = float(total[j][2:len(total[j])-5]) / 666.0
arr[1][j] = float(total[j+2500][2:len(total[j])-5]) / 666.0
arr[2][j] = float(total[j+5000][2:len(total[j])-5]) / 666.0
arr[3][j] = float(total[j+7500][2:len(total[j])-5]) / 666.0
x_train.append(arr)
y_trainFourths.append(int(total[10001]))
y_trainEighths.append(int(total[10002]))
# In[3]:
x_train = np.array(x_train)
y_trainFourths = np.array(y_trainFourths)
y_trainEighths = np.array(y_trainEighths)
shuffle = np.arange(x_train.shape[0])
np.random.shuffle(shuffle)
x_train = x_train[shuffle]
y_trainFourths = y_trainFourths[shuffle]
y_trainEighths = y_trainEighths[shuffle]
x_test = x_train[527:]
x_train = x_train[:527]
y_testFourths = y_trainFourths[527:]
y_trainFourths = y_trainFourths[:527]
y_testEighths = y_trainEighths[527:]
y_trainEighths = y_trainEighths[:527]
# In[4]:
print(x_train.shape)
print(y_trainFourths.shape)
# In[5]:
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu, input_shape=(4, 2500, 1)),
tf.keras.layers.Conv2D(32, (2,2), padding='same'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(64, activation=tf.nn.relu),
tf.keras.layers.Dense(32, activation=tf.nn.relu),
tf.keras.layers.Dense(16, activation=tf.nn.relu),
tf.keras.layers.Dense(4, activation=tf.nn.softmax)
])
#change shape of input to correct
# In[6]:
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
# In[ ]:
history = model.fit(x_train, y_trainFourths, epochs=200, batch_size=2, validation_data=(x_test, y_testFourths))
# In[ ]:
# loss = history.history['loss']
# plt.plot(loss)
# acc = history.history['accuracy']
# plt.plot(acc)
# val_acc = history.history['val_accuracy']
# plt.plot(val_acc)
# plt.ylim(0,5)