Bibliothèque pour faciliter l'utilisation de Theano et TensorFlow
Instance EC2 (AWS) g2.2xlarge (Oregon = ouest des États-Unis) Python 2.7.6 TensorFlow 0.8.0
L'instance AWS a été initialisée à l'aide de l'AMI d'une autre personne, mais si vous souhaitez l'introduire vous-même, reportez-vous à ce qui suit
Faites comme dans Documentation En supposant que TensorFlow est déjà installé, ajoutez "udo "si nécessaire
pip install scipy
pip install scikit-learn
pip install pyyaml
apt-get install libhdf5-dev
pip install h5py
pip install keras
Une fois de python
import keras
Exécutez Keras en tant que et modifiez '~ / .keras / keras.json' comme suit
"backend": "theano"
↓
"backend": "tensorflow"
mnist.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Activation, Flatten, Dense
from keras.callbacks import EarlyStopping
(X_train, y_train), (X_test, y_test) = mnist.load_data()
nb_classes = 10
X_train = X_train.reshape(-1, 1, 28, 28).astype('float32')
X_test = X_test.reshape(-1, 1, 28, 28).astype('float32')
X_train /= 255
X_test /= 255
y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Convolution2D(nb_filter = 16, nb_row = 3, nb_col = 3, border_mode = 'same', input_shape = (1, 28, 28)))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filter = 32, nb_row = 3, nb_col = 3, border_mode = 'same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2), border_mode = 'same'))
model.add(Convolution2D(nb_filter = 64, nb_row = 3, nb_col = 3, border_mode = 'same'))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filter = 128, nb_row = 3, nb_col = 3, border_mode = 'same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2), border_mode = 'same'))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
early_stopping = EarlyStopping(monitor = 'val_loss', patience = 2)
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(X_train, y_train, nb_epoch = 5, batch_size = 100, callbacks = [early_stopping])
score = model.evaluate(X_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Deep Learning Library Keras Keras Documentation
Recommended Posts