def get_weightnorm_model(data): from weightnorm import SGDWithWeightnorm from weightnorm import data_based_init num_classes = data[0] img_size = data[1].shape[1] x_train = data[1] model = Sequential() model.add(Dense(512, activation='relu', input_shape=(img_size,))) model.add(Dense(512, activation='relu')) model.add(Dense(num_classes, activation='softmax')) #model.summary() sgd_wn = SGDWithWeightnorm(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd_wn, metrics=['accuracy', 'top_k_categorical_accuracy']) data_based_init(model, x_train[:100]) return model
model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) # let's train the model using SGD + momentum (how original). EDIT: now with weight normalization, so slightly more original ;-) if doweightnorm: from weightnorm import SGDWithWeightnorm as SGD else: from keras.optimizers import SGD sgd_wn = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy',optimizer=sgd_wn,metrics=['accuracy']) # data based initialization of parameters if doweightnorm: from weightnorm import data_based_init data_based_init(model, X_train[:100]) if not data_augmentation: print('Not using data augmentation.') model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(X_test, Y_test), shuffle=True) else: print('Using real-time data augmentation.') # this will do preprocessing and realtime data augmentation datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset