-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrockmine.py
174 lines (132 loc) · 5.39 KB
/
rockmine.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
# Reading Dataset
def read_dataset():
df = pd.read_csv('sonar-all-data.csv')
print(df.shape)
X = df[df.columns[0:60]].values
y = df[df.columns[60]]
#Encode th dependent Variables
encoder = LabelEncoder()
encoder.fit(y)
y = encoder.transform(y)
Y = one_hot_encode(y)
#print(Y)
print(X.shape)
return(X, Y)
# Define the encoder function
def one_hot_encode(labels):
n_labels = len(labels)
n_unique_labels = len(np.unique(labels))
one_hot_encode = np.zeros((n_labels, n_unique_labels))
one_hot_encode[np.arange(n_labels), labels] = 1
return one_hot_encode
# Read the Dataset
X, Y = read_dataset()
# Shuffle the dataset to mix rows because in beggining of dataset there are only mines and then rocks...so Shuffle.
X, Y = shuffle(X, Y, random_state = 1)
# Split the dataset into train and test
train_x, test_x, train_y, test_y = train_test_split(X, Y, test_size = 0.20, random_state = 415)
# Check or quick look at the train and test shape
print("\nTrain and Test Shape")
print(train_x.shape)
print(train_y.shape)
print(test_x.shape)
# Define important parameters and variables to work with tensors
learning_rate = 0.3
training_epochs = 1000 #Total Number of iterations will be done to minimize the error
cost_history = np.empty(shape = [1], dtype = float)
n_dim = X.shape[1]
print("n_dim = ", n_dim)
n_class = 2
model_path = "/Users/rudrajikadra/Documents/Machine Learning Works/3) Rock and Mine Identifier/" #Path to store the model
### MULTILAYER PERCEPTRON
# Define number of hidden layers and number of neurons for each layer
n_hidden_1 = 60
n_hidden_2 = 60
n_hidden_3 = 60
n_hidden_4 = 60
x = tf.placeholder(tf.float32, [None, n_dim])
W = tf.Variable(tf.zeros([n_dim, n_class]))
b = tf.Variable(tf.zeros([n_class]))
y_ = tf.placeholder(tf.float32, [None, n_class]) #Output of our model
#Define the model
def multilayer_perceptron(x, weights, biases):
# Hidden Layer 1 with Sigmoid Activation function
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.sigmoid(layer_1)
# Hidden Layer 2 with Sigmoid Activation function
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.sigmoid(layer_2)
# Hidden Layer 3 with Sigmoid Activation function
layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
layer_3 = tf.nn.sigmoid(layer_3)
# Hidden Layer 4 with Relu Activation function
layer_4 = tf.add(tf.matmul(layer_3, weights['h4']), biases['b4'])
layer_4 = tf.nn.relu(layer_4)
# Output layer with linear activation
out_layer = tf.matmul(layer_4, weights['out']) + biases['out']
return out_layer
# Define the weights and biases for each layer
weights = {
'h1' : tf.Variable(tf.truncated_normal([n_dim, n_hidden_1])), #60 x 60
'h2' : tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])),
'h3' : tf.Variable(tf.truncated_normal([n_hidden_2, n_hidden_3])),
'h4' : tf.Variable(tf.truncated_normal([n_hidden_3, n_hidden_4])),
'out' : tf.Variable(tf.truncated_normal([n_hidden_4, n_class]))
}
biases = {
'b1' : tf.Variable(tf.truncated_normal([n_hidden_1])),
'b2' : tf.Variable(tf.truncated_normal([n_hidden_2])),
'b3' : tf.Variable(tf.truncated_normal([n_hidden_3])),
'b4' : tf.Variable(tf.truncated_normal([n_hidden_4])),
'out' : tf.Variable(tf.truncated_normal([n_class]))
}
# Initialize all the variables
init = tf.global_variables_initializer()
# Saver object in order to save the model
saver = tf.train.Saver()
# Call the model Defined above
y = multilayer_perceptron(x, weights, biases)
# Define the cost/loss function and optimizer
cost_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y, labels = y_))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)
# Tensoflow Session
sess = tf.Session()
sess.run(init)
# Calculte the cost and accuracy of each epoch
mse_history = []
accuracy_history = []
for epoch in range(training_epochs):
sess.run(training_step, feed_dict = {x: train_x, y_: train_y})
cost = sess.run(cost_function, feed_dict = {x: train_x, y_: train_y})
cost_history = np.append(cost_history, cost)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) #Difference between the actual output and model outputs
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
pred_y = sess.run(y, feed_dict = {x: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y)) #Mean square error
mse_ = sess.run(mse)
mse_history.append(mse_)
accuracy = (sess.run(accuracy, feed_dict = {x: train_x, y_: train_y}))
accuracy_history.append(accuracy)
print("Epoch : ", epoch, " - Cost:", cost, " - MSE: ", mse_, "- Train Accuracy: ", accuracy)
save_path = saver.save(sess, model_path)
print("Model saved in file: %s" % save_path)
# Plot mse and accuracy grapth
plt.plot(mse_history, 'r')
plt.show()
plt.plot(accuracy_history, 'b')
plt.show()
# Print the final accuracy
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Test Accuracy: ", (sess.run(accuracy, feed_dict = {x: test_x, y_: test_y})))
# Print the final mean square error
pred_y = sess.run(y, feed_dict = {x: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y))
print("MSE: %.4f" % sess.run(mse))