Logistic Regression

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.datasets import mnist
from keras.utils import np_utils
Using TensorFlow backend.
batch_size = 128
nb_classes = 10
nb_epoch = 100
# Load MNIST dataset
(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
Y_Train = np_utils.to_categorical(y_train, nb_classes)
Y_Test = np_utils.to_categorical(y_test, nb_classes)
# Logistic regression model
model = Sequential()
model.add(Dense(output_dim=10, input_shape=(784,), init='normal', activation='softmax'))
model.compile(optimizer=SGD(lr=0.05), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
dense_1 (Dense)                  (None, 10)            7850        dense_input_1[0][0]              
====================================================================================================
Total params: 7850
____________________________________________________________________________________________________
# Train
history = model.fit(X_train, Y_Train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1)
Epoch 1/100
60000/60000 [==============================] - 2s - loss: 0.7221 - acc: 0.8227     
Epoch 2/100
60000/60000 [==============================] - 1s - loss: 0.4324 - acc: 0.8856     
Epoch 3/100
60000/60000 [==============================] - 1s - loss: 0.3858 - acc: 0.8948     
Epoch 4/100
60000/60000 [==============================] - 1s - loss: 0.3623 - acc: 0.9003     
Epoch 5/100
60000/60000 [==============================] - 1s - loss: 0.3474 - acc: 0.9034     
Epoch 6/100
60000/60000 [==============================] - 1s - loss: 0.3368 - acc: 0.9060     
Epoch 7/100
60000/60000 [==============================] - 1s - loss: 0.3287 - acc: 0.9082     
Epoch 8/100
60000/60000 [==============================] - 1s - loss: 0.3223 - acc: 0.9100     
Epoch 9/100
60000/60000 [==============================] - 1s - loss: 0.3170 - acc: 0.9113     
Epoch 10/100
60000/60000 [==============================] - 1s - loss: 0.3124 - acc: 0.9127     
Epoch 11/100
60000/60000 [==============================] - 1s - loss: 0.3087 - acc: 0.9138     
Epoch 12/100
60000/60000 [==============================] - 1s - loss: 0.3055 - acc: 0.9146     
Epoch 13/100
60000/60000 [==============================] - 1s - loss: 0.3025 - acc: 0.9156     
Epoch 14/100
60000/60000 [==============================] - 1s - loss: 0.2999 - acc: 0.9161     
Epoch 15/100
60000/60000 [==============================] - 1s - loss: 0.2976 - acc: 0.9171     
Epoch 16/100
60000/60000 [==============================] - 1s - loss: 0.2955 - acc: 0.9173     
Epoch 17/100
60000/60000 [==============================] - 1s - loss: 0.2935 - acc: 0.9182     
Epoch 18/100
60000/60000 [==============================] - 1s - loss: 0.2918 - acc: 0.9184     
Epoch 19/100
60000/60000 [==============================] - 1s - loss: 0.2902 - acc: 0.9188     
Epoch 20/100
60000/60000 [==============================] - 1s - loss: 0.2888 - acc: 0.9195     
Epoch 21/100
60000/60000 [==============================] - 1s - loss: 0.2873 - acc: 0.9196     
Epoch 22/100
60000/60000 [==============================] - 1s - loss: 0.2860 - acc: 0.9202     
Epoch 23/100
60000/60000 [==============================] - 1s - loss: 0.2849 - acc: 0.9207     
Epoch 24/100
60000/60000 [==============================] - 1s - loss: 0.2836 - acc: 0.9211     
Epoch 25/100
60000/60000 [==============================] - 1s - loss: 0.2825 - acc: 0.9216     
Epoch 26/100
60000/60000 [==============================] - 1s - loss: 0.2815 - acc: 0.9216     
Epoch 27/100
60000/60000 [==============================] - 1s - loss: 0.2806 - acc: 0.9218     
Epoch 28/100
60000/60000 [==============================] - 1s - loss: 0.2797 - acc: 0.9223     
Epoch 29/100
60000/60000 [==============================] - 1s - loss: 0.2788 - acc: 0.9220     
Epoch 30/100
60000/60000 [==============================] - 1s - loss: 0.2779 - acc: 0.9229     
Epoch 31/100
60000/60000 [==============================] - 1s - loss: 0.2772 - acc: 0.9230     
Epoch 32/100
60000/60000 [==============================] - 1s - loss: 0.2765 - acc: 0.9233     
Epoch 33/100
60000/60000 [==============================] - 1s - loss: 0.2757 - acc: 0.9232     
Epoch 34/100
60000/60000 [==============================] - 1s - loss: 0.2750 - acc: 0.9234     
Epoch 35/100
60000/60000 [==============================] - 1s - loss: 0.2742 - acc: 0.9240     
Epoch 36/100
60000/60000 [==============================] - 1s - loss: 0.2737 - acc: 0.9242     
Epoch 37/100
60000/60000 [==============================] - 1s - loss: 0.2731 - acc: 0.9241     
Epoch 38/100
60000/60000 [==============================] - 1s - loss: 0.2725 - acc: 0.9241     
Epoch 39/100
60000/60000 [==============================] - 1s - loss: 0.2719 - acc: 0.9243     
Epoch 40/100
60000/60000 [==============================] - 1s - loss: 0.2714 - acc: 0.9248     
Epoch 41/100
60000/60000 [==============================] - 1s - loss: 0.2707 - acc: 0.9252     
Epoch 42/100
60000/60000 [==============================] - 1s - loss: 0.2703 - acc: 0.9247     
Epoch 43/100
60000/60000 [==============================] - 1s - loss: 0.2699 - acc: 0.9250     
Epoch 44/100
60000/60000 [==============================] - 1s - loss: 0.2692 - acc: 0.9255     
Epoch 45/100
60000/60000 [==============================] - 1s - loss: 0.2689 - acc: 0.9252     
Epoch 46/100
60000/60000 [==============================] - 1s - loss: 0.2684 - acc: 0.9258     
Epoch 47/100
60000/60000 [==============================] - 1s - loss: 0.2679 - acc: 0.9261     
Epoch 48/100
60000/60000 [==============================] - 1s - loss: 0.2675 - acc: 0.9257     
Epoch 49/100
60000/60000 [==============================] - 1s - loss: 0.2670 - acc: 0.9261     
Epoch 50/100
60000/60000 [==============================] - 1s - loss: 0.2666 - acc: 0.9266     
Epoch 51/100
60000/60000 [==============================] - 1s - loss: 0.2663 - acc: 0.9266     
Epoch 52/100
60000/60000 [==============================] - 1s - loss: 0.2660 - acc: 0.9266     
Epoch 53/100
60000/60000 [==============================] - 1s - loss: 0.2657 - acc: 0.9268     
Epoch 54/100
60000/60000 [==============================] - 1s - loss: 0.2653 - acc: 0.9267     
Epoch 55/100
60000/60000 [==============================] - 1s - loss: 0.2648 - acc: 0.9268     
Epoch 56/100
60000/60000 [==============================] - 1s - loss: 0.2644 - acc: 0.9269     
Epoch 57/100
60000/60000 [==============================] - 1s - loss: 0.2641 - acc: 0.9268     
Epoch 58/100
60000/60000 [==============================] - 1s - loss: 0.2639 - acc: 0.9275     
Epoch 59/100
60000/60000 [==============================] - 1s - loss: 0.2635 - acc: 0.9270     
Epoch 60/100
60000/60000 [==============================] - 1s - loss: 0.2631 - acc: 0.9277     
Epoch 61/100
60000/60000 [==============================] - 1s - loss: 0.2629 - acc: 0.9273     
Epoch 62/100
60000/60000 [==============================] - 1s - loss: 0.2625 - acc: 0.9279     
Epoch 63/100
60000/60000 [==============================] - 1s - loss: 0.2623 - acc: 0.9276     
Epoch 64/100
60000/60000 [==============================] - 1s - loss: 0.2619 - acc: 0.9278     
Epoch 65/100
60000/60000 [==============================] - 1s - loss: 0.2616 - acc: 0.9281     
Epoch 66/100
60000/60000 [==============================] - 1s - loss: 0.2613 - acc: 0.9282     
Epoch 67/100
60000/60000 [==============================] - 1s - loss: 0.2610 - acc: 0.9281     
Epoch 68/100
60000/60000 [==============================] - 1s - loss: 0.2609 - acc: 0.9281     
Epoch 69/100
60000/60000 [==============================] - 1s - loss: 0.2606 - acc: 0.9284     
Epoch 70/100
60000/60000 [==============================] - 1s - loss: 0.2602 - acc: 0.9283     
Epoch 71/100
60000/60000 [==============================] - 1s - loss: 0.2601 - acc: 0.9283     
Epoch 72/100
60000/60000 [==============================] - 1s - loss: 0.2597 - acc: 0.9285     
Epoch 73/100
60000/60000 [==============================] - 1s - loss: 0.2595 - acc: 0.9283     
Epoch 74/100
60000/60000 [==============================] - 1s - loss: 0.2594 - acc: 0.9283     
Epoch 75/100
60000/60000 [==============================] - 1s - loss: 0.2590 - acc: 0.9287     
Epoch 76/100
60000/60000 [==============================] - 1s - loss: 0.2589 - acc: 0.9286     
Epoch 77/100
60000/60000 [==============================] - 1s - loss: 0.2586 - acc: 0.9291     
Epoch 78/100
60000/60000 [==============================] - 1s - loss: 0.2583 - acc: 0.9292     
Epoch 79/100
60000/60000 [==============================] - 1s - loss: 0.2581 - acc: 0.9291     
Epoch 80/100
60000/60000 [==============================] - 1s - loss: 0.2579 - acc: 0.9290     
Epoch 81/100
60000/60000 [==============================] - 1s - loss: 0.2576 - acc: 0.9291     
Epoch 82/100
60000/60000 [==============================] - 1s - loss: 0.2574 - acc: 0.9295     
Epoch 83/100
60000/60000 [==============================] - 1s - loss: 0.2573 - acc: 0.9292     
Epoch 84/100
60000/60000 [==============================] - 1s - loss: 0.2571 - acc: 0.9294     
Epoch 85/100
60000/60000 [==============================] - 1s - loss: 0.2567 - acc: 0.9293     
Epoch 86/100
60000/60000 [==============================] - 1s - loss: 0.2566 - acc: 0.9294     
Epoch 87/100
60000/60000 [==============================] - 1s - loss: 0.2565 - acc: 0.9295     
Epoch 88/100
60000/60000 [==============================] - 1s - loss: 0.2563 - acc: 0.9294     
Epoch 89/100
60000/60000 [==============================] - 1s - loss: 0.2562 - acc: 0.9296     
Epoch 90/100
60000/60000 [==============================] - 1s - loss: 0.2559 - acc: 0.9294     
Epoch 91/100
60000/60000 [==============================] - 1s - loss: 0.2557 - acc: 0.9297     
Epoch 92/100
60000/60000 [==============================] - 1s - loss: 0.2555 - acc: 0.9297     
Epoch 93/100
60000/60000 [==============================] - 1s - loss: 0.2554 - acc: 0.9296     
Epoch 94/100
60000/60000 [==============================] - 1s - loss: 0.2552 - acc: 0.9295     
Epoch 95/100
60000/60000 [==============================] - 1s - loss: 0.2550 - acc: 0.9300     
Epoch 96/100
60000/60000 [==============================] - 1s - loss: 0.2549 - acc: 0.9302     
Epoch 97/100
60000/60000 [==============================] - 1s - loss: 0.2547 - acc: 0.9299     
Epoch 98/100
60000/60000 [==============================] - 1s - loss: 0.2545 - acc: 0.9301     
Epoch 99/100
60000/60000 [==============================] - 1s - loss: 0.2543 - acc: 0.9302     
Epoch 100/100
60000/60000 [==============================] - 1s - loss: 0.2543 - acc: 0.9302     
# Evaluate
evaluation = model.evaluate(X_test, Y_Test, verbose=1)
print('Summary: Loss over the test dataset: %.2f, Accuracy: %.2f' % (evaluation[0], evaluation[1]))
10000/10000 [==============================] - 0s     
Summary: Loss over the test dataset: 0.27, Accuracy: 0.93

results matching ""

    No results matching ""