Commit 2967d54d by Eva Lina Fesefeldt

### MNIST LambdaMax Plot

parent 5f0faef9

#### Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.
 ... ... @@ -2,11 +2,12 @@ import numpy as np from numpy.linalg import norm import tensorflow as tf import matplotlib.pyplot as plt from tensorflow import keras from generate_dataset import generate_tictactoe from helper import matrix_trainable_shape_to_flat_shape for size_hidden_layer in range(1,100): for size_hidden_layer in range(1,30): # Erstelle Daten train_set, train_labels = generate_tictactoe() norms = norm(train_labels, ord=1, axis=0) ... ... @@ -14,8 +15,8 @@ for size_hidden_layer in range(1,100): number_of_parameters = 9*size_hidden_layer + size_hidden_layer + 3 * size_hidden_layer + 3 # KNN erzeugen from tf.keras.models import Sequential from tf.keras.layers import Dense from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(size_hidden_layer, input_dim = 9,activation='sigmoid')) ... ...
File deleted
This diff is collapsed.
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
 import numpy as np from scipy.special import comb import matplotlib.pyplot as plt import math # 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('float32') labels = tictactoe_labels(X).astype('float32') return X, labels
 import numpy as np from tensorflow import keras from tensorflow.keras import layers from autograd_minimize.tf_wrapper import tf_function_factory from autograd_minimize import minimize import tensorflow as tf from keras.models import Sequential from keras.layers import Dense for i in range(1000): size_hidden_layer = 20 model = Sequential() model.add(Dense(size_hidden_layer, input_dim = 9,activation='sigmoid')) model.add(Dense(3, input_dim=size_hidden_layer, activation='sigmoid')) list_weights_bias = model.get_weights() filename_W_1 = "initializersn_20/W_1_n20_" + str(i) + ".npy" filename_b_1 = "initializersn_20/b_1_n20_" + str(i) + ".npy" filename_W_2 = "initializersn_20/W_2_n20_" + str(i) + ".npy" filename_b_2 = "initializersn_20/b_2_n20_" + str(i) + ".npy" np.save(filename_W_1, list_weights_bias[0]) np.save(filename_b_1, list_weights_bias[1]) np.save(filename_W_2, list_weights_bias[2]) np.save(filename_b_2, list_weights_bias[3])
 import tensorflow as tf import numpy as np def reelles_skalarprodukt_trainable_shape(v_1, v_2): sum = 0 for i in range(len(v_1)): sum += np.sum(v_1[i]*v_2[i]) return sum # Todo umschreiben allgemeines Model def vector_flat_shape_to_trainable_shape(v): try: p,m = v.shape except: p = v.shape[0] n = int((p-3)/13) # Größe der versteckten Schicht slice1 = 9*n slice2 = 9*n+n slice3 = 9*n+n+n*3 v1 = tf.reshape(v[:slice1], (9,n)) v2 = tf.reshape(v[slice1:slice2], (n,)) v3 = tf.reshape(v[slice2:slice3], (n,3)) v4 = tf.reshape(v[slice3:], (3,)) unit_vector_trainable_shape = [v1, v2, v3, v4] return unit_vector_trainable_shape # Todo umschreiben für allgemeines n, allgemeines Model def vector_trainable_shape_to_flat_shape(list): p = 0 #Number of Parameters p for i in range(len(list)): try: n,m = list[i].shape except: n = list[i].shape[0] m = 1 finally: p += n*m n = int((p-3)/13) # Größe der versteckten Schicht v1 = list[0] v2 = list[1] v3 = list[2] v4 = list[3] slice1 = 9*n slice2 = 9*n+n slice3 = 9*n+n+n*3 v = np.zeros((p,)) v[:slice1] = np.reshape(v1, (9*n,)) v[slice1:slice2] = np.reshape(v2, (n,)) v[slice2:slice3] = np.reshape(v3, (3*n,)) v[slice3:] = np.reshape(v4, (3,)) return v def matrix_trainable_shape_to_flat_shape(model, h): layer1 = model.layers[0] layer2 = model.layers[1] n_params = tf.reduce_prod(layer1.kernel.shape) + tf.reduce_prod(layer2.kernel.shape) + tf.reduce_prod(layer1.bias.shape) + tf.reduce_prod(layer2.bias.shape) #h[0] ist die Ableitung des Gradienten nach den Gewichten Layer 1 n_params_D_weights_1 = tf.reduce_prod(layer1.kernel.shape) H_weights_1 = tf.reshape(h[0], [n_params, n_params_D_weights_1]) #h[1] ist die Ableitung des Gradienten nach den Biasen Layer 1 n_params_D_bias_1 = tf.reduce_prod(layer1.bias.shape) H_bias_1 = tf.reshape(h[1], [n_params, n_params_D_bias_1]) #h[2] ist die Ableitung des Gradienten nach den Gewichten Layer 2 n_params_D_weights_2 = tf.reduce_prod(layer2.kernel.shape) H_weights_2 = tf.reshape(h[2], [n_params, n_params_D_weights_2]) #h[3] ist die Ableitung des Gradienten nach den Biasen Layer 2 n_params_D_bias_2 = tf.reduce_prod(layer2.bias.shape) H_bias_2 = tf.reshape(h[3], [n_params, n_params_D_bias_2]) # Hesse-Matrix zusammensetzen ToDo vorher allokieren h_mat = tf.concat([H_weights_1, H_bias_1, H_weights_2, H_bias_2], axis = 1) return h_mat def matrix_flat_shape_to_trainable_shape(model, A): layer1 = model.layers[0] layer2 = model.layers[1] n_params, m = A.shape A_trainable = [] #A_trainable.append(tf.reshape()) \ No newline at end of file
File deleted
File deleted
File deleted
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!