-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvolutionalNeuralNetwork.py
More file actions
122 lines (88 loc) · 3.78 KB
/
ConvolutionalNeuralNetwork.py
File metadata and controls
122 lines (88 loc) · 3.78 KB
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
# coding: utf-8
# In[1]:
# citation: this drastic code change is after consulting with another team from ML2 class after professor Chen's request.
#package import and simplification
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
#data loading process and data manipulating process
bbs = np.loadtxt('bbs-train.txt') #opens file with name of "test.txt"
label = np.loadtxt('label-train.txt')
label = label[:,1]
label_pro = np.empty([len(label),2])
for i in range(len(label)):
if label[i] == 0:
label_pro[i] = [1,0]
elif label[i] == 1:
label_pro[i] = [0,1]
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(bbs, label_pro, test_size=0.2, random_state=0)
x = tf.placeholder("float", [None, 800])
y_ = tf.placeholder("float", [None, 2])
xshape = tf.reshape(x, [-1,40,20,1])
#weight and bias setup
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
W_conv1 = weight_variable([10, 10, 1, 32])
W_conv2 = weight_variable([10, 10, 32, 64])
b_conv1 = bias_variable([32])
b_conv2 = bias_variable([64])
W_fc1 = weight_variable([10 * 5 * 64, 250])
W_fc2 = weight_variable([250, 2])
b_fc1 = bias_variable([250])
b_fc2 = bias_variable([2])
# pool and convolution set up
h_conv1 = tf.nn.relu(conv2d(xshape, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
h_pool2_flat = tf.reshape(h_pool2, [-1, 10*5*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#function setup with 2 convolution layer
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
y_conv2 = tf.argmax(y_conv,1)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits= y_conv ))
train_step = tf.train.AdamOptimizer(2e-5).minimize(cross_entropy)
#train_step = tf.train.RMSPropOptimizer(2e-5).minimize(cross_entropy)
#train_step = tf.train.GradientDescentOptimizer(2e-5).minimize(cross_entropy)
#train_step = tf.train.ProximalAdagradOptimizer(2e-5).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for img in x_train:
img *= 255.0/img.max()
for img in x_test:
img *= 255.0/img.max()
error = []
_result = []
# setup epoch=600 and batchsize=2000
#training process
for j in range(600):
for i in range(10):
random_select = np.random.randint(0,len(y_train), 2000)
xs = [x_train[k] for k in random_select]
ys = [y_train[k] for k in random_select]
sess.run(train_step, feed_dict={x: x_train, y_: y_train,keep_prob: 0.8})
train_accuracy, loss, y_soft = sess.run([accuracy,cross_entropy, y_conv2]
, feed_dict={x:x_train, y_: y_train, keep_prob: 1})
_result.append(y_soft)
error.append(loss)
if j%50 == 0:
print("step %d, training accuracy="%(j, train_accuracy),("test accuracy="% sess.run(accuracy, feed_dict={x: x_test, y_: y_test, keep_prob: 1.0})))
print("loss= ", loss)
train_accuracy, loss, y_soft, tf = sess.run([accuracy,cross_entropy, y_conv2, correct_prediction], feed_dict={x: x_test, y_: y_test, keep_prob: 1})
# In[ ]: