Examples

Keras model sketch

fromkeras.layersimportInput,Dense,BatchNormalization,Dropoutfromkeras.modelsimportModelfromkeras.callbacksimportEarlyStopping,ModelCheckpoint,TensorBoardimportkeras.backendasKK.clear_session()a=EarlyStopping(monitor='val_loss',min_delta=0,patience=1,verbose=1,mode='auto')#will stop the model if val_loss does not improve for 2 consecutive epochs
b=ModelCheckpoint(filepath='../models/course5.hdf5',verbose=1,save_best_only=True)#save model weights after each epoch if val_loss improves
c=TensorBoard(log_dir='./logs/')#saves a log file for tensorboard; remember to save different runs to different subdirectories
callbacks=[a,b,c]model=Sequential()model.add(Dense(8,input_shape=(13,),activation='tanh',kernel_initializer='lecun_uniform'))model.add(BatchNormalization())model.add(Dense(5,activation='tanh',kernel_initializer='lecun_uniform'))model.add(Dense(2,activation='tanh',kernel_initializer='lecun_uniform'))model.add(Dropout(0.5))model.add(Dense(3,activation='softmax',kernel_initializer='lecun_uniform'))model.compile(optimizer=eval('Adam(lr=0.04)'),loss='categorical_crossentropy',metrics=['accuracy'])model.fit(X_train,y_train,epochs=100,validation_data=(X_test,y_test),callbacks=callbacks)# starts training
result=model.evaluate(X_test,y_test)print(result)

Keras CNN with Inception module

fromkeras.layersimportInput,MaxPooling2D,Flatten,Activation,Conv2D,AvgPool2D,Dense,Dropoutfromkeras.optimizersimportSGDimportkerasfromkeras.modelsimportModelfromkeras.callbacksimportEarlyStopping,ModelCheckpoint,TensorBoardimportkeras.backendasKi=i+1!mkdirmodels!mkdirmnist-conv-logsa=EarlyStopping(monitor='val_acc',min_delta=0,patience=5,verbose=1,mode='auto')#will stop the model if val_loss does not improve for 2 consecutive epochs
b=ModelCheckpoint(monitor='val_acc',filepath='./models/course6-cifar10-inception-'+str(i)+'.hdf5',verbose=1,save_best_only=True)#save model weights after each epoch if val_loss improves
c=TensorBoard(log_dir='./mnist-conv-logs/cifar10-inception-'+str(i))#saves a log file for tensorboard; remember to save different runs to different subdirectories
callbacks=[a,b,c]epochs=25lrate=0.01decay=lrate/epochssgd=SGD(lr=lrate,momentum=0.9,decay=decay,nesterov=False)input_img=Input(shape=(32,32,3))tower_1=Conv2D(9,(1,1),padding='same',activation='relu')(input_img)tower_1=Conv2D(9,(3,3),padding='same',activation='relu')(tower_1)tower_2=Conv2D(9,(1,1),padding='same',activation='relu')(input_img)tower_2=Conv2D(9,(5,5),padding='same',activation='relu')(tower_2)tower_3=MaxPooling2D((3,3),strides=(1,1),padding='same')(input_img)tower_3=Conv2D(9,(1,1),padding='same',activation='relu')(tower_3)output=keras.layers.concatenate([tower_1,tower_2,tower_3],axis=3)output=Flatten()(output)dense=Dense(64)(drop)out=Dense(10,activation='softmax')(dense)model=Model(inputs=input_img,outputs=out)model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])model.summary()