In [89]:
%matplotlib inline
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD, Adam
import numpy as np
import matplotlib.pyplot as plt

In [58]:
# ランダムシードは固定するべし
# 結果を再現・アルゴリズムを比較するため
np.random.seed(7)

In [59]:
!wget http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data


--2017-05-26 17:02:49--  http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data
Resolving archive.ics.uci.edu... 128.195.10.249
Connecting to archive.ics.uci.edu|128.195.10.249|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23279 (23K) [text/plain]
Saving to: ‘pima-indians-diabetes.data.2’

pima-indians-diabet 100%[===================>]  22.73K  --.-KB/s    in 0.1s    

2017-05-26 17:02:49 (174 KB/s) - ‘pima-indians-diabetes.data.2’ saved [23279/23279]


In [60]:
dataset = np.loadtxt('pima-indians-diabetes.data', delimiter=',')

In [61]:
dataset


Out[61]:
array([[   6.   ,  148.   ,   72.   , ...,    0.627,   50.   ,    1.   ],
       [   1.   ,   85.   ,   66.   , ...,    0.351,   31.   ,    0.   ],
       [   8.   ,  183.   ,   64.   , ...,    0.672,   32.   ,    1.   ],
       ..., 
       [   5.   ,  121.   ,   72.   , ...,    0.245,   30.   ,    0.   ],
       [   1.   ,  126.   ,   60.   , ...,    0.349,   47.   ,    1.   ],
       [   1.   ,   93.   ,   70.   , ...,    0.315,   23.   ,    0.   ]])

In [119]:
X = dataset[:, 0:8]
Y = dataset[:, 8]

In [133]:
from sklearn import preprocessing
X_scaled = preprocessing.scale(X)
print(X_scaled.shape)
print(X_scaled.mean(axis=0))
print(X_scaled.std(axis=0))


(768, 8)
[ -7.74843153e-17   3.61400724e-18  -1.32724416e-17   7.76288755e-17
  -5.49329101e-17   2.97273780e-15   1.92438658e-15   2.19297959e-16]
[ 1.  1.  1.  1.  1.  1.  1.  1.]

In [176]:
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_scaled, Y, epochs=150, batch_size=10)


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_88 (Dense)             (None, 12)                108       
_________________________________________________________________
dense_89 (Dense)             (None, 8)                 104       
_________________________________________________________________
dense_90 (Dense)             (None, 1)                 9         
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
Epoch 1/150
768/768 [==============================] - 1s - loss: 0.6448 - acc: 0.6719     
Epoch 2/150
768/768 [==============================] - 0s - loss: 0.5721 - acc: 0.7253     
Epoch 3/150
768/768 [==============================] - 0s - loss: 0.5276 - acc: 0.7500     
Epoch 4/150
768/768 [==============================] - 0s - loss: 0.5052 - acc: 0.7526     
Epoch 5/150
768/768 [==============================] - 0s - loss: 0.4934 - acc: 0.7461     
Epoch 6/150
768/768 [==============================] - 0s - loss: 0.4866 - acc: 0.7552     
Epoch 7/150
768/768 [==============================] - 0s - loss: 0.4821 - acc: 0.7630     
Epoch 8/150
768/768 [==============================] - 0s - loss: 0.4775 - acc: 0.7604     
Epoch 9/150
768/768 [==============================] - 0s - loss: 0.4754 - acc: 0.7643     
Epoch 10/150
768/768 [==============================] - 0s - loss: 0.4721 - acc: 0.7682     
Epoch 11/150
768/768 [==============================] - 0s - loss: 0.4700 - acc: 0.7695     
Epoch 12/150
768/768 [==============================] - 0s - loss: 0.4676 - acc: 0.7669     
Epoch 13/150
768/768 [==============================] - 0s - loss: 0.4667 - acc: 0.7682     
Epoch 14/150
768/768 [==============================] - 0s - loss: 0.4643 - acc: 0.7682     
Epoch 15/150
768/768 [==============================] - 0s - loss: 0.4631 - acc: 0.7682     
Epoch 16/150
768/768 [==============================] - 0s - loss: 0.4615 - acc: 0.7656     
Epoch 17/150
768/768 [==============================] - 0s - loss: 0.4607 - acc: 0.7747     
Epoch 18/150
768/768 [==============================] - 0s - loss: 0.4604 - acc: 0.7786     
Epoch 19/150
768/768 [==============================] - 0s - loss: 0.4576 - acc: 0.7773     
Epoch 20/150
768/768 [==============================] - 0s - loss: 0.4566 - acc: 0.7812     
Epoch 21/150
768/768 [==============================] - 0s - loss: 0.4553 - acc: 0.7826     
Epoch 22/150
768/768 [==============================] - 0s - loss: 0.4549 - acc: 0.7773     
Epoch 23/150
768/768 [==============================] - 0s - loss: 0.4531 - acc: 0.7839     
Epoch 24/150
768/768 [==============================] - 0s - loss: 0.4528 - acc: 0.7826     
Epoch 25/150
768/768 [==============================] - 0s - loss: 0.4515 - acc: 0.7839     
Epoch 26/150
768/768 [==============================] - 0s - loss: 0.4515 - acc: 0.7839     
Epoch 27/150
768/768 [==============================] - 0s - loss: 0.4502 - acc: 0.7852     
Epoch 28/150
768/768 [==============================] - 0s - loss: 0.4489 - acc: 0.7891     
Epoch 29/150
768/768 [==============================] - 0s - loss: 0.4484 - acc: 0.7878     
Epoch 30/150
768/768 [==============================] - 0s - loss: 0.4474 - acc: 0.7852     
Epoch 31/150
768/768 [==============================] - 0s - loss: 0.4466 - acc: 0.7891     
Epoch 32/150
768/768 [==============================] - 0s - loss: 0.4460 - acc: 0.7865     
Epoch 33/150
768/768 [==============================] - 0s - loss: 0.4450 - acc: 0.7865     
Epoch 34/150
768/768 [==============================] - 0s - loss: 0.4437 - acc: 0.7852     
Epoch 35/150
768/768 [==============================] - 0s - loss: 0.4428 - acc: 0.7891     
Epoch 36/150
768/768 [==============================] - 0s - loss: 0.4421 - acc: 0.7878     
Epoch 37/150
768/768 [==============================] - 0s - loss: 0.4415 - acc: 0.7826     
Epoch 38/150
768/768 [==============================] - 0s - loss: 0.4411 - acc: 0.7865     
Epoch 39/150
768/768 [==============================] - 0s - loss: 0.4403 - acc: 0.7891     
Epoch 40/150
768/768 [==============================] - 0s - loss: 0.4399 - acc: 0.7878     
Epoch 41/150
768/768 [==============================] - 0s - loss: 0.4392 - acc: 0.7930     
Epoch 42/150
768/768 [==============================] - 0s - loss: 0.4386 - acc: 0.7878     
Epoch 43/150
768/768 [==============================] - 0s - loss: 0.4381 - acc: 0.7930     
Epoch 44/150
768/768 [==============================] - 0s - loss: 0.4374 - acc: 0.7930     
Epoch 45/150
768/768 [==============================] - 0s - loss: 0.4375 - acc: 0.7904     
Epoch 46/150
768/768 [==============================] - 0s - loss: 0.4364 - acc: 0.7904     
Epoch 47/150
768/768 [==============================] - 0s - loss: 0.4352 - acc: 0.7969     
Epoch 48/150
768/768 [==============================] - 0s - loss: 0.4343 - acc: 0.7943     
Epoch 49/150
768/768 [==============================] - 0s - loss: 0.4345 - acc: 0.7969     
Epoch 50/150
768/768 [==============================] - 0s - loss: 0.4335 - acc: 0.7943     
Epoch 51/150
768/768 [==============================] - 0s - loss: 0.4331 - acc: 0.7982     
Epoch 52/150
768/768 [==============================] - 0s - loss: 0.4330 - acc: 0.7969     
Epoch 53/150
768/768 [==============================] - 0s - loss: 0.4326 - acc: 0.7891     
Epoch 54/150
768/768 [==============================] - 0s - loss: 0.4318 - acc: 0.7904     
Epoch 55/150
768/768 [==============================] - 0s - loss: 0.4316 - acc: 0.7995     
Epoch 56/150
768/768 [==============================] - 0s - loss: 0.4313 - acc: 0.7943     
Epoch 57/150
768/768 [==============================] - 0s - loss: 0.4312 - acc: 0.7917     
Epoch 58/150
768/768 [==============================] - 0s - loss: 0.4304 - acc: 0.7995     
Epoch 59/150
768/768 [==============================] - 0s - loss: 0.4296 - acc: 0.7995     
Epoch 60/150
768/768 [==============================] - 0s - loss: 0.4295 - acc: 0.8021     
Epoch 61/150
768/768 [==============================] - 0s - loss: 0.4289 - acc: 0.7995     
Epoch 62/150
768/768 [==============================] - 0s - loss: 0.4290 - acc: 0.7943     
Epoch 63/150
768/768 [==============================] - 0s - loss: 0.4296 - acc: 0.8008     
Epoch 64/150
768/768 [==============================] - 0s - loss: 0.4279 - acc: 0.7917     
Epoch 65/150
768/768 [==============================] - 0s - loss: 0.4279 - acc: 0.7982     
Epoch 66/150
768/768 [==============================] - 0s - loss: 0.4276 - acc: 0.7982     
Epoch 67/150
768/768 [==============================] - 0s - loss: 0.4270 - acc: 0.7969     
Epoch 68/150
768/768 [==============================] - 0s - loss: 0.4269 - acc: 0.7982     
Epoch 69/150
768/768 [==============================] - 0s - loss: 0.4260 - acc: 0.7982     
Epoch 70/150
768/768 [==============================] - 0s - loss: 0.4257 - acc: 0.7956     
Epoch 71/150
768/768 [==============================] - 0s - loss: 0.4254 - acc: 0.8021     
Epoch 72/150
768/768 [==============================] - 0s - loss: 0.4264 - acc: 0.7982     
Epoch 73/150
768/768 [==============================] - 0s - loss: 0.4259 - acc: 0.7969     
Epoch 74/150
768/768 [==============================] - 0s - loss: 0.4247 - acc: 0.8060     
Epoch 75/150
768/768 [==============================] - 0s - loss: 0.4258 - acc: 0.7995     
Epoch 76/150
768/768 [==============================] - 0s - loss: 0.4246 - acc: 0.7995     
Epoch 77/150
768/768 [==============================] - 0s - loss: 0.4248 - acc: 0.7956     
Epoch 78/150
768/768 [==============================] - 0s - loss: 0.4240 - acc: 0.8047     
Epoch 79/150
768/768 [==============================] - 0s - loss: 0.4235 - acc: 0.7969     
Epoch 80/150
768/768 [==============================] - 0s - loss: 0.4232 - acc: 0.7995     
Epoch 81/150
768/768 [==============================] - 0s - loss: 0.4234 - acc: 0.7982     
Epoch 82/150
768/768 [==============================] - 0s - loss: 0.4238 - acc: 0.8021     
Epoch 83/150
768/768 [==============================] - 0s - loss: 0.4227 - acc: 0.7969     
Epoch 84/150
768/768 [==============================] - 0s - loss: 0.4222 - acc: 0.8008     
Epoch 85/150
768/768 [==============================] - 0s - loss: 0.4218 - acc: 0.7995     
Epoch 86/150
768/768 [==============================] - 0s - loss: 0.4220 - acc: 0.7956     
Epoch 87/150
768/768 [==============================] - 0s - loss: 0.4215 - acc: 0.7969     
Epoch 88/150
768/768 [==============================] - 0s - loss: 0.4214 - acc: 0.7982     
Epoch 89/150
768/768 [==============================] - 0s - loss: 0.4212 - acc: 0.8008     
Epoch 90/150
768/768 [==============================] - 0s - loss: 0.4213 - acc: 0.7995     
Epoch 91/150
768/768 [==============================] - 0s - loss: 0.4203 - acc: 0.7969     
Epoch 92/150
768/768 [==============================] - 0s - loss: 0.4197 - acc: 0.7943     
Epoch 93/150
768/768 [==============================] - 0s - loss: 0.4210 - acc: 0.8021     
Epoch 94/150
768/768 [==============================] - 0s - loss: 0.4200 - acc: 0.7969     
Epoch 95/150
768/768 [==============================] - 0s - loss: 0.4192 - acc: 0.8008     
Epoch 96/150
768/768 [==============================] - 0s - loss: 0.4196 - acc: 0.7982     
Epoch 97/150
768/768 [==============================] - 0s - loss: 0.4189 - acc: 0.7969     
Epoch 98/150
768/768 [==============================] - 0s - loss: 0.4182 - acc: 0.7995     
Epoch 99/150
768/768 [==============================] - 0s - loss: 0.4176 - acc: 0.7982     
Epoch 100/150
768/768 [==============================] - 0s - loss: 0.4173 - acc: 0.8021     
Epoch 101/150
768/768 [==============================] - 0s - loss: 0.4171 - acc: 0.7969     
Epoch 102/150
768/768 [==============================] - 0s - loss: 0.4171 - acc: 0.8021     
Epoch 103/150
768/768 [==============================] - 0s - loss: 0.4170 - acc: 0.7995     
Epoch 104/150
768/768 [==============================] - 0s - loss: 0.4155 - acc: 0.8060     
Epoch 105/150
768/768 [==============================] - 0s - loss: 0.4161 - acc: 0.8008     
Epoch 106/150
768/768 [==============================] - 0s - loss: 0.4160 - acc: 0.8047     
Epoch 107/150
768/768 [==============================] - 0s - loss: 0.4150 - acc: 0.8021     
Epoch 108/150
768/768 [==============================] - 0s - loss: 0.4152 - acc: 0.8034     
Epoch 109/150
768/768 [==============================] - 0s - loss: 0.4147 - acc: 0.8021     
Epoch 110/150
768/768 [==============================] - 0s - loss: 0.4140 - acc: 0.8021     
Epoch 111/150
768/768 [==============================] - 0s - loss: 0.4137 - acc: 0.8034     
Epoch 112/150
768/768 [==============================] - 0s - loss: 0.4143 - acc: 0.8008     
Epoch 113/150
768/768 [==============================] - 0s - loss: 0.4130 - acc: 0.8008     
Epoch 114/150
768/768 [==============================] - 0s - loss: 0.4125 - acc: 0.7995     
Epoch 115/150
768/768 [==============================] - 0s - loss: 0.4129 - acc: 0.8021     
Epoch 116/150
768/768 [==============================] - 0s - loss: 0.4117 - acc: 0.8008     
Epoch 117/150
768/768 [==============================] - 0s - loss: 0.4120 - acc: 0.7956     
Epoch 118/150
768/768 [==============================] - 0s - loss: 0.4112 - acc: 0.7969     
Epoch 119/150
768/768 [==============================] - 0s - loss: 0.4108 - acc: 0.8034     
Epoch 120/150
768/768 [==============================] - 0s - loss: 0.4103 - acc: 0.7995     
Epoch 121/150
768/768 [==============================] - 0s - loss: 0.4110 - acc: 0.8021     
Epoch 122/150
768/768 [==============================] - 0s - loss: 0.4107 - acc: 0.7943     
Epoch 123/150
768/768 [==============================] - 0s - loss: 0.4095 - acc: 0.8060     
Epoch 124/150
768/768 [==============================] - 0s - loss: 0.4086 - acc: 0.7982     
Epoch 125/150
768/768 [==============================] - 0s - loss: 0.4102 - acc: 0.8073     
Epoch 126/150
768/768 [==============================] - 0s - loss: 0.4079 - acc: 0.8034     
Epoch 127/150
768/768 [==============================] - 0s - loss: 0.4082 - acc: 0.8047     
Epoch 128/150
768/768 [==============================] - 0s - loss: 0.4078 - acc: 0.7995     
Epoch 129/150
768/768 [==============================] - 0s - loss: 0.4080 - acc: 0.8034     
Epoch 130/150
768/768 [==============================] - 0s - loss: 0.4065 - acc: 0.8034     
Epoch 131/150
768/768 [==============================] - 0s - loss: 0.4062 - acc: 0.8021     
Epoch 132/150
768/768 [==============================] - 0s - loss: 0.4064 - acc: 0.8073     
Epoch 133/150
768/768 [==============================] - 0s - loss: 0.4059 - acc: 0.8021     
Epoch 134/150
768/768 [==============================] - 0s - loss: 0.4061 - acc: 0.8008     
Epoch 135/150
768/768 [==============================] - 0s - loss: 0.4048 - acc: 0.8047     
Epoch 136/150
768/768 [==============================] - 0s - loss: 0.4051 - acc: 0.8034     
Epoch 137/150
768/768 [==============================] - 0s - loss: 0.4043 - acc: 0.7995     
Epoch 138/150
768/768 [==============================] - 0s - loss: 0.4034 - acc: 0.8086     
Epoch 139/150
768/768 [==============================] - 0s - loss: 0.4038 - acc: 0.8060     
Epoch 140/150
768/768 [==============================] - 0s - loss: 0.4040 - acc: 0.8034     
Epoch 141/150
768/768 [==============================] - 0s - loss: 0.4040 - acc: 0.8073     
Epoch 142/150
768/768 [==============================] - 0s - loss: 0.4029 - acc: 0.8086     
Epoch 143/150
768/768 [==============================] - 0s - loss: 0.4028 - acc: 0.8099     
Epoch 144/150
768/768 [==============================] - 0s - loss: 0.4042 - acc: 0.8099     
Epoch 145/150
768/768 [==============================] - 0s - loss: 0.4018 - acc: 0.8099     
Epoch 146/150
768/768 [==============================] - 0s - loss: 0.4018 - acc: 0.8112     
Epoch 147/150
768/768 [==============================] - 0s - loss: 0.4018 - acc: 0.8060     
Epoch 148/150
768/768 [==============================] - 0s - loss: 0.4020 - acc: 0.8125     
Epoch 149/150
768/768 [==============================] - 0s - loss: 0.4009 - acc: 0.8086     
Epoch 150/150
768/768 [==============================] - 0s - loss: 0.4002 - acc: 0.8151     

In [138]:
scores = model.evaluate(X_scaled, Y)


 32/768 [>.............................] - ETA: 0s

In [139]:
print('%s: %.2f%%' % (model.metrics_names[1], scores[1] * 100))


acc: 83.72%

In [143]:
predictions = model.predict(X_scaled)

In [144]:
rounded = [round(x[0]) for x in predictions]

In [148]:
print(rounded[:10])
print(Y[:10])


[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0]
[ 1.  0.  1.  0.  1.  0.  1.  0.  1.  1.]

Functional API


In [157]:
from keras.layers import Input, Dense
from keras.models import Model

inputs = Input(shape=(8, ))
x = Dense(12, activation='relu')(inputs)
x = Dense(8, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)

model = Model(inputs=inputs, outputs=predictions)
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_9 (InputLayer)         (None, 8)                 0         
_________________________________________________________________
dense_85 (Dense)             (None, 12)                108       
_________________________________________________________________
dense_86 (Dense)             (None, 8)                 104       
_________________________________________________________________
dense_87 (Dense)             (None, 1)                 9         
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________

In [159]:
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_scaled, Y, epochs=150, batch_size=10)


Epoch 1/150
768/768 [==============================] - 1s - loss: 0.4722 - acc: 0.7799     
Epoch 2/150
768/768 [==============================] - 0s - loss: 0.4649 - acc: 0.7826     
Epoch 3/150
768/768 [==============================] - 0s - loss: 0.4604 - acc: 0.7852     
Epoch 4/150
768/768 [==============================] - 0s - loss: 0.4575 - acc: 0.7839     
Epoch 5/150
768/768 [==============================] - 0s - loss: 0.4551 - acc: 0.7865     
Epoch 6/150
768/768 [==============================] - 0s - loss: 0.4518 - acc: 0.7852     
Epoch 7/150
768/768 [==============================] - 0s - loss: 0.4497 - acc: 0.7839     
Epoch 8/150
768/768 [==============================] - 0s - loss: 0.4485 - acc: 0.7799     
Epoch 9/150
768/768 [==============================] - 0s - loss: 0.4467 - acc: 0.7812     
Epoch 10/150
768/768 [==============================] - 0s - loss: 0.4451 - acc: 0.7786     
Epoch 11/150
768/768 [==============================] - 0s - loss: 0.4432 - acc: 0.7839     
Epoch 12/150
768/768 [==============================] - 0s - loss: 0.4423 - acc: 0.7852     
Epoch 13/150
768/768 [==============================] - 0s - loss: 0.4405 - acc: 0.7865     
Epoch 14/150
768/768 [==============================] - 0s - loss: 0.4393 - acc: 0.7852     
Epoch 15/150
768/768 [==============================] - 0s - loss: 0.4386 - acc: 0.7891     
Epoch 16/150
768/768 [==============================] - 0s - loss: 0.4375 - acc: 0.7930     
Epoch 17/150
768/768 [==============================] - 0s - loss: 0.4372 - acc: 0.7865     
Epoch 18/150
768/768 [==============================] - 0s - loss: 0.4351 - acc: 0.7930     
Epoch 19/150
768/768 [==============================] - 0s - loss: 0.4341 - acc: 0.7917     
Epoch 20/150
768/768 [==============================] - 0s - loss: 0.4333 - acc: 0.7917     
Epoch 21/150
768/768 [==============================] - 0s - loss: 0.4319 - acc: 0.7930     
Epoch 22/150
768/768 [==============================] - 0s - loss: 0.4311 - acc: 0.7956     
Epoch 23/150
768/768 [==============================] - 0s - loss: 0.4306 - acc: 0.7917     
Epoch 24/150
768/768 [==============================] - 0s - loss: 0.4304 - acc: 0.7943     
Epoch 25/150
768/768 [==============================] - 0s - loss: 0.4286 - acc: 0.7956     
Epoch 26/150
768/768 [==============================] - 0s - loss: 0.4283 - acc: 0.7995     
Epoch 27/150
768/768 [==============================] - 0s - loss: 0.4273 - acc: 0.7943     
Epoch 28/150
768/768 [==============================] - 0s - loss: 0.4267 - acc: 0.7969     
Epoch 29/150
768/768 [==============================] - 0s - loss: 0.4256 - acc: 0.7969     
Epoch 30/150
768/768 [==============================] - 0s - loss: 0.4249 - acc: 0.7956     
Epoch 31/150
768/768 [==============================] - 0s - loss: 0.4239 - acc: 0.7956     
Epoch 32/150
768/768 [==============================] - 0s - loss: 0.4239 - acc: 0.8021     
Epoch 33/150
768/768 [==============================] - 0s - loss: 0.4218 - acc: 0.8008     
Epoch 34/150
768/768 [==============================] - 0s - loss: 0.4211 - acc: 0.7969     
Epoch 35/150
768/768 [==============================] - 0s - loss: 0.4203 - acc: 0.7969     
Epoch 36/150
768/768 [==============================] - 0s - loss: 0.4194 - acc: 0.7982     
Epoch 37/150
768/768 [==============================] - 0s - loss: 0.4188 - acc: 0.7995     
Epoch 38/150
768/768 [==============================] - 0s - loss: 0.4186 - acc: 0.8047     
Epoch 39/150
768/768 [==============================] - 0s - loss: 0.4188 - acc: 0.8047     
Epoch 40/150
768/768 [==============================] - 0s - loss: 0.4173 - acc: 0.8021     
Epoch 41/150
768/768 [==============================] - 0s - loss: 0.4165 - acc: 0.8060     
Epoch 42/150
768/768 [==============================] - 0s - loss: 0.4156 - acc: 0.8060     
Epoch 43/150
768/768 [==============================] - 0s - loss: 0.4151 - acc: 0.8047     
Epoch 44/150
768/768 [==============================] - 0s - loss: 0.4148 - acc: 0.8034     
Epoch 45/150
768/768 [==============================] - 0s - loss: 0.4145 - acc: 0.8060     
Epoch 46/150
768/768 [==============================] - 0s - loss: 0.4136 - acc: 0.8086     
Epoch 47/150
768/768 [==============================] - 0s - loss: 0.4131 - acc: 0.8125     
Epoch 48/150
768/768 [==============================] - 0s - loss: 0.4124 - acc: 0.8099     
Epoch 49/150
768/768 [==============================] - 0s - loss: 0.4123 - acc: 0.8099     
Epoch 50/150
768/768 [==============================] - 0s - loss: 0.4112 - acc: 0.8060     
Epoch 51/150
768/768 [==============================] - 0s - loss: 0.4108 - acc: 0.8125     
Epoch 52/150
768/768 [==============================] - 0s - loss: 0.4112 - acc: 0.8138     
Epoch 53/150
768/768 [==============================] - 0s - loss: 0.4101 - acc: 0.8151     
Epoch 54/150
768/768 [==============================] - 0s - loss: 0.4105 - acc: 0.8086     
Epoch 55/150
768/768 [==============================] - 0s - loss: 0.4086 - acc: 0.8138     
Epoch 56/150
768/768 [==============================] - 0s - loss: 0.4099 - acc: 0.8112     
Epoch 57/150
768/768 [==============================] - 0s - loss: 0.4080 - acc: 0.8125     
Epoch 58/150
768/768 [==============================] - 0s - loss: 0.4073 - acc: 0.8177     
Epoch 59/150
768/768 [==============================] - 0s - loss: 0.4069 - acc: 0.8164     
Epoch 60/150
768/768 [==============================] - 0s - loss: 0.4066 - acc: 0.8099     
Epoch 61/150
768/768 [==============================] - 0s - loss: 0.4072 - acc: 0.8099     
Epoch 62/150
768/768 [==============================] - 0s - loss: 0.4064 - acc: 0.8125     
Epoch 63/150
768/768 [==============================] - 0s - loss: 0.4054 - acc: 0.8125     
Epoch 64/150
768/768 [==============================] - 0s - loss: 0.4055 - acc: 0.8164     
Epoch 65/150
768/768 [==============================] - 0s - loss: 0.4048 - acc: 0.8125     
Epoch 66/150
768/768 [==============================] - 0s - loss: 0.4039 - acc: 0.8151     
Epoch 67/150
768/768 [==============================] - 0s - loss: 0.4044 - acc: 0.8151     
Epoch 68/150
768/768 [==============================] - 0s - loss: 0.4039 - acc: 0.8164     
Epoch 69/150
768/768 [==============================] - 0s - loss: 0.4039 - acc: 0.8203     
Epoch 70/150
768/768 [==============================] - 0s - loss: 0.4023 - acc: 0.8190     
Epoch 71/150
768/768 [==============================] - 0s - loss: 0.4022 - acc: 0.8164     
Epoch 72/150
768/768 [==============================] - 0s - loss: 0.4018 - acc: 0.8151     
Epoch 73/150
768/768 [==============================] - 0s - loss: 0.4021 - acc: 0.8151     
Epoch 74/150
768/768 [==============================] - 0s - loss: 0.4016 - acc: 0.8177     
Epoch 75/150
768/768 [==============================] - 0s - loss: 0.4017 - acc: 0.8151     
Epoch 76/150
768/768 [==============================] - 0s - loss: 0.4005 - acc: 0.8112     
Epoch 77/150
768/768 [==============================] - 0s - loss: 0.3997 - acc: 0.8203     
Epoch 78/150
768/768 [==============================] - 0s - loss: 0.3994 - acc: 0.8177     
Epoch 79/150
768/768 [==============================] - 0s - loss: 0.4003 - acc: 0.8190     
Epoch 80/150
768/768 [==============================] - 0s - loss: 0.3986 - acc: 0.8177     
Epoch 81/150
768/768 [==============================] - 0s - loss: 0.3987 - acc: 0.8190     
Epoch 82/150
768/768 [==============================] - 0s - loss: 0.3982 - acc: 0.8164     
Epoch 83/150
768/768 [==============================] - 0s - loss: 0.3998 - acc: 0.8164     
Epoch 84/150
768/768 [==============================] - 0s - loss: 0.3980 - acc: 0.8177     
Epoch 85/150
768/768 [==============================] - 0s - loss: 0.3978 - acc: 0.8164     
Epoch 86/150
768/768 [==============================] - 0s - loss: 0.3962 - acc: 0.8190     
Epoch 87/150
768/768 [==============================] - 0s - loss: 0.3964 - acc: 0.8203     
Epoch 88/150
768/768 [==============================] - 0s - loss: 0.3960 - acc: 0.8099     
Epoch 89/150
768/768 [==============================] - 0s - loss: 0.3962 - acc: 0.8216     
Epoch 90/150
768/768 [==============================] - 0s - loss: 0.3947 - acc: 0.8151     
Epoch 91/150
768/768 [==============================] - 0s - loss: 0.3950 - acc: 0.8177     
Epoch 92/150
768/768 [==============================] - 0s - loss: 0.3962 - acc: 0.8177     
Epoch 93/150
768/768 [==============================] - 0s - loss: 0.3939 - acc: 0.8216     
Epoch 94/150
768/768 [==============================] - 0s - loss: 0.3932 - acc: 0.8190     
Epoch 95/150
768/768 [==============================] - 0s - loss: 0.3922 - acc: 0.8190     
Epoch 96/150
768/768 [==============================] - 0s - loss: 0.3920 - acc: 0.8177     
Epoch 97/150
768/768 [==============================] - 0s - loss: 0.3922 - acc: 0.8164     
Epoch 98/150
768/768 [==============================] - 0s - loss: 0.3921 - acc: 0.8177     
Epoch 99/150
768/768 [==============================] - 0s - loss: 0.3907 - acc: 0.8190     
Epoch 100/150
768/768 [==============================] - 0s - loss: 0.3916 - acc: 0.8190     
Epoch 101/150
768/768 [==============================] - 0s - loss: 0.3900 - acc: 0.8203     
Epoch 102/150
768/768 [==============================] - 0s - loss: 0.3896 - acc: 0.8177     
Epoch 103/150
768/768 [==============================] - 0s - loss: 0.3899 - acc: 0.8177     
Epoch 104/150
768/768 [==============================] - 0s - loss: 0.3883 - acc: 0.8229     
Epoch 105/150
768/768 [==============================] - 0s - loss: 0.3876 - acc: 0.8216     
Epoch 106/150
768/768 [==============================] - 0s - loss: 0.3877 - acc: 0.8203     
Epoch 107/150
768/768 [==============================] - 0s - loss: 0.3872 - acc: 0.8229     
Epoch 108/150
768/768 [==============================] - 0s - loss: 0.3872 - acc: 0.8190     
Epoch 109/150
768/768 [==============================] - 0s - loss: 0.3862 - acc: 0.8190     
Epoch 110/150
768/768 [==============================] - 0s - loss: 0.3872 - acc: 0.8177     
Epoch 111/150
768/768 [==============================] - 0s - loss: 0.3866 - acc: 0.8216     
Epoch 112/150
768/768 [==============================] - 0s - loss: 0.3845 - acc: 0.8190     
Epoch 113/150
768/768 [==============================] - 0s - loss: 0.3831 - acc: 0.8216     
Epoch 114/150
768/768 [==============================] - 0s - loss: 0.3848 - acc: 0.8216     
Epoch 115/150
768/768 [==============================] - 0s - loss: 0.3839 - acc: 0.8255     
Epoch 116/150
768/768 [==============================] - 0s - loss: 0.3838 - acc: 0.8216     
Epoch 117/150
768/768 [==============================] - 0s - loss: 0.3834 - acc: 0.8203     
Epoch 118/150
768/768 [==============================] - 0s - loss: 0.3814 - acc: 0.8216     
Epoch 119/150
768/768 [==============================] - 0s - loss: 0.3825 - acc: 0.8216     
Epoch 120/150
768/768 [==============================] - 0s - loss: 0.3825 - acc: 0.8216     
Epoch 121/150
768/768 [==============================] - 0s - loss: 0.3818 - acc: 0.8216     
Epoch 122/150
768/768 [==============================] - 0s - loss: 0.3814 - acc: 0.8203     
Epoch 123/150
768/768 [==============================] - 0s - loss: 0.3804 - acc: 0.8216     
Epoch 124/150
768/768 [==============================] - 0s - loss: 0.3798 - acc: 0.8203     
Epoch 125/150
768/768 [==============================] - 0s - loss: 0.3799 - acc: 0.8164     
Epoch 126/150
768/768 [==============================] - 0s - loss: 0.3799 - acc: 0.8190     
Epoch 127/150
768/768 [==============================] - 0s - loss: 0.3798 - acc: 0.8242     
Epoch 128/150
768/768 [==============================] - 0s - loss: 0.3789 - acc: 0.8268     
Epoch 129/150
768/768 [==============================] - 0s - loss: 0.3789 - acc: 0.8268     
Epoch 130/150
768/768 [==============================] - 0s - loss: 0.3778 - acc: 0.8255     
Epoch 131/150
768/768 [==============================] - 0s - loss: 0.3770 - acc: 0.8216     
Epoch 132/150
768/768 [==============================] - 0s - loss: 0.3778 - acc: 0.8242     
Epoch 133/150
768/768 [==============================] - 0s - loss: 0.3767 - acc: 0.8268     
Epoch 134/150
768/768 [==============================] - 0s - loss: 0.3781 - acc: 0.8294     
Epoch 135/150
768/768 [==============================] - 0s - loss: 0.3770 - acc: 0.8255     
Epoch 136/150
768/768 [==============================] - 0s - loss: 0.3758 - acc: 0.8242     
Epoch 137/150
768/768 [==============================] - 0s - loss: 0.3755 - acc: 0.8268     
Epoch 138/150
768/768 [==============================] - 0s - loss: 0.3748 - acc: 0.8281     
Epoch 139/150
768/768 [==============================] - 0s - loss: 0.3742 - acc: 0.8255     
Epoch 140/150
768/768 [==============================] - 0s - loss: 0.3740 - acc: 0.8320     
Epoch 141/150
768/768 [==============================] - 0s - loss: 0.3738 - acc: 0.8229     
Epoch 142/150
768/768 [==============================] - 0s - loss: 0.3746 - acc: 0.8294     
Epoch 143/150
768/768 [==============================] - 0s - loss: 0.3725 - acc: 0.8320     
Epoch 144/150
768/768 [==============================] - 0s - loss: 0.3724 - acc: 0.8307     
Epoch 145/150
768/768 [==============================] - 0s - loss: 0.3717 - acc: 0.8359     
Epoch 146/150
768/768 [==============================] - 0s - loss: 0.3728 - acc: 0.8320     
Epoch 147/150
768/768 [==============================] - 0s - loss: 0.3716 - acc: 0.8333     
Epoch 148/150
768/768 [==============================] - 0s - loss: 0.3719 - acc: 0.8268     
Epoch 149/150
768/768 [==============================] - 0s - loss: 0.3715 - acc: 0.8320     
Epoch 150/150
768/768 [==============================] - 0s - loss: 0.3712 - acc: 0.8294     

Save and load a model


In [165]:
model_json = model.to_json()
with open('model.json', 'w') as json_file:
    json_file.write(model_json)
model.save_weights('model.h5')

In [167]:
del model

In [170]:
from keras.models import model_from_json
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights('model.h5')

In [172]:
loaded_model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_9 (InputLayer)         (None, 8)                 0         
_________________________________________________________________
dense_85 (Dense)             (None, 12)                108       
_________________________________________________________________
dense_86 (Dense)             (None, 8)                 104       
_________________________________________________________________
dense_87 (Dense)             (None, 1)                 9         
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________

In [175]:
del loaded_model

In [177]:
model.save('mymodel.h5')

In [178]:
del model

In [180]:
from keras.models import load_model
model = load_model('mymodel.h5')

In [181]:
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_88 (Dense)             (None, 12)                108       
_________________________________________________________________
dense_89 (Dense)             (None, 8)                 104       
_________________________________________________________________
dense_90 (Dense)             (None, 1)                 9         
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________

In [183]:
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])

In [184]:
model.optimizer


Out[184]:
<keras.optimizers.SGD at 0x137fc0198>