Last active
June 8, 2019 09:44
-
-
Save fumiya-kume/30ab2a6168e8c9f5683b0b43b7e499ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# 3 layer neural network | |
class ThreeLayerNetwork: | |
def sigmoid(self,x): | |
return 1.0 / (1.0 + np.exp(-np.clip(x, -self.sigmoid_range, self.sigmoid_range))) | |
def derivative_sigmoid(self,o): | |
return o * (1.0 - o) | |
# constracta | |
def __init__(self, inodes, hnodes, onodes, lr): | |
# each layer node count | |
self.inodes = inodes | |
self.hnodes = hnodes | |
self.onodes = onodes | |
# learning rate | |
self.lr = lr | |
# init weight | |
self.w_ih = np.random.normal(0.0, 1.0, (self.hnodes, self.inodes)) | |
self.w_ho = np.random.normal(0.0, 1.0, (self.onodes, self.hnodes)) | |
# activation function setting. | |
self.sigmoid_range = 34.538776394910684 | |
# 誤差逆伝搬 | |
def backprop(self, idata, tdata): | |
# 縦ベクトルに変換 | |
o_i = np.array(idata, ndmin=2).T | |
t = np.array(tdata, ndmin=2).T | |
# hidden layer | |
x_h = np.dot(self.w_ih, o_i) | |
o_h = self.sigmoid(x_h) | |
# output layer | |
x_o = np.dot(self.w_ho, o_h) | |
o_o = self.sigmoid(x_o) | |
# 誤差計算 | |
e_o = (t - o_o) | |
e_h = np.dot(self.w_ho.T, e_o) | |
# Refresh weight setting. | |
self.w_ho += self.lr * np.dot((e_o * self.derivative_sigmoid(o_o)), o_h.T) | |
self.w_ih += self.lr * np.dot((e_h * self.derivative_sigmoid(o_h)), o_i.T) | |
# 順伝搬 | |
def feedforward(self, idata): | |
# 入力のリストを縦ベクトルに変換 | |
o_i = np.array(idata, ndmin=2).T | |
# hidden layer | |
x_h = np.dot(self.w_ih, o_i) | |
o_h = self.sigmoid(x_h) | |
# output layer | |
x_o = np.dot(self.w_ho, o_h) | |
o_o = self.sigmoid(x_o) | |
return o_o | |
if __name__=='__main__': | |
# parametor | |
inodes = 784 | |
hnodes = 100 | |
onodes = 10 | |
lr = 0.3 | |
# init neural network | |
nn = ThreeLayerNetwork(inodes, hnodes, onodes, lr) | |
# load training data | |
training_data_file = open('mnist_train.csv', 'r') | |
training_data_list = training_data_file.readlines() | |
training_data_file.close() | |
# load test data | |
test_data_file = open('mnist_test.csv') | |
test_data_list = test_data_file.readlines() | |
test_data_file.close() | |
# learning | |
epoch = 10 | |
for e in range(epoch): | |
print('#epoch ', e) | |
data_size = len(training_data_list) | |
for i in range(data_size): | |
if i % 1000 == 0: | |
print(' train: {0:>5d} / {1:>5d}'.format(i, data_size)) | |
val = training_data_list[i].split(',') | |
idata = (np.asfarray(val[1:]) / 255.0 * 0.99) + 0.01 | |
tdata = np.zeros(onodes) + 0.01 | |
tdata[int(val[0])] = 0.99 | |
nn.backprop(idata, tdata) | |
pass | |
pass | |
# test | |
scoreboard = [] | |
for record in test_data_list: | |
val = record.split(',') | |
idata = (np.asfarray(val[1:]) / 255.0 * 0.99) + 0.01 | |
tlabel = int(val[0]) | |
predict = nn.feedforward(idata) | |
plabel = np.argmax(predict) | |
scoreboard.append(tlabel == plabel) | |
pass | |
scoreboard_array = np.asarray(scoreboard) | |
print('performance: ', scoreboard_array.sum() / scoreboard_array.size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment