Modify single layer training script, add 2-layer training script

This commit is contained in:
Maximiliana Behnke 2016-09-14 18:56:13 +02:00
parent aa22b47fcc
commit ea04f8a6ba
2 changed files with 76 additions and 4 deletions

View File

@ -0,0 +1,72 @@
#!/usr/bin/env python
import sys
import os
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
def softmax(x):
return np.exp(x) / np.sum(np.exp(x), axis=1)[:, None]
def baseline_model(pixels_count, classes_count):
model = Sequential()
model.add(Dense(100, input_dim=pixels_count, init='normal', activation='tanh'))
model.add(Dense(classes_count, input_dim=100, init='normal', activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
if __name__ == "__main__":
### Load trainset from mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
### Flatten pictures into vectors
pixels_count = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(X_train.shape[0], pixels_count).astype('float32')
print "X shape: ", X_train.shape
X_test = X_test.reshape(X_test.shape[0], pixels_count).astype('float32')
### Normalize data to (0, 1)
X_train = X_train / 255
X_test = X_test / 255
### Change classes to one hot encoding matrixes
y_train = np_utils.to_categorical(y_train)
classes_count = y_train.shape[1]
print "Y shape: ", y_train.shape
y_test = np_utils.to_categorical(y_test)
# Train weight matrix
# Build the model
model = baseline_model(pixels_count, classes_count)
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=10, batch_size=200, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
### Weight and bias matrixes - we extract them from the model
# weights_ones = np.ones((pixels_count, classes_count))
# print weights_ones.shape
weights1, bias1, weights2, bias2 = model.get_weights()
### Save model to npz files
if not os.path.exists("test_model_multi"):
os.makedirs("test_model_multi")
# np.savez("test_model_multi/model", *model)
np.savez("test_model_multi/model", weights1 = weights1, bias1 = bias1, weights2 = weights2, bias2 = bias2)
print "Model saved! Check test_model_multi directory"

View File

@ -84,8 +84,8 @@ if __name__ == "__main__":
# print np.count_nonzero(lr)i
### Save model to npz files
if not os.path.exists("test_model"):
os.makedirs("test_model")
np.savez("test_model/model", weights = weights, bias = bias)
if not os.path.exists("test_model_single"):
os.makedirs("test_model_single")
np.savez("test_model_single/model", weights = weights, bias = bias)
print "Model saved! Check test_model directory"
print "Model saved! Check test_model_single directory"