Created
May 31, 2016 03:38
-
-
Save henry0312/9c1aee0a4330c860ff7b2bb139acbb15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import * | |
from keras.optimizers import * | |
from keras.regularizers import * | |
from keras.datasets import mnist | |
from keras.utils import np_utils | |
def create_model(): | |
x = Input(shape=(784,), name='x') | |
h = BatchNormalization(mode=2)(x) | |
y = Dense(64, init='uniform', activation='relu', W_regularizer='l2', b_regularizer='l2')(h) | |
model = Model(input=x, output=y) | |
x1 = Input(shape=(784,), name='x1') | |
x2 = Input(shape=(784,), name='x2') | |
y1 = model(x1) | |
y2 = model(x2) | |
merged_vector = merge([y1, y2], mode=lambda x: x[0] - x[1], output_shape=(64,)) | |
output = Dense(10, activation='sigmoid')(merged_vector) | |
net = Model(input=[x1, x2], output=output) | |
net.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) | |
return model, net | |
batch_size = 128 | |
nb_classes = 10 | |
nb_epoch = 1 | |
# the data, shuffled and split between train and test sets | |
(X_train, y_train), (X_test, y_test) = mnist.load_data() | |
X_train = X_train.reshape(60000, 784) | |
X_test = X_test.reshape(10000, 784) | |
X_train = X_train.astype('float32') | |
X_test = X_test.astype('float32') | |
X_train /= 255 | |
X_test /= 255 | |
print(X_train.shape[0], 'train samples') | |
print(X_test.shape[0], 'test samples') | |
# convert class vectors to binary class matrices | |
Y_train = np_utils.to_categorical(y_train, nb_classes) | |
Y_test = np_utils.to_categorical(y_test, nb_classes) | |
model, net = create_model() | |
json_string = net.to_json() | |
open('my_model_architecture.json', 'w').write(json_string) | |
history = net.fit([X_train, X_train], Y_train, | |
batch_size=batch_size, nb_epoch=nb_epoch, | |
verbose=1, validation_data=([X_test, X_test], Y_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment