Skip to content

Instantly share code, notes, and snippets.

@murayama333
Created August 24, 2017 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murayama333/487b1864ea6c3b9e15011fe78ea965c0 to your computer and use it in GitHub Desktop.
Save murayama333/487b1864ea6c3b9e15011fe78ea965c0 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
class NeuralNetwork:
def __init__(self):
self.hw = 0.01 * np.random.randn(50, 4)
self.hb = np.zeros(50)
self.ow = 0.01 * np.random.randn(3, 50)
self.ob = np.zeros(3)
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def softmax(self, x):
return np.exp(x) / np.sum(np.exp(x))
def cross_entropy_error(self, y, t):
delta = 1e-7
return -np.sum(t * np.log(y + delta))
def diff(self, f, x):
h = 1e-4
retuf (f(x + h) - f(x - h)) / 2 * h
def _grad(self, f, x):
h = 1e-4
grad = np.zeros_like(x)
for i in range(x.size):
tmp = x[i]
x[i] = tmp + h
fxh1 = f(x)
x[i] = tmp - h
fxh2 = f(x)
grad[i] = (fxh1 - fxh2) / (2 * h)
x[i] = tmp
return grad
def grad(self, f, x):
if x.ndim == 1:
return self._grad(f, x)
else :
grad = np.zeros_like(x)
for i, x in enumerate(x):
grad[i] = self._grad(f, x)
return grad
def neuron(self, w, b, x, activate):
return activate(w.dot(x) + b)
def input(self, x, t):
hy = self.neuron(self.hw, self.hb, x, self.sigmoid)
y = self.neuron(self.ow, self.ob, hy, self.softmax)
loss = self.cross_entropy_error(y, t)
return loss
def train(self, x, t, lr=0.1):
loss = lambda w: self.input(x, t)
grads = {}
grads['hw'] = self.grad(loss, nn.hw)
grads['hb'] = self.grad(loss, nn.hb)
grads['ow'] = self.grad(loss, nn.ow)
grads['ob'] = self.grad(loss, nn.ob)
self.hw -= lr * grads['hw']
self.hb -= lr * grads['hb']
self.ow -= lr * grads['ow']
self.ob -= lr * grads['ob']
# print(self.hw[0][0])
def test(self, x, t):
hy = self.neuron(self.hw, self.hb, x, self.sigmoid)
y = self.neuron(self.ow, self.ob, hy, self.softmax)
a = np.argmax(y)
b = np.argmax(t)
return (a == b).astype('int')
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
iris = load_iris()
x = iris.data
y = to_categorical(iris.target)
x_train, x_test, y_train, y_test = train_test_split(x, y)
nn = NeuralNetwork()
for epoch in range(5):
for i in range(x_train.shape[0]):
nn.train(x_train[i], y_train[i])
loss = nn.input(x_train[i], y_train[i])
print(epoch)
collect = 0
for i in range(x_test.shape[0]):
collect += nn.test(x_test[i], y_test[i])
print(collect / x_test.shape[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment