In [1]:
import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Activation


Using Theano backend.
Using gpu device 0: GeForce GT625M (CNMeM is disabled, cuDNN not available)

sequential


In [2]:
%%time

model = Sequential()
model.add(Dense(1,input_dim=784, activation='tanh'))
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])

data = np.random.random((1000,784))
labels = np.random.randint(2,size=(1000,1))

model.fit(data,labels,nb_epoch=10,batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 0s - loss: 2.9005 - acc: 0.5010     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 4.4020 - acc: 0.3610     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 7.2061 - acc: 0.5130     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 7.7480 - acc: 0.5140     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 7.7480 - acc: 0.5140     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 7.7480 - acc: 0.5140     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 7.7480 - acc: 0.5140     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 7.7480 - acc: 0.5140     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 7.7480 - acc: 0.5140     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 7.7480 - acc: 0.5140     
CPU times: user 1.7 s, sys: 416 ms, total: 2.12 s
Wall time: 1min 14s

In [3]:
%%time

model = Sequential()
model.add(Dense(1,input_dim=784, activation='linear'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

data = np.random.random((1000,784))
labels = np.random.randint(2,size=(1000,1))

model.fit(data,labels,nb_epoch=10,batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 0s - loss: 4.2552 - acc: 0.4020     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 5.8849 - acc: 0.3460     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 3.8482 - acc: 0.4740     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 2.6821 - acc: 0.5090     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 2.6482 - acc: 0.4820     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 2.8464 - acc: 0.4740     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 3.3461 - acc: 0.4530     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 3.3146 - acc: 0.4630     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 3.6025 - acc: 0.4620     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 2.3228 - acc: 0.5060     
CPU times: user 1.46 s, sys: 212 ms, total: 1.68 s
Wall time: 17.8 s

In [4]:
%%time

model = Sequential()
model.add(Dense(1,input_dim=784, activation='relu'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

data = np.random.random((1000,784))
labels = np.random.randint(2,size=(1000,1))

model.fit(data,labels,nb_epoch=50,batch_size=32)


Epoch 1/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 2/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 3/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 4/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 5/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 6/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 7/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 8/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 9/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 10/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 11/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 12/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 13/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 14/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 15/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 16/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 17/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 18/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 19/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 20/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 21/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 22/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 23/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 24/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 25/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 26/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 27/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 28/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 29/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 30/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 31/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 32/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 33/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 34/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 35/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 36/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 37/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 38/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 39/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 40/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 41/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 42/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 43/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 44/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 45/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 46/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 47/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 48/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 49/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
Epoch 50/50
1000/1000 [==============================] - 0s - loss: 7.8817 - acc: 0.5110     
CPU times: user 3.9 s, sys: 612 ms, total: 4.52 s
Wall time: 14.7 s

In [5]:
%%time

model = Sequential()
model.add(Dense(1,input_dim=784, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

data = np.random.random((1000,784))
labels = np.random.randint(2,size=(1000,1))

model.fit(data,labels,nb_epoch=10,batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 0s - loss: 0.7277 - acc: 0.5130     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 0.7125 - acc: 0.5300     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 0.7040 - acc: 0.5030     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 0.7012 - acc: 0.5450     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 0.6945 - acc: 0.5410     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 0.6837 - acc: 0.5570     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 0.6777 - acc: 0.5680     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 0.6756 - acc: 0.5870     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 0.6645 - acc: 0.6030     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 0.6560 - acc: 0.6110     
CPU times: user 1.5 s, sys: 188 ms, total: 1.68 s
Wall time: 11.3 s

In [6]:
%%time

model = Sequential()
model.add(Dense(1,input_dim=784, activation='hard_sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

data = np.random.random((1000,784))
labels = np.random.randint(2,size=(1000,1))

model.fit(data,labels,nb_epoch=10,batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 0s - loss: 0.7120 - acc: 0.5130     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 0.6932 - acc: 0.5540     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 0.6892 - acc: 0.5550     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 0.6840 - acc: 0.5790     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 0.6763 - acc: 0.5750     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 0.6795 - acc: 0.5670     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 0.6643 - acc: 0.5960     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 0.6600 - acc: 0.5970     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 0.6530 - acc: 0.6080     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 0.6490 - acc: 0.6230     
CPU times: user 1.54 s, sys: 220 ms, total: 1.76 s
Wall time: 13.7 s

In [7]:
%%time

from keras.regularizers import l1,l2,l1l2, activity_l2

model = Sequential()
model.add(Dense(1,input_dim=784, activation='sigmoid', W_regularizer=l2()))
# model.add(Dense(1,input_dim=784, activation='sigmoid'))

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

data = np.random.random((1000,784))
labels = np.random.randint(2,size=(1000,1))

model.fit(data,labels,nb_epoch=10,batch_size=2)


Epoch 1/10
1000/1000 [==============================] - 0s - loss: 0.8269 - acc: 0.4830     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 0.7506 - acc: 0.5540     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 0.6852 - acc: 0.6180     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 0.6625 - acc: 0.6510     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 0.6564 - acc: 0.6460     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 0.6392 - acc: 0.6700     
Epoch 7/10
1000/1000 [==============================] - 1s - loss: 0.6369 - acc: 0.6810     
Epoch 8/10
1000/1000 [==============================] - 1s - loss: 0.6046 - acc: 0.7180     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 0.6090 - acc: 0.7220     
Epoch 10/10
1000/1000 [==============================] - 1s - loss: 0.6131 - acc: 0.7120     
CPU times: user 9.64 s, sys: 1.76 s, total: 11.4 s
Wall time: 26.9 s

In [8]:
data.shape


Out[8]:
(1000, 784)

labels.shape


In [9]:
labels[:5]


Out[9]:
array([[0],
       [0],
       [1],
       [1],
       [0]])

In [10]:
test = np.random.random(784).reshape(1,-1)
proba = model.predict_proba(test)
classes = model.predict_classes(test)
proba,classes


1/1 [==============================] - 0s
1/1 [==============================] - 0s
Out[10]:
(array([[ 0.13410421]], dtype=float32), array([[0]], dtype=int32))

merge layers


In [ ]: