Created
February 26, 2015 16:54
-
-
Save derf/46407f16a2b38bbbf35f to your computer and use it in GitHub Desktop.
Neuronales Netzwerk in Python
This file contains 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
# -*- coding: utf8 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib import animation, cm | |
class NeuralNetwork(object): # {{{ | |
# Lernrate: 0.1 bis 0.3 ist sinnvoll (zu hoch -> Schießt über das Ziel hinaus) | |
# alpha: Steilheit des Sigmoid. Sollte < 5 bleiben, sonst gibts | |
# numerische Fehler | |
def __init__(self, learning_rate, iterations, activation='Sigmoid', alpha=1): | |
self.__learning_rate = learning_rate | |
self.__iterations = iterations | |
self.__weights = (np.random.rand(iterations + 1, 1, 3) - 0.5) * 2 | |
self.__errors = np.ndarray((iterations)) | |
# Lineare Aktivierung: Hält sich nicht an "Ausgabe zwischen 0 und 1" | |
if activation == 'Linear': | |
func = lambda x : x | |
deriv = lambda x : 1 | |
elif activation == 'tanh': | |
func = lambda x : (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x)) | |
deriv = lambda x : 2. / (np.exp(x) + np.exp(-x)) | |
elif activation == 'Sigmoid': | |
self.__alpha = alpha | |
func = lambda x : 1. / (1 + np.exp(-alpha * x)) | |
deriv = lambda x : alpha * x * (1 - x) | |
else: | |
raise ValueError('activation') | |
self.__func = np.vectorize(func) | |
self.__deriv = np.vectorize(deriv) | |
def estimate(self, train_samples, train_labels): | |
self.__labels = labels = np.unique(train_labels) | |
weights = self.__weights | |
target_results = np.ndarray(train_labels.shape) | |
target_results[train_labels == labels[0]] = 0.1 | |
target_results[train_labels == labels[1]] = 0.9 | |
# Eigentlich wählt mal 0 und 1, aber da die Sigmoid-Funktion nicht | |
# perfekt ist, ist Training mit 0.1 und 0.9 numerisch besser | |
for iteration in range(self.__iterations): | |
output_1 = self.__calculate(train_samples, iteration) | |
output_0 = self.__calculate(train_samples, iteration, layer=0) | |
error_term = -(target_results - output_1.T) * self.__deriv(output_1.T) | |
error_term = error_term.repeat(3, axis=0).T * output_0 | |
self.__errors[iteration] = np.mean(error_term) | |
weights[iteration+1] = np.mean(weights[iteration] - self.__learning_rate * error_term, axis=0) | |
def __output_to_label(self, output): | |
if output < 0.5: | |
return self.__labels[0] | |
return self.__labels[1] | |
def __calculate(self, samples, iteration, layer=1): | |
weights = self.__weights | |
init_values = np.ones((samples.shape[0], 1)) * -1 | |
input_arr = np.hstack((init_values, samples)) | |
if layer == 1: | |
return self.__func(input_arr.dot(weights[iteration].T)) | |
return input_arr | |
def classify(self, test_samples, iteration=None): | |
labels = self.__labels | |
weights = self.__weights | |
if iteration is None: | |
iteration = self.__iterations | |
vfunc = np.vectorize(lambda x : self.__output_to_label(x)) | |
return vfunc(self.__calculate(test_samples, iteration)) | |
def get_error_terms(self): | |
return self.__errors | |
# }}} | |
def plot_nn(ax, data, labels, nn, step_size=0.1, iteration=None): # {{{ | |
predict_func = lambda x : nn.classify(x, iteration=iteration) | |
plot_hyperplane(ax, data, labels, predict_func, step_size) | |
# }}} | |
def plot_nn_anim(fig, ax, data, labels, nn, step_size=0.1): # {{{ | |
def update(udata): | |
iteration, = udata | |
predict_func = lambda x : nn.classify(x, iteration=iteration) | |
return plot_hyperplane(ax, data, labels, predict_func, step_size) | |
def generator(): | |
iteration = 0 | |
while iteration < 1000: | |
yield iteration, | |
iteration += 1 | |
ani = animation.FuncAnimation(fig, update, generator, interval=10) | |
plt.show() | |
# }}} | |
def plot_hyperplane(ax, data, labels, predict_func, step_size=0.1, animation=False): # {{{ | |
labels = labels.astype(int) | |
label_max = float(labels.max()) + 1 | |
data_min = data.min(axis=0) - 1 | |
data_max = data.max(axis=0) + 1 | |
xx, yy = np.meshgrid(np.arange(data_min[0], data_max[0], step_size), np.arange(data_min[1], data_max[1], step_size)) | |
zz = predict_func(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape) | |
zz = np.array(zz, dtype=int) | |
cmap = cm.get_cmap('gist_rainbow') | |
c = cmap(zz/label_max) | |
ax.imshow(c, interpolation='nearest', extent=(data_min[0],data_max[0],data_min[1],data_max[1]),origin='lower',aspect='auto', animated=animation) | |
c = cmap(labels/label_max) | |
scat = ax.scatter(data[:,0], data[:,1], c=c, animated=animation) | |
ax.set_xlim(data_min[0], data_max[0]) | |
ax.set_ylim(data_min[1], data_max[1]) | |
return scat | |
# }}} | |
n_samples = 1000 | |
mean1 = np.array([5, 3]) | |
cov1 = np.array([[ 3.0, .2 ], | |
[ .5, 4.0 ]]); | |
mean2 = np.array([2, 8]) | |
cov2 = np.array([[ 1.8, .4 ], | |
[ .7, 2.7 ]]); | |
samples1 = np.random.multivariate_normal(mean1, cov1, n_samples) | |
samples2 = np.random.multivariate_normal(mean2, cov2, n_samples) | |
samples = np.concatenate((samples1, samples2)) | |
labels = np.concatenate((np.zeros((n_samples)), np.ones((n_samples)))) | |
plt.scatter(samples[:, 0], samples[:, 1], c=labels, cmap=cm.get_cmap('jet')) | |
plt.show() | |
network = NeuralNetwork(0.3, 1000, 'Sigmoid') | |
network.estimate(samples, labels) | |
fig, ax = plt.subplots() | |
ax.set_title('Untrainiertes Netz') | |
plot_nn(ax, samples, labels, network, iteration=0) | |
plt.show() | |
fig, ax = plt.subplots() | |
ax.set_title('Trainiertes Netz') | |
plot_nn(ax, samples, labels, network) | |
plt.show() | |
fig, ax = plt.subplots() | |
plot_nn_anim(fig, ax, samples, labels, network) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment