Skip to content

Instantly share code, notes, and snippets.

@jilm
Last active January 11, 2019 14:15
Show Gist options
  • Save jilm/9c543a5c47b785e79cded3b67ad07085 to your computer and use it in GitHub Desktop.
Save jilm/9c543a5c47b785e79cded3b67ad07085 to your computer and use it in GitHub Desktop.
Just Neural Network playground
import numpy as np
def sigmoid(x):
return 1.0/(1.0+np.exp(-x))
def sigmoidPrime(s):
return s*(1-s)
def sigmoid_derivative(x):
return np.exp(-x)/(1.0+np.exp(-x))**2
def feedforward(inp, weights):
# Append bias into the input
ni, nts = inp.shape
inp_bias = np.concatenate((inp, np.ones((1, nts), dtype=float)))
# Calculate values for the output
return sigmoid(np.dot(weights.T, inp_bias))
def test_feedforward():
inp = np.transpose(np.array([[1.0, 3.0], [0.0, 5.5]]))
W = np.transpose(np.array([[0.01, 0.02, 0.03], [0.03, 0.04, 0.05]]))
print(feedforward(inp, W))
class NeuralNetwork:
hidden_layer_size = 8
learning = 10000
def __init__(self, hiddenSize = 10):
self.inputSize = 35
self.outputSize = 4
self.hiddenSize = hiddenSize
self.W1 = np.random.randn(self.inputSize, self.hiddenSize)
self.W2 = np.random.randn(self.hiddenSize, self.outputSize)
def forward(self, X):
self.z = np.dot(X, self.W1)
self.z2 = sigmoid(self.z)
self.z3 = np.dot(self.z2, self.W2)
o = sigmoid(self.z3)
return o
def backward(self, X, y, o):
self.o_error = y - o
self.o_delta = self.o_error * sigmoidPrime(o)
self.z2_error = self.o_delta.dot(self.W2.T)
self.z2_delta = self.z2_error * sigmoidPrime(self.z2)
self.W1 += X.T.dot(self.z2_delta)
self.W2 += self.z2.T.dot(self.o_delta)
def train(self, X, y):
o = self.forward(X)
self.backward(X, y, o)
if __name__ == "__main__":
# y, w, g, r
colors = {
'y': [1,0,0,0],
'w': [0,1,0,0],
'g': [0,0,1,0],
'r': [0,0,0,1],
}
data = [
{
'system': 'ASR',
'data': [
0, 0, 0, 0, 0, # kap1 bezpecnost
0, 1, 0, 6, 13, 1, # kap2 udalosti
4, 524, 0, 0, 20, 0, 0, 23, 0, # kap3 fyzicky stav
50, 161, 60, # kap4 ekonomika
0, 0, 0, 0, # kap5 ztraty
0, 1, 0, 0, 7, 0, 0, 4, # kap6 tech. zmeny
],
'hodnoceni': 'w',
'rok': 2016,
},
{
'system': 'ASR',
'data': [
0, 0, 0, 0, 0, # kap1 bezpecnost
0, 0, 0, 4, 5, 0, # kap2 udalosti
4, 478, 0, 8, 14, 0, 0, 3, 0, # kap3 fyzicky stav
90, 163, 68, # kap4 ekonomika
0, 0, 0, 0, # kap5 ztraty
0, 0, 1, 0, 10, 0, 0, 5, # kap6 tech. zmeny
],
'hodnoceni': 'w',
'rok': 2017,
},
]
x = [i['data'] for i in data if i['rok'] == 2017]
X = np.array([[j/sum(i) for j in i] for i in x ])
y = np.array([colors[i['hodnoceni']] for i in data if i['rok'] == 2017])
valid_x = [i['data'] for i in data if i['rok'] == 2016]
valid_X = np.array([[j/sum(i) for j in i] for i in valid_x ])
valid_y = np.array([colors[i['hodnoceni']] for i in data if i['rok'] == 2016])
result = []
for hs in range(50):
NN = NeuralNetwork(hs)
for i in range(10000):
#print("#Loss: {}".format(str(np.mean(np.square(y - NN.forward(X))))))
NN.train(X, y)
#print("#Valid: {}".format(str(np.mean(np.square(valid_y - NN.forward(valid_X))))))
#print("#Valid output: \n" + str(NN.forward(valid_X)))
#print("#Expected output: \n" + str(valid_y))
result.append((
np.mean(np.square(valid_y - NN.forward(valid_X))),
np.mean(np.square(y - NN.forward(X)))
))
#print("#Input: \n" + str(X))
#print("#Actual Output: \n" + str(y))
#print("#Predicted OUtput: \n" + str(NN.forward(X)))
#print("#Loss: {}".format(str(np.mean(np.square(y - NN.forward(X))))))
#print("#Valid: {}".format(str(np.mean(np.square(valid_y - NN.forward(valid_X))))))
#print("#Valid output: \n" + str(NN.forward(valid_X)))
#print("#Expected output: \n" + str(valid_y))
#print("#\n")
print("""
plot '-' with lines, '-' with lines
{}
e
{}
e
""".format(
'\n'.join(
'{0!s} {1[0]!s}'.format(idx, i) for idx,i in enumerate(result)
),
'\n'.join(
'{0!s} {1[1]!s}'.format(idx, i) for idx,i in enumerate(result)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment