Last active
December 22, 2016 08:11
-
-
Save pyben/41d12f33c9a4717e3adf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 __future__ import absolute_import | |
| from __future__ import print_function | |
| import numpy as np | |
| np.random.seed(1337) # for reproducibility | |
| from keras.datasets import mnist | |
| from keras.models import Sequential | |
| from keras.layers import containers | |
| from keras.layers.core import Dense, AutoEncoder | |
| from keras.layers.noise import GaussianNoise | |
| from keras.utils import np_utils | |
| batch_size = 64 | |
| nb_classes = 10 | |
| nb_epoch = 0 | |
| nb_hidden_layers = [784, 600, 500, 400] | |
| nb_noise_layers = [0.5, 0.2, 0.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(-1, 784) | |
| X_test = X_test.reshape(-1, 784) | |
| X_train = X_train.astype("float32") / 255.0 | |
| X_test = X_test.astype("float32") / 255.0 | |
| 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) | |
| # Layer-wise pre-training | |
| trained_encoders = [] | |
| X_train_tmp = X_train | |
| for i, (n_in, n_out) in enumerate( | |
| zip(nb_hidden_layers[:-1], nb_hidden_layers[1:]), start=1): | |
| print('Pre-training the layer: Input {} -> Output {}' | |
| .format(n_in, n_out)) | |
| ae = Sequential() | |
| encoder = containers.Sequential([ | |
| GaussianNoise(nb_noise_layers[i - 1], input_shape=(n_in,)), | |
| Dense(input_dim=n_in, output_dim=n_out, | |
| init='uniform', activation='sigmoid') | |
| ]) | |
| decoder = containers.Sequential([ | |
| Dense(input_dim=n_out, output_dim=n_in, activation='sigmoid') | |
| ]) | |
| ae.add(AutoEncoder(encoder=encoder, decoder=decoder, | |
| output_reconstruction=False)) | |
| ae.compile(loss='mean_squared_error', optimizer='rmsprop') | |
| ae.fit(X_train_tmp, X_train_tmp, batch_size=batch_size, nb_epoch=nb_epoch) | |
| # Store trainined weight | |
| trained_encoders.append(ae.layers[0].encoder) | |
| # Update training data | |
| X_train_tmp = ae.predict(X_train_tmp) | |
| # Fine-tuning | |
| print('Fine-tuning') | |
| model = Sequential() | |
| for encoder in trained_encoders: | |
| model.add(encoder) | |
| model.add(Dense(input_dim=nb_hidden_layers[-1], | |
| output_dim=nb_classes, activation='softmax')) | |
| model.compile(loss='categorical_crossentropy', optimizer='rmsprop') | |
| model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, | |
| show_accuracy=True, validation_data=(X_test, Y_test)) | |
| score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0) | |
| print('Test score:', score[0]) | |
| print('Test accuracy:', score[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excuse me, I couldn't find out the model of "from keras.layers import containers""from keras.models import Sequential". Could you please tell me the whole project?