Commit f1904284 authored by Eva Lina Fesefeldt's avatar Eva Lina Fesefeldt
Browse files

Stand nach Präsentation

parent 206ed83c
......@@ -24,5 +24,6 @@ for size_hidden_layer in range(1,30,5):
ax.set_xlabel('n')
ax.set_ylabel('relativer Eigenwertindex')
ax.set_zlabel('Betrag des Eigenwertes')
for t in ax.zaxis.get_major_ticks(): t.label.set_fontsize(7)
#ax.set_zscale('log')
plt.show()
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.framework.dtypes import as_dtype
from tensorflow.python.ops.gen_math_ops import xlog1py_eager_fallback
import matplotlib.pyplot as plt
from math import sqrt
from generate_dataset import generate_tictactoe
from jvp import _back_over_back_hvp
from helper import *
from tensorflow.python.eager import forwardprop
from helper import reelles_skalarprodukt_trainable_shape
# Erstelle Daten
train_set, train_labels = generate_tictactoe()
dataset = tf.data.Dataset.from_tensor_slices((train_set, train_labels))
# Modell erzeugen
size_hidden_layer = 30
number_of_epochs = 10000
number_of_parameters = 9*size_hidden_layer + size_hidden_layer + 3 * size_hidden_layer + 3
from keras.models import Sequential
from keras.layers import Dense
from keras import backend as k
model = Sequential()
model.add(Dense(size_hidden_layer, input_dim = 9,activation='sigmoid'))
model.add(Dense(3, input_dim=size_hidden_layer, activation='sigmoid'))
# Gewichte und Biase initialisieren, um nachher Vergleichbarkeit zu haben
filename_W1 = "initializers/W_1_n" + str(size_hidden_layer) + ".npy"
filename_b1 = "initializers/b_1_n" + str(size_hidden_layer) + ".npy"
filename_W2 = "initializers/W_2_n" + str(size_hidden_layer) + ".npy"
filename_b2 = "initializers/b_2_n" + str(size_hidden_layer) + ".npy"
W_1 = np.load(filename_W1)
b_1 = np.load(filename_b1)
W_2 = np.load(filename_W2)
b_2 = np.load(filename_b2)
list_of_weights_and_biases = [W_1, b_1, W_2, b_2]
model.set_weights(list_of_weights_and_biases)
loss_fn = tf.keras.losses.MeanSquaredError()
opt = tf.keras.optimizers.SGD(learning_rate=10)
model.compile(optimizer=opt, loss=loss_fn, metrics=loss_fn)
history = model.fit(train_set, train_labels, batch_size=126, epochs = 10000, verbose=1)
filename = "results/MSE_loss_gd.npy"
np.save(filename, history.history['loss'])
\ No newline at end of file
......@@ -8,20 +8,23 @@ hessian_hand = np.load("results/hesse_analytisch_n1.npy")
hessian_nested_grad_tapes = np.load("results/hesse_nested_grad_tapes.npy")
err_abs = hessian_hand - hessian_nested_grad_tapes
err_rel = (hessian_hand - hessian_nested_grad_tapes) / hessian_hand
#err_rel = (hessian_hand - hessian_nested_grad_tapes) / hessian_hand
fig, (ax1, ax2) = plt.subplots(1,2, figsize= (6,3), constrained_layout = True)
#fig, (ax1, ax2) = plt.subplots(1,2, figsize= (6,3), constrained_layout = True)
fig, ax1 = plt.subplots(1,1, figsize=(3,3), constrained_layout = True)
lim_abs = np.max(np.abs(err_abs))
lim_rel = np.max(np.abs(err_rel))
#lim_rel = np.max(np.abs(err_rel))
im1 = ax1.imshow(err_abs, vmin=-lim_abs, vmax=lim_abs, cmap='seismic')
ax1.set_title("Absoluter Fehler")
#fig.colorbar(im1, ax=ax1)
im2 = ax2.imshow(err_rel, vmin=-lim_rel, vmax=lim_rel, cmap='seismic')
ax2.set_title("Relativer Fehler")
fig.colorbar(im1, ax=ax2)
#ax1.set_title("Komponentenweise Abweichung")
fig.colorbar(im1, ax=ax1)
#lim_abs = np.max(np.abs(err_abs))
#im2 = ax2.imshow(err_rel, vmin=-lim_rel, vmax=lim_rel, cmap='seismic')
#ax2.set_title("Relativer Fehler")
#fig.colorbar(im1, ax=ax2)
ax1.axis('off')
ax2.axis('off')
#ax2.axis('off')
plt.show()
\ No newline at end of file
import numpy as np
from scipy.special import comb
# Finde alle Vektoren x \in {0,1}^n mit genau 5 Einträgen = 1 (weiß)
# Gibt eine Matrix der Größe (N,n) zurück, deren Zeilen die gesuchten Vektoren sind
def find_combinations(n,k):
# Rekursionsanfang
if k==0:
return np.zeros(n)
if n==1 & k==1:
return np.array([1])
if n==1 & k==0:
return np.array([0])
# Anzahl der möglichen Kombinationen: k aus n auswählen, Matrix anlegen
N = int(comb(n,k))
X = np.zeros((N,n))
# Setze den ersten Eintrag auf 1 (weiß) und rufe das Subproblem auf
number_of_combinations_problem_1 = int(comb(n-1,k-1))
X[0:number_of_combinations_problem_1,0] = 1
X[0:number_of_combinations_problem_1,1:n] = find_combinations(n-1,k-1)
if number_of_combinations_problem_1 == N:
return X
# Belasse den ersten Eintrag bei 0 (schwarz) und rufe das Subproblem auf
X[number_of_combinations_problem_1:,1:n] = find_combinations(n-1,k)
return X
# (weiß gewinnt, schwarz gewinnt, niemand gewinnt)
def winner_one_line(x1,x2,x3):
if x1 != x2 or x2 != x3:
return np.array([0,0,1]).T
if x1 == 1:
return np.array([1,0,0]).T
return np.array([0,1,0]).T
def one_tictactoe_label(x):
strikes = np.zeros((3, 8))
# Alle Möglichkeiten zu gewinnen
strikes[:,0] = winner_one_line(x[0], x[4], x[8]) # Diagonale
strikes[:,1] = winner_one_line(x[2], x[4], x[6]) # Antidiagonale
strikes[:,2] = winner_one_line(x[0], x[1], x[2]) # Horizontal 1
strikes[:,3] = winner_one_line(x[3], x[4], x[5]) # Horizontal 2
strikes[:,4] = winner_one_line(x[6], x[7], x[8]) # Horizontal 3
strikes[:,5] = winner_one_line(x[0], x[3], x[6]) # Vertikal 1
strikes[:,6] = winner_one_line(x[1], x[4], x[7]) # Vertikal 2
strikes[:,7] = winner_one_line(x[2], x[5], x[8]) # Vertikal 3
# Eine Farbe gewinnt, falls sie mindestens einen Strike hat und die andere Farbe keine Strikes hat
strikes_white = np.sum(strikes[0,:])
strikes_black = np.sum(strikes[1,:])
# Weiß gewinnt
if strikes_black == 0 and strikes_white > 0:
return np.array([1,0,0])
# Schwarz gewinnt
if strikes_white == 0 and strikes_black > 0:
return np.array([0,1,0])
return np.array([0,0,1])
def tictactoe_labels(X):
N,n = X.shape
labels = np.zeros((N,3))
for i in range(N):
labels[i,:] = one_tictactoe_label(X[i,:])
return labels.astype(float)
def generate_tictactoe():
n = 9
k = 5
N = int(comb(n,k))
X = np.zeros((N,n))
X = find_combinations(n,k).astype(float)
labels = tictactoe_labels(X)
return X, labels
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment