023. install tensorflow and keras
# @
# You should create setting file for keras
# First, you should create keras folder
mkdir c:/user/keras
# Then, you should create keras.json file
nano c:/user/keras/keras.json
# You should write following code in keras.json
{
"image_dim_ordering":"tf",
"epsilon":1e-07,
"floatx":"float32",
"backend":"tensorflow"
}
# Exit from nano editor by pressing ctrl x
# @
# Let's test mnist with keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import Adam
from keras.utils import np_utils
# I load mnist data and divide them into train data and test data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# I convert data into float32 and then normalize them
X_train = X_train.reshape(60000, 784).astype('float32')
X_test = X_test.reshape(10000, 784).astype('float')
X_train /= 255
X_test /= 255
# I convert label data into categorical array
# whose elements are ranged from 0 to 9
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
# I define structure of predicting model
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))
# I implement defined model from above
model.compile(
loss='categorical_crossentropy',
optimizer=Adam(),
metrics=['accuracy'])
# For learning
hist = model.fit(X_train,y_train)
# For evaluating
score = model.evaluate(X_test,y_test,verbose=1)
print('loss=',score[0])
print('accuracy=',score[1])
# You should run this python file
# When we load data and let model to train full mnist train data with scikit-learn,
# it takes 40 minutes
# Keras takes less time