-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_classify_sgd.py
129 lines (83 loc) · 2.77 KB
/
image_classify_sgd.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
# In[134]:
data = pd.read_csv("fashion-mnist_train.csv")
valid_split = 0.1
def train_test_split(data,valid_split):
'''
This function splits the data into validation set and train set
'''
data = data.values[:,:]
np.random.shuffle(data) # randomly shuffling the data
total_count = len(data)
valid_count = int(valid_split * total_count)
valid_data = data[0:valid_count,:]
train_data = data[valid_count:,:]
return valid_data,train_data
valid_data,train_data = train_test_split(data,valid_split)
valid_label = valid_data[:,0] #validation labels
train_label = train_data[:,0] #train labels
valid_data = valid_data[:,1:] # remove first column (labels)
train_data = train_data[:,1:]
# Binarize the data
threshold = 127
def binarization(data,threshold):
data[data <= threshold] = 0
data[data > threshold] = 1
return data
valid_binary_data = binarization(valid_data,threshold) #binary validaiton examples
train_binary_data = binarization(train_data,threshold) #binary training examples
# In[140]:
def sigmoid(x):
return 1/(1+np.exp(-x))
# In[203]:
def sigmoid_derivative(x):
return sigmoid(x) *(1-sigmoid (x))
# In[204]:
def softmax(x):
expo = np.exp(x)
return expo / expo.sum(axis=1, keepdims=True)
# In[228]:
def image_classification(x,y,lr,epochs,hidden_units):
'''
x: hidden unit features
y: labels
lr: learning rate
hidden_units: neurons count in layer1
'''
classes = 10
examples,features = x.shape
one_hot_labels = np.zeros((examples,10))
for i in range(examples):
one_hot_labels[i,y[i]] = 1
w1 = np.random.rand(features,hidden_units)
b1 = np.random.rand(hidden_units)
w2 = np.random.rand(hidden_units,10)
b2 = np.random.rand(10)
error =[]
for i in range(epochs):
#print(i)
pre_act_out1 = np.dot(x, w1) + b1
act_out1 = sigmoid(pre_act_out1)
pre_act_out2 = np.dot(act_out1, w2) + b2
act_out2 = softmax(pre_act_out2)
cost = act_out2 - one_hot_labels
cost1 = np.dot(act_out1.T, cost)
cost2 = np.dot(cost,w2.T)
der_w1 = sigmoid_derivative(pre_act_out1)
cosh1t = np.dot(x.T,der_w1*cost2)
delw = cost2 * der_w1
w1 = w1 -lr * cosh1t
b1 = b1 - lr * cosh1t.sum(axis=0)
w2 = w2 - lr * cost1
b2 = b2 - lr * cost1.sum(axis=0)
err = np.mean(-one_hot_labels * np.log(act_out2+0.0001))
print("Epoch = \t %s \t Error = \t %s \t accuracy = \t %s" %(i,err,1-err))
error.append(err)
return w1,b1,w2,b2
# In[229]:
w1,b1,w2,b2 = image_classification(x,y,0.00013,200,16)
# In[ ]: