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

Stand nach Präsentation

parent 206ed83c
import numpy as np
from numpy.linalg import norm
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.python.keras import optimizers
from tensorflow.python.keras.backend import learning_phase
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import backend as k
# Aufruf: main.py opt --batch_size 32 --LR 0.01 --epochs 100
# Abfangen der Eingabedaten
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("opt", type=str) # "sgd_with_decay", "sgd", "adam", "adagrad"
parser.add_argument("--batch_size", type=int, dest="batch_size")
parser.add_argument("--LR", type=float, dest="LR")
parser.add_argument("--epochs", type=int, dest="epochs")
args = parser.parse_args()
optimizer_str = args.opt
if args.batch_size is not None:
batch_size = args.batch_size
else:
batch_size = 32
print("Setting batch size to default (32)")
if args.LR is not None:
LR = args.LR
else:
LR = 0.01
print("Setting learning rate to default (0.01)")
if args.epochs is not None:
epochs = args.epochs
else:
epochs = 10000
print("Setting batch sizeepochs to default (10000)")
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
# MNIST laden
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
input_dim = 28*28
x_train = tf.reshape(x_train, (60000, 28*28))
y_train = tf.one_hot(y_train, depth=10)
# Modell erzeugen
size_hidden_layer = 200
model = Sequential()
model.add(Dense(size_hidden_layer, input_dim = input_dim, activation='sigmoid'))
model.add(Dense(10, 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()
# Initialen Loss speichern
loss = loss_fn(model.predict(x_train), y_train)
loss_numpy = np.zeros(epochs+1)
loss_numpy[0] = loss
filename_loss = "results/MSE_loss_" + optimizer_str + "_batch" + str(batch_size) + "_LR" + str(LR) + "_epochs" + str(epochs) + "_n" + str(size_hidden_layer)
# Training
model.compile(optimizer='adam', loss=loss_fn)
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0)
loss_numpy[1:] = history.history['loss']
np.save(filename_loss, loss_numpy)
import numpy as np
import os, sys
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['KMP_DUPLICATE_LIB_OK']='True'
os.environ["KMP_WARNINGS"] = "FALSE"
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import backend as k
import time
# Import hessianlearn repository
sys.path.append( os.environ.get('HESSIANLEARN_PATH', "../../"))
from hessianlearn import *
from generate_dataset import generate_tictactoe
# MNIST laden
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# normalisieren
x_train, x_test = x_train / 255.0, x_test / 255.0
# Reshape und Konvertieren in one-hot-Encoding
input_dim = 28*28
x_train = np.reshape(x_train, (60000, 28*28))
y_train_one_hot = np.zeros((y_train.size, y_train.max()+1))
y_train_one_hot[np.arange(y_train.size),y_train] = 1
y_train = y_train_one_hot
# Modell erzeugen
size_hidden_layer = 200
number_of_epochs = 200
model = Sequential()
model.add(Dense(size_hidden_layer, input_dim = input_dim, activation='sigmoid'))
model.add(Dense(10, 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)
model.predict(x_train)
problem = ClassificationProblem(model, dtype=tf.float32)
regularization = L2Regularization(problem, gamma = 0)
# Instante the data object
train_data = {problem.x:x_train}
validation_data = {problem.x:x_train}
data = Data({problem.x:x_train, problem.y_true:y_train}, batch_size=60000, validation_data_size=0, hessian_batch_size=60000)
HLModelSettings = HessianlearnModelSettings()
HLModelSettings['optimizer'] = 'incg'
HLModelSettings['fixed_step'] = False
HLModelSettings['max_sweeps'] = 100
HLModelSettings['alpha'] = 5e-2
HLModelSettings['printing_sweep_frequency'] = 10
HLModel = HessianlearnModel(problem,regularization,data, settings=HLModelSettings)
HLModel.fit()
import pickle
import numpy as np
# Python pickle Datein laden und Ergebnisse plotten
def dict_to_numpy(dictionary):
A = np.array([])
for i in range(len(dictionary)):
A = np.append(A, dictionary[i])
return A
infile = open('./_logging/-2021-08-05-incg-dW=159010.pkl','rb')
meta = pickle.load(infile)
infile.close()
loss_incg = dict_to_numpy(meta["train_loss"])
no_epochs_gd = loss_incg.shape
import matplotlib.pyplot as plt
plt.semilogy(loss_incg, 'r.', label="Inexaktes Newton-CG mit E-W-Abbruchbedingung, L2-Regularisierung und Liniensuche")
plt.title("TTT")
plt.xlabel("Epochen")
plt.legend()
plt.show()
#plt.savefig("HF_vs_GD.pdf")
......@@ -204,7 +204,7 @@ n,m = train_set.shape
mpl.rcParams.update({'font.size': 8})
plt.figure(figsize=(6, 8), constrained_layout=True)
ncols = 5
ncols = 8
nrows = math.ceil(n / ncols)
for i in range(n):
ax = plt.subplot(nrows, ncols, i + 1)
......
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