In [1]:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
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)
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)
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)
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)
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)
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)
In [8]:
data.shape
Out[8]:
labels.shape
In [9]:
labels[:5]
Out[9]:
In [10]:
test = np.random.random(784).reshape(1,-1)
proba = model.predict_proba(test)
classes = model.predict_classes(test)
proba,classes
Out[10]:
In [ ]: