Commit 206ed83c authored by Eva Lina Fesefeldt's avatar Eva Lina Fesefeldt
Browse files

MNIST Plot

parent 983ff5a4
import tensorflow as tf
import numpy as np
# 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
x_train = tf.reshape(x_train, (60000, 28*28))
input_dim = 28*28
epochs = 100
for label in range(10):
which_labels = y_train == label
x_selection = x_train[which_labels]
img = np.reshape(x_selection[0], (28,28))
filename = "results/single_number_" + str(label) + ".npy"
np.save(filename, img)
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 8})
plt.figure(figsize=(6, 2.4), constrained_layout=True)
for i in range(10):
filename = "results/single_number_" + str(i) + ".npy"
img = np.load(filename)
ax = plt.subplot(2, 5, i + 1)
ax.imshow(img, cmap='gray')
plt.axis('off')
plt.show()
\ No newline at end of file
import tensorflow as tf
# MNIST laden
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = tf.reshape(x_train, (60000, 28*28))
y_train = tf.one_hot(y_train, depth=10)
# Modell erzeugen
size_hidden_layer = 30
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 = 28*28,activation='sigmoid'))
model.add(Dense(10, input_dim=size_hidden_layer, activation='sigmoid'))
loss_fn = tf.keras.losses.MeanSquaredError()
model.compile(optimizer="adam", loss = loss_fn)
model.fit(x_train, y_train, epochs=10000)
#!/bin/bash -l
#SBATCH -p gpu
#SBATCH --ntasks 1
#SBATCH --cpus-per-task 4
#SBATCH --gres gpu:2
#SBATCH --constraint gpu_k20
#SBTACH --mem-per-cpu 2000
#SBATCH --time 30:00:00
#SBATCH --mail-type END,FAIL,REQUEUE
#SBATCH --mail-user eva.fesefeldt@tuhh.de
conda activate tf-gpu
python3 main.py
exit
import tensorflow as tf
import numpy as np
# MNIST laden
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = tf.reshape(x_train, (60000, 28*28))
y_train = tf.one_hot(y_train, depth=10)
input_dim = 28*28
epochs = 100
# Modell erzeugen
from tf.keras.models import Sequential
from tf.keras.layers import Dense
from tf.keras import backend as k
loss_fn = tf.keras.losses.MeanSquaredError()
mini_loss = np.zeros((1000,epochs))
for size_hidden_layer in range(1, 1000):
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'))
#list_of_weights_and_biases = model.get_weights()
model.summary()
model.compile(optimizer="adam", loss = loss_fn)
history = model.fit(x_train, y_train, epochs=epochs, verbose=False)
mini_loss[size_hidden_layer-1,:] = history.history['loss']
np.save("results/mini_loss_vs_size_hidden_layer.npy", mini_loss)
import numpy as np
import matplotlib.pyplot as plt
loss_vs_hidden_layer = np.load("results/mini_loss_vs_size_hidden_layer.npy")
for size_hidden_layer in range(0,51, 5):
plt.semilogy(loss_vs_hidden_layer[size_hidden_layer,:], label="n="+str(size_hidden_layer+1))
plt.xlabel("Epoche")
plt.ylabel("Loss")
plt.legend()
plt.show()
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