-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py~
128 lines (109 loc) · 4.14 KB
/
test.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
import numpy as np
#from sklearn.datasets import load_digit
from sklearn.datasets import fetch_mldata
import pylab as pl
import matplotlib.pyplot as plt
#from math import exp
#from random import random
import random as ra
from numpy.random import *
import sys
mnist = fetch_mldata('MNIST original')
mnist.data = mnist.data.astype(np.float32)
mnist.data /= 255
mnist.target = mnist.target.astype(np.int32)
N = 60000
x_train,x_test = np.split(mnist.data,[N])
y_train,y_test = np.split(mnist.target,[N])
N_test = y_test.size
data_num = 28*28
input_vector_length = data_num + 1
inter_vector1_length = 300
inter_vector2_length = 300
output_vector_length = 10
learn_gain = 0.1
sig_gain = 1
def sigmoid(x):
return 1/(1+np.exp(x*(-1)*sig_gain))
def draw_digit(data):
size = 28
plt.figure(figsize = (2.5,3))
X,Y = np.meshgrid(range(size),range(size))
Z = data.reshape(size,size)
Z = Z[::-1,:]
plt.xlim(0,27)
plt.ylim(0,27)
plt.pcolor(X,Y,Z)
plt.gray()
plt.tick_params(labelbottom = "off")
plt.tick_params(labelleft = "off")
plt.show()
def make_delta_k(vector,err):
return sig_gain*err*vector*(1-vector)
def make_delta_h(vector,sigma_vector):
return sig_gain*vector*(1-vector)*sigma_vector
def make_delta_i(vector,sigma_vector):
return sig_gain*vector*(1-vector)*sigma_vector
def neural_net(input_vector,w1,w2,w3):
inter_vector1 = np.r_[1,sigmoid(np.dot(w1,input_vector))]
inter_vector2 = np.r_[1,sigmoid(np.dot(w2,inter_vector1))]
output = sigmoid(np.dot(w3,inter_vector2))
return output
weight_vector1 = rand(inter_vector1_length-1 , input_vector_length)
weight_vector2 = rand(inter_vector2_length-1 , inter_vector1_length)
weight_vector3 = rand(output_vector_length , inter_vector2_length)
#print weight_vector3
#inter_vector1 = np.zeros(inter_vector1_length)
#input_vector = np.zeros(input_vector_length)
#output_vector = np.zeros(output_vector_length)
train_num = N
print ra.randint(1,100)
for var in range(0,train_num):
seed = ra.randint(0,train_num-1)
if seed%10 ==0:
#input_vector = 0
#inter_vector1 = 0
#output_vector = 0
input_vector = np.r_[1,x_train[seed]]
inter_vector1 = np.r_[1,sigmoid(np.dot(weight_vector1,input_vector))]
#inter_vector1 = inter_vector1 / np.linalg.norm(inter_vector1)
#inter_vector2 = np.r_[1,sigmoid(np.dot(weight_vector2,inter_vector1))]
#inter_vector2 = inter_vector2 / np.linalg.norm(inter_vector2)
output_vector = sigmoid(np.dot(weight_vector3,inter_vector1))
output_vector /= output_vector.sum()
print output_vector
print y_train[seed]
minus = np.zeros(output_vector_length)
minus[y_train[seed]] = 1
#print minus
err = output_vector - minus
#print err
delta_k = make_delta_k(output_vector,err)
#print delta_k
weight_vector3 -= learn_gain * np.dot(np.matrix(delta_k).T , np.matrix(inter_vector1) )
#print weight_vector3.shape
#delta_h = make_delta_h(inter_vector2,np.dot(weight_vector3.T,delta_k))[1:inter_vector2_length]
#weight_vector2 -= learn_gain * np.dot(np.matrix(delta_h).T , np.matrix(inter_vector1) )
delta_i = make_delta_i(inter_vector1,np.dot(weight_vector3.T,delta_k))[1:inter_vector1_length]
weight_vector1 -= learn_gain * np.dot(np.matrix(delta_i).T , np.matrix(input_vector))
else:
a=1
inter_vector1 = np.zeros(inter_vector1_length)
input_vector = np.zeros(input_vector_length)
output_vector = np.zeros(output_vector_length)
test_num = 10000
for var in range(0,test_num):
if var%100 == 0:
print var
input_vector = np.r_[1,x_train[var]]
inter_vector1 = np.r_[1,sigmoid(np.dot(weight_vector1,input_vector))]
#inter_vector1 = inter_vector1 / np.linalg.norm(inter_vector1)
#inter_vector2 = np.r_[1,sigmoid(np.dot(weight_vector2,inter_vector1))]
#inter_vector2 = inter_vector2 / np.linalg.norm(inter_vector2)
output_vector = sigmoid(np.dot(weight_vector3,inter_vector1))
output_vector /= output_vector.sum()
print output_vector
print y_train[var]
else:
a=1
#print delta_k.size