def _reset(self): reg = WeightRegularizer() # a hack to make regularization variable reg.l1 = K.variable(0.0) reg.l2 = K.variable(0.0) data, nb_classes = self.data_mix() X, Y, Xv, Yv = data # input square image dimensions img_rows, img_cols = X.shape[-1], X.shape[-1] img_channels = X.shape[1] # save number of classes and instances self.nb_classes = nb_classes self.nb_inst = len(X) # convert class vectors to binary class matrices Y = np_utils.to_categorical(Y, nb_classes) Yv = np_utils.to_categorical(Yv, nb_classes) # here definition of the model happens model = Sequential() # double true for icnreased probability of conv layers if random.choice([True, True, False]): # Choose convolution #1 self.convAsz = random.choice([32, 64, 128]) model.add(Convolution2D(self.convAsz, 3, 3, border_mode='same', input_shape=(img_channels, img_rows, img_cols), W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) model.add(Convolution2D(self.convAsz, 3, 3, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) # Choose convolution size B (if needed) self.convBsz = random.choice([0, 32, 64]) if self.convBsz > 0: model.add(Convolution2D(self.convBsz, 3, 3, border_mode='same', W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) model.add(Convolution2D(self.convBsz, 3, 3, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) else: model.add(Flatten(input_shape=(img_channels, img_rows, img_cols))) self.convAsz = 0 self.convBsz = 0 # choose fully connected layer size self.densesz = random.choice([256, 512, 762]) model.add(Dense(self.densesz, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('softmax')) # let's train the model using SGD + momentum (how original). sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) X = X.astype('float32') Xv = Xv.astype('float32') X /= 255 Xv /= 255 self.data = (X, Y, Xv, Yv) self.model = model self.sgd = sgd # initial accuracy values self.best_val = 0.0 self.previous_acc = 0.0 self.reg = reg self.epoch_idx = 0 return self._get_obs()
def _reset(self): reg = WeightRegularizer() # a hack to make regularization variable reg.l1 = K.variable(0.0) reg.l2 = K.variable(0.0) data, nb_classes = self.data_mix() X, Y, Xv, Yv = data # input square image dimensions img_rows, img_cols = X.shape[-1], X.shape[-1] img_channels = X.shape[1] # save number of classes and instances self.nb_classes = nb_classes self.nb_inst = len(X) # convert class vectors to binary class matrices Y = np_utils.to_categorical(Y, nb_classes) Yv = np_utils.to_categorical(Yv, nb_classes) # here definition of the model happens model = Sequential() # double true for icnreased probability of conv layers if random.choice([True, True, False]): # Choose convolution #1 self.convAsz = random.choice([32,64,128]) model.add(Convolution2D(self.convAsz, 3, 3, border_mode='same', input_shape=(img_channels, img_rows, img_cols), W_regularizer = reg, b_regularizer = reg)) model.add(Activation('relu')) model.add(Convolution2D(self.convAsz, 3, 3, W_regularizer = reg, b_regularizer = reg)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) # Choose convolution size B (if needed) self.convBsz = random.choice([0,32,64]) if self.convBsz > 0: model.add(Convolution2D(self.convBsz, 3, 3, border_mode='same', W_regularizer = reg, b_regularizer = reg)) model.add(Activation('relu')) model.add(Convolution2D(self.convBsz, 3, 3, W_regularizer = reg, b_regularizer = reg)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) else: model.add(Flatten(input_shape=(img_channels, img_rows, img_cols))) self.convAsz = 0 self.convBsz = 0 # choose fully connected layer size self.densesz = random.choice([256,512,762]) model.add(Dense(self.densesz, W_regularizer = reg, b_regularizer = reg)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes, W_regularizer = reg, b_regularizer = reg)) model.add(Activation('softmax')) # let's train the model using SGD + momentum (how original). sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) X = X.astype('float32') Xv = Xv.astype('float32') X /= 255 Xv /= 255 self.data = (X,Y,Xv,Yv) self.model = model self.sgd = sgd # initial accuracy values self.best_val = 0.0 self.previous_acc = 0.0 self.reg = reg self.epoch_idx = 0 return self._get_obs()
def train_blueprint(self, lr, decay, momentum, batch_size, l1, l2, convs, fcs): X, Y, Xv, Yv = self.data nb_classes = self.nb_classes reg = WeightRegularizer() # a hack to make regularization variable reg.l1 = K.variable(0.0) reg.l2 = K.variable(0.0) # input square image dimensions img_rows, img_cols = X.shape[-1], X.shape[-1] img_channels = X.shape[1] # convert class vectors to binary class matrices Y = np_utils.to_categorical(Y, nb_classes) Yv = np_utils.to_categorical(Yv, nb_classes) # here definition of the model happens model = Sequential() has_convs = False # create all convolutional layers for val, use in convs: # Size of convolutional layer cnvSz = int(val * 127) + 1 if use < 0.5: continue has_convs = True model.add( Convolution2D(cnvSz, 3, 3, border_mode='same', input_shape=(img_channels, img_rows, img_cols), W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Dropout(0.25)) if has_convs: model.add(Flatten()) else: model.add(Flatten( input_shape=(img_channels, img_rows, img_cols))) # avoid excetpions on no convs # create all fully connected layers for val, use in fcs: if use < 0.5: continue # choose fully connected layer size densesz = int(1023 * val) + 1 model.add(Dense(densesz, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) # model.add(Dropout(0.5)) model.add(Dense(nb_classes, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('softmax')) # let's train the model using SGD + momentum (how original). sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) X = X.astype('float32') Xv = Xv.astype('float32') X /= 255 Xv /= 255 model = model sgd = sgd reg = reg # set parameters of training step sgd.lr.set_value(lr) sgd.decay.set_value(decay) sgd.momentum.set_value(momentum) reg.l1.set_value(l1) reg.l2.set_value(l2) # train model for one epoch_idx H = model.fit(X, Y, batch_size=int(batch_size), nb_epoch=10, shuffle=True) diverged = math.isnan(H.history['loss'][-1]) acc = 0.0 if not diverged: _, acc = model.evaluate(Xv, Yv) return diverged, acc
def train_blueprint(self, lr, decay, momentum, batch_size, l1, l2, convs, fcs): X, Y, Xv, Yv = self.data nb_classes = self.nb_classes reg = WeightRegularizer() # a hack to make regularization variable reg.l1 = K.variable(0.0) reg.l2 = K.variable(0.0) # input square image dimensions img_rows, img_cols = X.shape[-1], X.shape[-1] img_channels = X.shape[1] # convert class vectors to binary class matrices Y = np_utils.to_categorical(Y, nb_classes) Yv = np_utils.to_categorical(Yv, nb_classes) # here definition of the model happens model = Sequential() has_convs = False # create all convolutional layers for val, use in convs: # Size of convolutional layer cnvSz = int(val * 127) + 1 if use < 0.5: continue has_convs = True model.add(Convolution2D(cnvSz, 3, 3, border_mode='same', input_shape=(img_channels, img_rows, img_cols), W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Dropout(0.25)) if has_convs: model.add(Flatten()) else: model.add(Flatten(input_shape=(img_channels, img_rows, img_cols))) # avoid excetpions on no convs # create all fully connected layers for val, use in fcs: if use < 0.5: continue # choose fully connected layer size densesz = int(1023 * val) + 1 model.add(Dense(densesz, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('relu')) # model.add(Dropout(0.5)) model.add(Dense(nb_classes, W_regularizer=reg, b_regularizer=reg)) model.add(Activation('softmax')) # let's train the model using SGD + momentum (how original). sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) X = X.astype('float32') Xv = Xv.astype('float32') X /= 255 Xv /= 255 model = model sgd = sgd reg = reg # set parameters of training step sgd.lr.set_value(lr) sgd.decay.set_value(decay) sgd.momentum.set_value(momentum) reg.l1.set_value(l1) reg.l2.set_value(l2) # train model for one epoch_idx H = model.fit(X, Y, batch_size=int(batch_size), nb_epoch=10, shuffle=True) diverged = math.isnan(H.history['loss'][-1]) acc = 0.0 if not diverged: _, acc = model.evaluate(Xv, Yv) return diverged, acc